Jesus
Jesus

Reputation: 19

Julia: global constrained optimization by minimizing an objective function

I am new to JULIA language and I am trying to perform a global optimization with constraints by minimizing an objective function RMSE by optimizing P1 and P2. I tried to run JumP algorithm but received many errors. I will be ever so grateful if you could suggest me a package that works or else help me to fix my bug?

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()

ERROR MESSAGE in Console

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

I have made some changes as indicated but still does not work. I copy it bellow in case anyone can help. Any alternative method for this type of optimization would be more than welcome. Thanks a lot!

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()

I managed to get a solution. If there is no better alternative I will stick onto it. Thanks!

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()

Upvotes: 0

Views: 613

Answers (1)

mbauman
mbauman

Reputation: 31342

When defining bounded @variables, the variable name must appear on the left-hand side of the inequality (or in the middle of a chained comparison). Try swapping the order of P1 to:

@variable(myModel, P1 >= 2)

Upvotes: 2

Related Questions