fbpx

How to use Mosel with Octeract Engine

Mosel

Get Started

Want to solve a problem written in Mosel? Let us guide you through it.

Our section on solving your first problem has all the instructions you need to get up and running. After going through this, you’ll have:

Whether Mosel is your preferred modelling language or you would like to try it out, you’ll be able to start solving with the Engine soon.

Step 1: Download and Install FICO Xpress

To get started, go to the FICO Xpress Community Licence page and complete the form on the right-hand side. Once you’ve done this, you’ll be redirected to a page where you can download the executable for Windows. Follow the instructions on the installation wizard to install both the solver (FICO Xpress) and the language (Mosel) on your PC.

Step 2: Write a Model in Mosel

To do this, you can either open text editor (Notepad, Notepad ++, etc.) or FICO Xpress Workbench. In this tutorial, we’ll use text editor. Copy and paste the snippet below to create a model with defined variable bounds, an objective function and a constraint. Save the model as .mos. In this tutorial, we’ll save it as my_example.mos

model "test - NL version"
 uses "nlsolv"

 parameters
   SOLVER="octeract-engine"
   SOLVERPATH=""
   SOLVEROPTIONS=""
 end-parameters

 declarations
   RN = 1..5
   x: array(RN) of mpvar        ! Decision variables
   Obj: nlctr                   ! Objective function
   Con: nlctr                   ! Constraint function
 end-declarations

 forall(i in RN) x(i) is_free

 ! Variable bounds
 forall(i in RN) x(i) >= 0
 forall(i in RN) x(i) <= 1

! Objective
 Obj:= 42*x(1) - 0.5*(100*x(1)*x(1) + 100*x(2)*x(2) + 
 100*x(3)*x(3) + 100*x(4)*x(4) + 100*x(5)*x(5)) + 44*x(2) + 
45*x(3) + 47*x(4) + 47.5*x(5)

! Constraint
 Con:= 20*x(1) + 12*x(2) + 11*x(3) + 7*x(4) + 4*x(5) <= 40

! Configuration of the solver
 setparam("nl_verbose", true)
 setparam("nl_solver", SOLVER)
 if SOLVERPATH<>"" then
   setparam("nl_solverpath", SOLVERPATH)
 end-if
 if SOLVEROPTIONS<>"" then
   setparam("nl_options", SOLVEROPTIONS)
 end-if

! Solve the problem
 minimise(Obj)

! Solution reporting
 if getprobstat<>NL_OPT and getprobstat<>NL_UNF then
   writeln("No solution available. Solver status: ", getparam("NL_STATUS"))
 else
   writeln("Solution: ", getobjval)
   forall(j in RN)
     writeln(strfmt(getsol(x(j)),10,5))
 end-if

end-model

Step 3: Solve the Model

Open a PowerShell session. Navigate to where Mosel and Xpress were installed on your PC. This should be in the bin folder. In this tutorial, the path is C:\Users\emmel\xpressmp\bin. Run the command in the snippet below, including the path where you’ve saved the .mos file on your PC. In this tutorial, the path is C:\Users\emmel\Documents\my_example.mos. The problem will be processed and the solution displayed in PowerShell.

mosel exec '..\..\Documents\my_example.mos'
========================================
 Octeract Engine v1.07.29
 Copyright (c) Octeract Ltd, 2020
========================================

Loading problem...
Preprocessing problem... 100% complete
Presolve time : 0.11s

-----------------------------------------------------------------------------------------
 Iteration            GAP               LLB          BUB            Pool      Time     Mem
-----------------------------------------------------------------------------------------
        13    1.254e-08 (  0.00%)   -1.700e+01   -1.700e+01            7      0.2s    23.0MB

Objective value at global solution: -1.700e+01
Solved_To_Global_Optimality
Solution file written to: C:\Users\emmel\AppData\Local\Temp\\nl_1fe2552bdb0.octsol
Solved_To_Global_Optimality
Solution: -16.99999999

The full solution is written and stored in a file. This can be located on your PC by using the path in the “Solution file written to” line. In this example, the path is C:\Users\emmel\AppData\Local\Temp\. From there, you’ll be able to find the solution file (.octsol) and open it in text editor. This will allow you to view a more detailed version of the solution.

And that’s it. Well done!
You’ve just solved an optimisation problem written in Mosel.

Leave a Reply