Branin Function¶
Principles learned¶
Add float decision variables
Minimize a non-linear objective
Create a highly non-linear expression: with operators “cos” and “pow”
Problem¶
Branin function is defined by f(x) = a(x2 - b*x1² + c*x1 - r)² +
s(1-t)cos(x1) + s
For more details, see: branin.html
Download the exampleProgram¶
Constants are fixed to the recommended values: a=1
, b=5.1/(4π²)
,
c=5/π
, s=10
and t=1/(8π)
. The domains of x1
and x2
are
respectively [-5,10]
and [0,15]
.
The model is really straightforward: two decision variables x1
and x2
are declared, and then the Branin function to be minimized is applied to this
couple.
- Execution:
- hexaly branin.hxm [hxTimeLimit=] [solFileName=]
use io;
/* Declare the optimization model */
function model() {
PI = 3.14159265359;
a = 1;
b = 5.1 / (4 * pow(PI, 2));
c = 5 / PI;
r = 6;
s = 10;
t = 1 / (8 * PI);
x1 <- float(-5, 10);
x2 <- float(0, 15);
// f = a(x2 - b*x1^2 + c*x1 - r)^2 + s(1-t)cos(x1) + s
f <- a * pow(x2 - b * pow(x1, 2) + c * x1 - r, 2) + s * (1 - t) * cos(x1) + s;
minimize f;
}
/* Parametrize the solver */
function param() {
if (hxTimeLimit == nil) hxTimeLimit = 6;
}
/* Write the solution in a file */
function output() {
if (solFileName != nil)
{
local solFile = io.openWrite(solFileName);
solFile.println("x1=", x1.value);
solFile.println("x2=", x2.value);
}
}
- Execution (Windows)
- set PYTHONPATH=%HX_HOME%\bin\pythonpython branin.py
- Execution (Linux)
- export PYTHONPATH=/opt/hexaly_13_0/bin/pythonpython branin.py
import hexaly.optimizer
import sys
with hexaly.optimizer.HexalyOptimizer() as optimizer:
# Parameters of the function
PI = 3.14159265359
a = 1
b = 5.1 / (4 * pow(PI, 2))
c = 5 / PI
r = 6
s = 10
t = 1 / (8 * PI)
#
# Declare the optimization model
#
model = optimizer.model
# Numerical decisions
x1 = model.float(-5.0, 10.0)
x2 = model.float(0.0, 15.0)
# f = a(x2 - b*x1^2 + c*x1 - r)^2 + s(1-t)cos(x1) + s
f = a * (x2 - b * x1 ** 2 + c * x1 - r) ** 2 + s * (1 - t) * model.cos(x1) + s
# Minimize f
model.minimize(f)
model.close()
# Parameterize the optimizer
if len(sys.argv) >= 3:
optimizer.param.time_limit = int(sys.argv[2])
else:
optimizer.param.time_limit = 6
optimizer.solve()
#
# Write the solution in a file
#
if len(sys.argv) >= 2:
with open(sys.argv[1], 'w') as f:
f.write("x1=%f\n" % x1.value)
f.write("x2=%f\n" % x2.value)
- Compilation / Execution (Windows)
- cl /EHsc branin.cpp -I%HX_HOME%\include /link %HX_HOME%\bin\hexaly130.libbranin
- Compilation / Execution (Linux)
- g++ branin.cpp -I/opt/hexaly_13_0/include -lhexaly130 -lpthread -o braninbranin
#include "optimizer/hexalyoptimizer.h"
#include <fstream>
#include <iostream>
#include <vector>
using namespace hexaly;
using namespace std;
class Branin {
public:
// Hexaly Optimizer
HexalyOptimizer optimizer;
// Hexaly Program variables
HxExpression x1;
HxExpression x2;
void solve(int limit) {
// Parameters of the function
hxdouble PI = 3.14159265359;
hxdouble a = 1;
hxdouble b = 5.1 / (4 * pow(PI, 2.0));
hxdouble c = 5 / PI;
hxdouble r = 6;
hxdouble s = 10;
hxdouble t = 1 / (8 * PI);
// Declare the optimization model
HxModel model = optimizer.getModel();
// Numerical decisions
x1 = model.floatVar(-5.0, 10.0);
x2 = model.floatVar(0.0, 15.0);
// f = a(x2 - b*x1^2 + c*x1 - r)^2 + s(1-t)cos(x1) + s
HxExpression f = a * model.pow(x2 - b * model.pow(x1, 2) + c * x1 - r, 2) + s * (1 - t) * model.cos(x1) + s;
// Minimize f
model.minimize(f);
model.close();
// Parametrize the optimizer
optimizer.getParam().setTimeLimit(limit);
optimizer.solve();
}
/* Write the solution in a file */
void writeSolution(const string& fileName) {
ofstream outfile;
outfile.exceptions(ofstream::failbit | ofstream::badbit);
outfile.open(fileName.c_str());
outfile << "x1=" << x1.getDoubleValue() << endl;
outfile << "x2=" << x2.getDoubleValue() << endl;
}
};
int main(int argc, char** argv) {
const char* solFile = argc > 1 ? argv[1] : NULL;
const char* strTimeLimit = argc > 2 ? argv[2] : "6";
try {
Branin model;
model.solve(atoi(strTimeLimit));
if (solFile != NULL)
model.writeSolution(solFile);
} catch (const exception& e) {
cerr << "An error occurred: " << e.what() << endl;
return 1;
}
return 0;
}
- Compilation / Execution (Windows)
- copy %HX_HOME%\bin\Hexaly.NET.dll .csc Branin.cs /reference:Hexaly.NET.dllBranin
using System;
using System.IO;
using Hexaly.Optimizer;
public class Branin : IDisposable
{
// Hexaly Optimizer
private HexalyOptimizer optimizer;
// Hexaly Program variables
private HxExpression x1;
private HxExpression x2;
public Branin()
{
optimizer = new HexalyOptimizer();
}
public void Dispose()
{
if (optimizer != null)
optimizer.Dispose();
}
public void Solve(int limit)
{
// Parameters of the function
double PI = 3.14159265359;
double a = 1;
double b = 5.1 / (4 * Math.Pow(PI, 2.0));
double c = 5 / PI;
double r = 6;
double s = 10;
double t = 1 / (8 * PI);
// Declare the optimization model
HxModel model = optimizer.GetModel();
// Numerical decisions
x1 = model.Float(-5, 10);
x2 = model.Float(0, 15);
// f = a(x2 - b*x1^2 + c*x1 - r)^2 + s(1-t)cos(x1) + s
HxExpression f =
a * model.Pow(x2 - b * model.Pow(x1, 2) + c * x1 - r, 2)
+ s * (1 - t) * model.Cos(x1)
+ s;
// Minimize f
model.Minimize(f);
model.Close();
// Parametrize the optimizer
optimizer.GetParam().SetTimeLimit(limit);
optimizer.Solve();
}
/* Write the solution in a file */
public void WriteSolution(string fileName)
{
using (StreamWriter output = new StreamWriter(fileName))
{
output.WriteLine("x1=" + x1.GetDoubleValue());
output.WriteLine("x2=" + x2.GetDoubleValue());
}
}
public static void Main(string[] args)
{
string outputFile = args.Length > 0 ? args[0] : null;
string strTimeLimit = args.Length > 1 ? args[1] : "6";
using (Branin model = new Branin())
{
model.Solve(int.Parse(strTimeLimit));
if (outputFile != null)
model.WriteSolution(outputFile);
}
}
}
- Compilation / Execution (Windows)
- javac Branin.java -cp %HX_HOME%\bin\hexaly.jarjava -cp %HX_HOME%\bin\hexaly.jar;. Branin
- Compilation / Execution (Linux)
- javac Branin.java -cp /opt/hexaly_13_0/bin/hexaly.jarjava -cp /opt/hexaly_13_0/bin/hexaly.jar:. Branin
import java.io.*;
import com.hexaly.optimizer.*;
public class Branin {
// Parameters of the function
private static final double PI = 3.14159265359;
private static final int a = 1;
private static final double b = 5.1 / (4 * Math.pow(PI, 2));
private static final double c = 5 / PI;
private static final int r = 6;
private static final int s = 10;
private static final double t = 1 / (8 * PI);
// Hexaly Optimizer
private final HexalyOptimizer optimizer;
// Hexaly Program variables
private HxExpression x1;
private HxExpression x2;
private Branin(HexalyOptimizer optimizer) {
this.optimizer = optimizer;
}
private void solve(int limit) {
// Declare the optimization model
HxModel model = optimizer.getModel();
// Numerical decisions
x1 = model.floatVar(-5, 10);
x2 = model.floatVar(0, 15);
// f = a(x2 - b*x1^2 + c*x1 - r)^2 + s(1-t)cos(x1) + s
HxExpression f = model.sum();
// f1 = x2 - b*x1^2 + c*x1 - r
HxExpression f1 = model.sum();
f1.addOperand(x2);
f1.addOperand(model.prod(-b, model.pow(x1, 2)));
f1.addOperand(model.prod(c, x1));
f1.addOperand(-r);
// f = a*f1^2 + s(1-t)cos(x1) + s
f.addOperand(model.prod(a, model.pow(f1, 2)));
f.addOperand(model.prod(s * (1 - t), model.cos(x1)));
f.addOperand(s);
// minimize f
model.minimize(f);
// close model, then solve
model.close();
// Parametrize the optimizer
optimizer.getParam().setTimeLimit(limit);
optimizer.solve();
}
/* Write the solution in a file */
private void writeSolution(String fileName) throws IOException {
try (PrintWriter output = new PrintWriter(fileName)) {
output.println("x1=" + x1.getDoubleValue());
output.println("x2=" + x2.getDoubleValue());
}
}
public static void main(String[] args) {
String outputFile = args.length > 0 ? args[0] : null;
String strTimeLimit = args.length > 1 ? args[1] : "6";
try (HexalyOptimizer optimizer = new HexalyOptimizer()) {
Branin model = new Branin(optimizer);
model.solve(Integer.parseInt(strTimeLimit));
if (outputFile != null) {
model.writeSolution(outputFile);
}
} catch (Exception ex) {
System.err.println(ex);
ex.printStackTrace();
System.exit(1);
}
}
}