Genconstrnl Examples#
This section includes source code for all of the Gurobi genconstrnl
examples. The same source code can be found in the examples
directory of the Gurobi distribution.
/* Copyright 2024, Gurobi Optimization, LLC */
/* This example formulates and solves the following simple nonlinear model:
minimize y
subject to y = sin(2.5*x1) + x2
y free
-1 <= x1, x2 <= 1
*/
#include <stdlib.h>
#include <stdio.h>
#include "gurobi_c.h"
int
main(int argc,
char *argv[])
{
GRBenv *env = NULL;
GRBmodel *model = NULL;
int error = 0;
/* Data for optimization variables */
double lb[3] = {-GRB_INFINITY, -1.0, -1.0};
double ub[3] = {GRB_INFINITY, 1.0, 1.0};
double obj[3] = {1.0, 0.0, 0.0};
char *varnames[3] = {"y", "x1", "x2"};
/* Array representation of expression tree for sin(2.5 * x1) + x2 */
int opcode[6] = {GRB_OPCODE_PLUS, GRB_OPCODE_SIN, GRB_OPCODE_MULTIPLY,
GRB_OPCODE_CONSTANT, GRB_OPCODE_VARIABLE,
GRB_OPCODE_VARIABLE};
double data[6] = {-1.0, -1.0, -1.0, 2.5, 1.0, 2.0};
int parent[6] = {-1, 0, 1, 2, 2, 0};
/* Data for querying solution information */
double sol[3];
int optimstatus;
double objval;
/* Create environment */
error = GRBloadenv(&env, "genconstrnl.log");
if (error) goto QUIT;
/* Create model with the three optimization variables */
error = GRBnewmodel(env, &model, "genconstrnl", 3, obj, lb, ub, NULL,
varnames);
if (error) goto QUIT;
/* Add general nonlinear constraint y = sin(2.5*x1) + x2 */
error = GRBaddgenconstrNL(model, NULL, 0, 6, opcode, data, parent);
if (error) goto QUIT;
/* Optimize model */
error = GRBoptimize(model);
if (error) goto QUIT;
/* Capture solution information */
error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
if (error) goto QUIT;
error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
if (error) goto QUIT;
error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, sol);
if (error) goto QUIT;
printf("\nOptimization complete\n");
if (optimstatus == GRB_OPTIMAL) {
printf("Optimal objective: %.4e\n", objval);
printf(" y=%.6f, x1=%.6f, x2=%.6f\n", sol[0], sol[1], sol[2]);
} else if (optimstatus == GRB_INF_OR_UNBD) {
printf("Model is infeasible or unbounded\n");
} else {
printf("Optimization was stopped early\n");
}
QUIT:
/* Error reporting */
if (error) {
printf("ERROR: %s\n", GRBgeterrormsg(env));
exit(1);
}
/* Free model */
GRBfreemodel(model);
/* Free environment */
GRBfreeenv(env);
return 0;
}
/* Copyright 2024, Gurobi Optimization, LLC */
/* This example formulates and solves the following simple nonlinear model:
minimize y
subject to y = sin(2.5*x1) + x2
y free
-1 <= x1, x2 <= 1
*/
#include "gurobi_c++.h"
using namespace std;
int
main(int argc,
char *argv[])
{
/* Array representation of expression tree for sin(2.5 * x1) + x2 */
int opcode[6] = {GRB_OPCODE_PLUS, GRB_OPCODE_SIN, GRB_OPCODE_MULTIPLY,
GRB_OPCODE_CONSTANT, GRB_OPCODE_VARIABLE,
GRB_OPCODE_VARIABLE};
double data[6] = {-1.0, -1.0, -1.0, 2.5, -1.0, -1.0};
int parent[6] = {-1, 0, 1, 2, 2, 0};
try {
// Create an environment
GRBEnv env = GRBEnv("genconstrnl.log");
// Create an empty model
GRBModel model = GRBModel(env);
// Create variables, only y has an objective coefficient
GRBVar y = model.addVar(-GRB_INFINITY, GRB_INFINITY, 1.0, GRB_CONTINUOUS,
"y");
GRBVar x1 = model.addVar(-1.0, 1.0, 0.0, GRB_CONTINUOUS, "x1");
GRBVar x2 = model.addVar(-1.0, 1.0, 0.0, GRB_CONTINUOUS, "x2");
// Finalize expression with variables indices
data[4] = x1.index();
data[5] = x2.index();
// Add general nonlinear constraint y = sin(2.5*x1) + x2
model.addGenConstrNL(y, 6, opcode, data, parent);
// Optimize model
model.optimize();
cout << y.get(GRB_StringAttr_VarName) << " "
<< y.get(GRB_DoubleAttr_X) << endl;
cout << x1.get(GRB_StringAttr_VarName) << " "
<< x1.get(GRB_DoubleAttr_X) << endl;
cout << x2.get(GRB_StringAttr_VarName) << " "
<< x2.get(GRB_DoubleAttr_X) << endl;
cout << "Obj: " << model.get(GRB_DoubleAttr_ObjVal) << endl;
} catch(GRBException e) {
cout << "Error code = " << e.getErrorCode() << endl;
cout << e.getMessage() << endl;
} catch(...) {
cout << "Exception during optimization" << endl;
}
return 0;
}
/* Copyright 2024, Gurobi Optimization, LLC */
/* This example formulates and solves the following simple nonlinear model:
minimize y
subject to y = sin(2.5*x1) + x2
y free
-1 <= x1, x2 <= 1
*/
using System;
using Gurobi;
class mip1_cs
{
static void Main()
{
/* Array representation of expression tree for sin(2.5 * x1) + x2 */
int[] opcode = {GRB.OPCODE_PLUS, GRB.OPCODE_SIN, GRB.OPCODE_MULTIPLY,
GRB.OPCODE_CONSTANT, GRB.OPCODE_VARIABLE,
GRB.OPCODE_VARIABLE};
double[] data = {-1.0, -1.0, -1.0, 2.5, -1.0, -1.0};
int[] parent = {-1, 0, 1, 2, 2, 0};
try {
// Create environment
GRBEnv env = new GRBEnv("genconstrnl.log");
// Create empty model
GRBModel model = new GRBModel(env);
// Create variables, only y has an objective coefficient
GRBVar y = model.AddVar(-GRB.INFINITY, GRB.INFINITY, 1.0, GRB.CONTINUOUS,
"y");
GRBVar x1 = model.AddVar(-1.0, 1.0, 0.0, GRB.CONTINUOUS, "x1");
GRBVar x2 = model.AddVar(-1.0, 1.0, 0.0, GRB.CONTINUOUS, "x2");
// Finalize expression with variables indices
data[4] = x1.Index;
data[5] = x2.Index;
// Add general nonlinear constraint y = sin(2.5*x1) + x2
model.AddGenConstrNL(y, opcode, data, parent, "nonlinear_constr");
// Optimize model
model.Optimize();
Console.WriteLine(y.VarName + " " + y.X);
Console.WriteLine(x1.VarName + " " + x1.X);
Console.WriteLine(x2.VarName + " " + x2.X);
Console.WriteLine("Obj: " + model.ObjVal);
// Dispose of model and env
model.Dispose();
env.Dispose();
} catch (GRBException e) {
Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message);
}
}
}
/* Copyright 2024, Gurobi Optimization, LLC */
/* This example formulates and solves the following simple nonlinear model:
minimize y
subject to y = sin(2.5*x1) + x2
y free
-1 <= x1, x2 <= 1
*/
import com.gurobi.gurobi.*;
public class GenconstrNL {
public static void main(String[] args) {
/* Array representation of expression tree for sin(2.5 * x1) + x2 */
int opcode[] = new int[] {GRB.OPCODE_PLUS, GRB.OPCODE_SIN, GRB.OPCODE_MULTIPLY,
GRB.OPCODE_CONSTANT, GRB.OPCODE_VARIABLE,
GRB.OPCODE_VARIABLE};
double data[] = new double[] {-1.0, -1.0, -1.0, 2.5, -1.0, -1.0};
int parent[] = new int[] {-1, 0, 1, 2, 2, 0};
try {
// Create environment
GRBEnv env = new GRBEnv("genconstrnl.log");
// Create empty model
GRBModel model = new GRBModel(env);
// Create variables, only y has an objective coefficient
GRBVar y = model.addVar(-GRB.INFINITY, GRB.INFINITY, 1.0, GRB.CONTINUOUS,
"x");
GRBVar x1 = model.addVar(-1.0, 1.0, 0.0, GRB.CONTINUOUS, "x1");
GRBVar x2 = model.addVar(-1.0, 1.0, 0.0, GRB.CONTINUOUS, "x2");
// Finalize expression with variables indices
data[4] = x1.index();
data[5] = x2.index();
// Add general nonlinear constraint y = sin(2.5*x1) + x2
model.addGenConstrNL(y, opcode, data, parent, "nonlinear_constr");
// Optimize model
model.optimize();
System.out.println(y.get(GRB.StringAttr.VarName)
+ " " +y.get(GRB.DoubleAttr.X));
System.out.println(x1.get(GRB.StringAttr.VarName)
+ " " +x1.get(GRB.DoubleAttr.X));
System.out.println(x2.get(GRB.StringAttr.VarName)
+ " " +x2.get(GRB.DoubleAttr.X));
System.out.println("Obj: " + model.get(GRB.DoubleAttr.ObjVal));
// Dispose of model and environment
model.dispose();
env.dispose();
} catch (GRBException e) {
System.out.println("Error code: " + e.getErrorCode() + ". " +
e.getMessage());
}
}
}
import gurobipy as gp
from gurobipy import nlfunc
# Formulate and solve the simple nonlinear model
# Minimize y
# s.t. y = sin(2.5 x1) + x2
# -1 <= x1, x2 <= 1
with gp.Env() as env, gp.Model(env=env) as model:
# Optimization variables
x1 = model.addVar(lb=-1, ub=1, name="x1")
x2 = model.addVar(lb=-1, ub=1, name="x2")
# Auxiliary resultant variable for general constraint
y = model.addVar(lb=-float("inf"), name="y")
# Nonlinear constraint for y
model.addGenConstrNL(y, nlfunc.sin(2.5 * x1) + x2)
# Use y for objective function
model.setObjective(y)
model.optimize()
print(f"x1={x1.X} x2={x2.X} obj={y.X}")
' Copyright 2024, Gurobi Optimization, LLC
'
' This example formulates and solves the following simple nonlinear model:
'
' minimize y
' subject to y = sin(2.5*x1) + x2
' y free
' -1 <= x1, x2 <= 1
Imports System
Imports Gurobi
Class mip1_vb
Shared Sub Main()
' Array representation of expression tree for sin(2.5 * x1) + x2
Dim opcode As Integer() = new Integer() {GRB.OPCODE_PLUS,
GRB.OPCODE_SIN, GRB.OPCODE_MULTIPLY, GRB.OPCODE_CONSTANT,
GRB.OPCODE_VARIABLE, GRB.OPCODE_VARIABLE}
Dim data As Double() = new Double() {-1.0, -1.0, -1.0, 2.5, -1.0, -1.0}
Dim parent As Integer() = new Integer() {-1, 0, 1, 2, 2, 0}
Try
Dim env As GRBEnv = New GRBEnv("genconstrnl.log")
Dim model As GRBModel = New GRBModel(env)
' Create variables, only y has an objective coefficient
Dim y As GRBVar = model.AddVar(-GRB.INFINITY, GRB.INFINITY, 1.0,
GRB.CONTINUOUS, "y")
Dim x1 As GRBVar = model.AddVar(-1.0, 1.0, 0.0, GRB.CONTINUOUS, "x1")
Dim x2 As GRBVar = model.AddVar(-1.0, 1.0, 0.0, GRB.CONTINUOUS, "x2")
' Finalize expression with variables indices
data(4) = x1.Index
data(5) = x2.Index
' Add general nonlinear constraint y = sin(2.5*x1) + x2
model.AddGenConstrNL(y, opcode, data, parent, "nonlinear_constr")
' Optimize model
model.Optimize()
Console.WriteLine(y.VarName & " " & y.X)
Console.WriteLine(x1.VarName & " " & x1.X)
Console.WriteLine(x2.VarName & " " & x2.X)
Console.WriteLine("Obj: " & model.ObjVal)
' Dispose of model and env
model.Dispose()
env.Dispose()
Catch e As GRBException
Console.WriteLine("Error code: " & e.ErrorCode & ". " & e.Message)
End Try
End Sub
End Class