Julia:通过最小化目标函数进行全局约束优化

时间:2017-10-11 23:04:24

标签: optimization julia mathematical-optimization julia-jump

我是JULIA语言的新手,我正在尝试通过优化P1和P2来最小化目标函数RMSE来执行具有约束的全局优化。我试图运行JumP算法但收到很多错误。如果你可以建议我一个有效的包,或者帮我修复我的错误,我会非常感激的吗?

using CSV
using JuMP, Ipopt
using Optim
using JuMP  
using GLPKMathProgInterface 


function READING()
   cd("\\\\RichardHadlee\\user1\\GalvezJ\\Documents\\JULIAPRO")
   table1 = CSV.read("table3.6.csv")
   Datas = Array(table1[1])
   Ydata = Array(table1[2])
   println( Datas, Ydata)
   return Datas, Ydata
end

function RMSE(Datas, P1,P2)
   ParticleSize(Datas, P1,P2) = (1.+(P1./Datas).^P2).^(-(1.-2./P2))
   RMSE(P1,P2) = sum(((ParticleSize(Datas, P1,P2) - Ydata)).^2.)
   result = RMSE3(P1,P2)
   return result
end

function FITTING2()
   Datas, Ydata = READING()
   println("read")
   myModel = Model(solver=GLPKSolverLP())

   @variable(myModel, P2 <= 1) 
   @variable(myModel, 2 <= P1) 

   JuMP.register(myModel, :RMSE, 2, RMSE, autodiff=true)
   @NLobjective(myModel, Min, RMSE(Datas, P1, P2))

   solve(myModel) # solves the model
   println("End")
end

FITTING2()

控制台中的错误消息

LoadError: MethodError: no method matching getname(::Int64

我已按指示进行了一些更改,但仍无法正常工作。如果有人可以提供帮助,我会复制它。对于这种类型的优化,任何替代方法都是非常受欢迎的。非常感谢!

function READING()
    cd("\\\\RichardHadlee\\user1\\GalvezJ\\Documents\\JULIAPRO")
    table1 = CSV.read("table3.6.csv")
    Datum = Array(table1[1])
    Ydata = Array(table1[2])
    global Datum, Ydata
    return Datum, Ydata
end

function RMSE_Error(P1,P2)
  global Datum, Ydata

  RMSE_Sum = 0.0
  Len_Datum = length(Datum)
  for i in 1:Len_Datum
     println(i," " ,Datum[i])
     ParticleSize(Datum,P1,P2) = (1.0+(P1/Datum[i])^P2)^(2/P2-1)
     println("PS ", ParticleSize(Datum,P1,P2))
     RMSE(P1,P2, Datum, Ydata) = (ParticleSize(Datum,P1,P2) - Ydata[i])^2.0
     RMSE_Sum = RMSE_Sum + RMSE(P1,P2, Datum, Ydata)
  end
  println("RMSE=", RMSE_Sum)
  return RMSE_Sum
end

function FITTING()
   Datum, Ydata = READING()
   println("read")
   myModel = Model(solver=GLPKSolverLP())
   @variable(myModel, P1 <= 1.) 
   @variable(myModel, P2 >= 2.) 
   JuMP.register(myModel, :RMSE_Error, 2, RMSE_Error, autodiff=true)
   @NLobjective(myModel, Min, RMSE_Error( P1, P2))
   solve(myModel) # solves the model
end

FITTING()

我设法找到了解决方案。如果没有更好的选择,我会坚持下去。谢谢!

using BalckBoxOptim

function sqerror(p, Datum, Ydata)

  println( p[1]," " ,p[2])
  err = 0.0
  for i in 1:length(Datum)
    pred_i =  (1.0+(p[1]/Datum[i])^p[2])^(2.0/p[2]-1.0)
    err += (Ydata[i] - pred_i)^2
   end
   println("Error: ", err)
   return err
 end

function Testing()
  Datum, Ydata = READING()
  res = bboptimize(p ->sqerror(p, Datum, Ydata); SearchRange = [(0.0001, 100.0), (2., 100.)], NumDimensions = 2, MaxSteps=5000000)
 end


Testing()

1 个答案:

答案 0 :(得分:2)

defining bounded @variables时,变量名必须出现在不等式的左侧(或链式比较的中间)。尝试将P1的顺序换成:

@variable(myModel, P1 >= 2)