Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Definition of Optimization problem

Definition of Optimization problem

Hello

I'm trying to use IpOpt for dynamic optimization of an electrical model created with OMEdit but I always get the "Failed to build model:" message

I could complete the BatchReactor.mo example of the User's guide but the optimization for the attached simple example always fail
PruebaRLIpOpt.mo

The model can be loaded in the shell but the optimization command returns error:

optimize(nmpcPruebaRL, numberOfIntervals=16,stopTime=1,tolerance=1e-8)

Can someone provide an example of using OpIpt with a model built with OMEdit? Or any idea about how I should create my optimization problem?

Thank you

Attachments:

Re: Definition of Optimization problem

robarn,
I'm interested in the IpOpt optimization. I'm getting some problem after the part

Code:

 optimization nmpcBatchReactor(objective=-x2)

  extends BatchReactor;
end nmpcBatchReactor;

When I run

Code:

optimize(nmpcBatchReactor, numberOfIntervals=16, stopTime=1, tolerance=1e-8)

record SimulationResult
    resultFile = "«DOCHOME»/nmpcBatchReactor_res.mat",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 16, tolerance = 1e-08, method = 'optimization', fileNamePrefix = 'nmpcBatchReactor', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
    messages = "LOG_SUCCESS       | info    | The initialization finished successfully without homotopy method.

Optimizer Variables
========================================================
State[0]:x1(start = 1, nominal = 1, min = 0, max = 1, init = 1)
State[1]:x2(start = 0, nominal = 1, min = 0, max = 1, init = 0)
Input[2]:u(start = 0, nominal = 5, min = 0, max = 5)
--------------------------------------------------------
number of nonlinear constraints: 0
========================================================

******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
Ipopt is released as open source code under the Eclipse Public License (EPL).
         For more information visit http://projects.coin-or.org/Ipopt
******************************************************************************

LOG_SUCCESS       | info    | The simulation finished successfully.
",
    timeFrontend = 0.107220861,
    timeBackend = 0.065935538,
    timeSimCode = 0.001220293,
    timeTemplates = 0.010451172,
    timeCompile = 0.496370947,
    timeSimulation = 0.03213160900000001,
    timeTotal = 0.713488376
end SimulationResult;

The output is

Error occurred building AST
Syntax Error

Could you please tell me how you did, so that I can treasure your experience?

Re: Definition of Optimization problem

Sorry ElMastro

I don't understand your question.
Once you run "optimize(nmpcBatchReactor, numberOfIntervals=16, stopTime=1, tolerance=1e-8) " I see that you obtain a correct execution: LOG_SUCCESS. And your log for results it seems correct
So I don't know why you receive "Error occurred building AST Syntax Error"
When I run the optimize funcion I obtaint a result log similar to yours and I can access the .mat results file without problem

Best regards

Re: Definition of Optimization problem

Excuse me, I pasted the wrong file; the output is

Code:

optimize(nmpcBatchReactor, numberOfIntervals=16, stopTime=1, tolerance=1e-8)

record SimulationResult
    resultFile = "",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 16, tolerance = 1e-08, method = 'optimization', fileNamePrefix = 'nmpcBatchReactor', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
    messages = "Failed to build model: nmpcBatchReactor",
    timeFrontend = 0.065435036,
    timeBackend = 0.0,
    timeSimCode = 0.0,
    timeTemplates = 0.0,
    timeCompile = 0.0,
    timeSimulation = 0.0,
    timeTotal = 0.065895884
end SimulationResult;

Re: Definition of Optimization problem

You will need to run getErrorString() afterwards to see what the error is.

Re: Definition of Optimization problem

Thank for your help;
I run it on OMEdit, with the COmpiler CLI and the outout is succesful

Code:

optimize(nmpcBatchReactor, numberOfIntervals=16, stopTime=1, tolerance=1e-8)

record SimulationResult
    resultFile = "/tmp/OpenModelica_maxtro/OMEdit/nmpcBatchReactor_res.mat",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 16, tolerance = 1e-08, method = 'optimization', fileNamePrefix = 'nmpcBatchReactor', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
    messages = "LOG_SUCCESS       | info    | The initialization finished successfully without homotopy method.

Optimizer Variables
========================================================
State[0]:x1(start = 1, nominal = 1, min = 0, max = 1, init = 1)
State[1]:x2(start = 0, nominal = 1, min = 0, max = 1, init = 0)
Input[2]:u(start = 0, nominal = 5, min = 0, max = 5)
--------------------------------------------------------
number of nonlinear constraints: 0
========================================================

******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
Ipopt is released as open source code under the Eclipse Public License (EPL).
         For more information visit http://projects.coin-or.org/Ipopt
******************************************************************************

LOG_SUCCESS       | info    | The simulation finished successfully.
",
    timeFrontend = 0.410979041,
    timeBackend = 0.010253416,
    timeSimCode = 0.00122332,
    timeTemplates = 0.00288823,
    timeCompile = 0.799306899,
    timeSimulation = 0.070365205,
    timeTotal = 1.295109968
end SimulationResult;

