Note
Click here to download the full example code
Optimization using bonmin¶
In this example we are going to explore mixed-integer non linear problems optimization using OpenTURNS’ bonmin interface. %%
from __future__ import print_function
import openturns as ot
ot.Log.Show(ot.Log.NONE)
List available algorithms
for algo in ot.Bonmin.GetAlgorithmNames():
print(algo)
Out:
B-BB
B-OA
B-QG
B-Hyb
B-iFP
Details and references on bonmin algorithms are available here .
Setting up and solving a simple problem¶
The following example will demonstrate the use of bonmin “BB” algorithm to solve the following problem:
such that:
The theoretical minimum is reached for . At this point, the objective function value is
N.B.: OpenTURNS requires equality and inequality constraints to be stated as and , respectively. Thus the inequalities above will have to be restated to match this requirement:
Definition of objective function
objectiveFunction = ot.SymbolicFunction(
['x0', 'x1', 'x2', 'x3'], ['-x0 -x1 -x2'])
# Definition of variables bounds
bounds = ot.Interval([0, 0, 0, 0], [1, 1e99, 1e99, 5], [
True, True, True, True], [True, False, False, True])
# Definition of constraints
# Constraints in OpenTURNS are defined as g(x) = 0 and h(x) >= 0
# No equality constraint -> nothing to do
# Inequality constraints:
h = ot.SymbolicFunction(['x0', 'x1', 'x2', 'x3'], [
'-(x1-0.5)^2 - (x2-0.5)^2 + 0.25', 'x1 - x0', '-x0 - x2 - x3 + 2'])
# Definition of variables types
variablesType = [ot.OptimizationProblemImplementation.BINARY, ot.OptimizationProblemImplementation.CONTINUOUS,
ot.OptimizationProblemImplementation.CONTINUOUS, ot.OptimizationProblemImplementation.INTEGER]
# Setting up Bonmin problem
problem = ot.OptimizationProblem(objectiveFunction)
problem.setBounds(bounds)
problem.setVariablesType(variablesType)
problem.setInequalityConstraint(h)
bonminAlgorithm = ot.Bonmin(problem, 'B-BB')
bonminAlgorithm.setMaximumEvaluationNumber(10000)
bonminAlgorithm.setMaximumIterationNumber(1000)
bonminAlgorithm.setStartingPoint([0, 0, 0, 0])
ot.ResourceMap.AddAsString('Bonmin-mu_oracle', 'loqo')
ot.ResourceMap.AddAsScalar('Bonmin-bonmin.time_limit', 5)
Running the solver
bonminAlgorithm.run()
# Retrieving the results
result = bonminAlgorithm.getResult()
print(" -- Optimal point = " + result.getOptimalPoint().__str__())
print(" -- Optimal value = " + result.getOptimalValue().__str__())
print(" -- Evaluation number = " + result.getInputSample().getSize().__str__())
Out:
-- Optimal point = [1,1,0.500141,0]
-- Optimal value = [-2.50014]
-- Evaluation number = 147
Total running time of the script: ( 0 minutes 0.066 seconds)