getErrorString however doesn't show anything, neither on OMShell nor here. Now I'll try your model to get the use of it, if it happens to work, I'll let you know

Re: Definition of Optimization problem

sjoelund.se , in my OMEdit your model works:
I cut the last three lines

Code:

optimization nmpcPruebaRL(objective=resistor1.i-3)

extends PruebaRL;
end nmpcPruebaRL;

Then on the Compiler CLI I run

Code:

optimization nmpcPruebaRL(objective=resistor1.i-3)

extends PruebaRL;
end nmpcPruebaRL;
{nmpcPruebaRL}

optimize(nmpcPruebaRL, numberOfIntervals=16,stopTime=1,tolerance=1e-8)
record SimulationResult
    resultFile = "/tmp/OpenModelica_maxtro/OMEdit/nmpcPruebaRL_res.mat",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 16, tolerance = 1e-08, method = 'optimization', fileNamePrefix = 'nmpcPruebaRL', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
    messages = "LOG_SUCCESS       | info    | The initialization finished successfully without homotopy method.

Optimizer Variables
========================================================
Input[0]:u(start = 0, nominal = 1, min = -Inf, max = +Inf)
--------------------------------------------------------
number of nonlinear constraints: 0
========================================================
stdout            | info    | Jacobian or SparsePattern is not generated or failed to initialize! Switch back to normal.

******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
Ipopt is released as open source code under the Eclipse Public License (EPL).
         For more information visit http://projects.coin-or.org/Ipopt
******************************************************************************

stdout            | warning | No optimal solution found!
|                 | |       | Use -lv=LOG_IPOPT for more information.
LOG_SUCCESS       | info    | The simulation finished successfully.
",
    timeFrontend = 0.4063101739999999,
    timeBackend = 0.006439266000000001,
    timeSimCode = 0.001593736,
    timeTemplates = 0.002677573,
    timeCompile = 0.8018269769999999,
    timeSimulation = 0.037602812,
    timeTotal = 1.256511073
end SimulationResult;

getDocumentationAnnotation(BatchReactor)
{"","",""}

getClassComment(BatchReactor)
""

readSimulationResultVars("/tmp/OpenModelica_maxtro/OMEdit/nmpcPruebaRL_res.mat",true,false)
{"$OMC$objectMayerTerm","ground1.p.i","ground1.p.v","resistor1.LossPower","resistor1.R","resistor1.R_actual","resistor1.T","resistor1.T_heatPort","resistor1.T_ref","resistor1.alpha","resistor1.i","resistor1.n.i","resistor1.n.v","resistor1.p.i","resistor1.p.v","resistor1.v","signalVoltage1.i","signalVoltage1.n.i","signalVoltage1.n.v","signalVoltage1.p.i","signalVoltage1.p.v","signalVoltage1.v","time","u"}

About the results this come out to me

Re: Definition of Optimization problem

I have always used OMShell instead of Compiler CLI
How do you run the three lines of "optimization ..." in the Compiler CLI?
I always get the error:

Code:


"Error occurred building AST
Syntax Error
[<interactive>:1:0-1:12:writable] Error: Parser error: Unexpected token near: optimization (IDENT)"

I have tried to run it in the command line and also as a script. In this second case I obtain "Failed" as output of the runScript command

Re: Definition of Optimization problem

In the same folder of PruebaRLIpOpt.mo I start OMEdit;
on the menu there is Tools ->  Openmodelica Compiler CLI;
this starts the COmpiler CLI, I paste in it (at the bottom)

Code:

optimization nmpcPruebaRL(objective=resistor1.i-3)

extends PruebaRL;
end nmpcPruebaRL;

and click send;
after I paste

Code:

optimize(nmpcPruebaRL, numberOfIntervals=16,stopTime=1,tolerance=1e-8) 

, click send and this does the trick.

OMShell gives the same results as you (I don't understand why).

Re: Definition of Optimization problem

Thank you for your help, ElMastro
This is what I was trying but it doesn't work
I tried to change the working directory in OMEdit and also in Compiler CLI. But the error is the same. I will keep on trying.

May you tell me what version of OpenModelica and OMEdit are you using? In my case:
OMEdit v1.13.2 (64-bit) - Connected to v1.13.2 (64-bit)
And I am working on a folder without spaces or symbols in its name

Or maybe you can send me an image of the Compiler CLI window after the execution of the optimization command (the three lines)

Thank you

Re: Definition of Optimization problem

My version of openmodelica is 1.14.0.dev.r26546.g4ce17f701-1 (from GIT). I think that OMEdit is the same, the about dialog says

USER wrote:

Connected to OMCompiler v1.14.0-dev.26546+g4ce17f701
Connected to OMSimulator v2.1.0-dev-112-g0ae785f-linux

For the image here is a link, I got some problem attaching it
https://ibb.co/wpHVQZH

Re: Definition of Optimization problem

Thank you!

I forgot to use: setCommandLineOptions("-g=Optimica");

Now it works

Re: Definition of Optimization problem

I'm glad you succeeded, you're welcome

There are 0 guests and 0 other users also viewing this topic
You are here: