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 2025, Gurobi Optimization, LLC */
/* This example formulates and solves the following simple nonlinear model:
minimize y1 + y2
subject to y1 = x2 ^2
y2 = sin(2.5 * x1) + x2
y1, y2 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[4] = {-GRB_INFINITY, -GRB_INFINITY, -1.0, -1.0};
double ub[4] = {GRB_INFINITY, GRB_INFINITY, 1.0, 1.0};
double obj[4] = {1.0, 1.0, 0.0, 0.0};
char *varnames[4] = {"y1", "y2", "x1", "x2"};
/* Array representation of first expression tree for x2 ^2 */
int opcode1[3] = {GRB_OPCODE_POW, GRB_OPCODE_VARIABLE, GRB_OPCODE_CONSTANT};
double data1[3] = {-1.0, 3.0, 2.0};
int parent1[3] = {-1, 0, 0};
/* Array representation of second expression tree for sin(2.5 * x1) + x2 */
int opcode2[6] = {GRB_OPCODE_PLUS, GRB_OPCODE_SIN, GRB_OPCODE_MULTIPLY,
GRB_OPCODE_CONSTANT, GRB_OPCODE_VARIABLE,
GRB_OPCODE_VARIABLE};
double data2[6] = {-1.0, -1.0, -1.0, 2.5, 2.0, 3.0};
int parent2[6] = {-1, 0, 1, 2, 2, 0};
/* Data for querying solution information */
double sol[4];
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", 4, obj, lb, ub, NULL,
varnames);
if (error) goto QUIT;
/* Add first nonlinear constraint y1 = x2 ^2 */
error = GRBaddgenconstrNL(model, NULL, 0, 3, opcode1, data1, parent1);
if (error) goto QUIT;
/* Add second nonlinear constraint y2 = sin(2.5 * x1) + x2 */
error = GRBaddgenconstrNL(model, NULL, 1, 6, opcode2, data2, parent2);
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;
printf("\nOptimization complete\n");
if (optimstatus == GRB_OPTIMAL) {
error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
if (error) goto QUIT;
error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 4, sol);
if (error) goto QUIT;
printf("Optimal objective: %.4e\n", objval);
printf(" y1=%.6f, y2=%.6f, x1=%.6f, x2=%.6f\n", sol[0], sol[1], sol[2], sol[3]);
} 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 2025, Gurobi Optimization, LLC */
/* This example formulates and solves the following simple nonlinear model:
minimize y1 + y2
subject to y1 = x2 ^2
y2 = sin(2.5 * x1) + x2
y1, y2 free
-1 <= x1, x2 <= 1
*/
#include "gurobi_c++.h"
using namespace std;
int
main(int argc,
char *argv[])
{
try {
// Create an environment
GRBEnv env = GRBEnv("genconstrnl.log");
// Create an empty model
GRBModel model = GRBModel(env);
// Create variables, only y variables have an objective coefficient
GRBVar y1 = model.addVar(-GRB_INFINITY, GRB_INFINITY, 1.0, GRB_CONTINUOUS,
"y1");
GRBVar y2 = model.addVar(-GRB_INFINITY, GRB_INFINITY, 1.0, GRB_CONTINUOUS,
"y2");
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");
// Array representation of first expression tree for x2 ^2
int opcode1[3] = {GRB_OPCODE_POW, GRB_OPCODE_VARIABLE, GRB_OPCODE_CONSTANT};
double data1[3] = {-1.0, x2.index(), 2.0};
int parent1[3] = {-1, 0, 0};
// Add first nonlinear constraint y1 = x2 ^2
model.addGenConstrNL(y1, 3, opcode1, data1, parent1);
// Array representation of second expression tree for sin(2.5 * x1) + x2
int opcode2[6] = {GRB_OPCODE_PLUS, GRB_OPCODE_SIN, GRB_OPCODE_MULTIPLY,
GRB_OPCODE_CONSTANT, GRB_OPCODE_VARIABLE,
GRB_OPCODE_VARIABLE};
double data2[6] = {-1.0, -1.0, -1.0, 2.5, x1.index(), x2.index()};
int parent2[6] = {-1, 0, 1, 2, 2, 0};
// Add second nonlinear constraint y2 = sin(2.5 *x 1) + x2
model.addGenConstrNL(y2, 6, opcode2, data2, parent2);
// Optimize model
model.optimize();
cout << y1.get(GRB_StringAttr_VarName) << " "
<< y1.get(GRB_DoubleAttr_X) << endl;
cout << y2.get(GRB_StringAttr_VarName) << " "
<< y2.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 2025, Gurobi Optimization, LLC */
/* This example formulates and solves the following simple nonlinear model:
minimize y1 + y2
subject to y1 = x2 ^2
y2 = sin(2.5 * x1) + x2
y1, y2 free
-1 <= x1, x2 <= 1
*/
using System;
using Gurobi;
class mip1_cs
{
static void Main()
{
try {
// Create environment
GRBEnv env = new GRBEnv("genconstrnl.log");
// Create empty model
GRBModel model = new GRBModel(env);
// Create variables, only y variables have an objective coefficient
GRBVar y1 = model.AddVar(-GRB.INFINITY, GRB.INFINITY, 1.0, GRB.CONTINUOUS,
"y1");
GRBVar y2 = model.AddVar(-GRB.INFINITY, GRB.INFINITY, 1.0, GRB.CONTINUOUS,
"y2");
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");
// Array representation of first expression tree for x2 ^2
int[] opcode1 = {GRB.OPCODE_POW, GRB.OPCODE_VARIABLE, GRB.OPCODE_CONSTANT};
double[] data1 = {-1.0, x2.Index, 2.0};
int[] parent1 = {-1, 0, 0};
// Add first nonlinear constraint y1 = x2 ^2
model.AddGenConstrNL(y1, opcode1, data1, parent1, "univariate_constraint");
// Array representation of second expression tree for sin(2.5 * x1) + x2
int[] opcode2 = {GRB.OPCODE_PLUS, GRB.OPCODE_SIN, GRB.OPCODE_MULTIPLY,
GRB.OPCODE_CONSTANT, GRB.OPCODE_VARIABLE,
GRB.OPCODE_VARIABLE};
double[] data2 = {-1.0, -1.0, -1.0, 2.5, x1.Index, x2.Index};
int[] parent2 = {-1, 0, 1, 2, 2, 0};
// Add second nonlinear constraint y2 = sin(2.5 * x1) + x2
model.AddGenConstrNL(y2, opcode2, data2, parent2, "multivariate_constraint");
// Optimize model
model.Optimize();
Console.WriteLine(y1.VarName + " " + y1.X);
Console.WriteLine(y2.VarName + " " + y2.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 2025, Gurobi Optimization, LLC */
/* This example formulates and solves the following simple nonlinear model:
minimize y1 + y2
subject to y1 = x2 ^2
y2 = sin(2.5 * x1) + x2
y1, y2 free
-1 <= x1, x2 <= 1
*/
import com.gurobi.gurobi.*;
public class GenconstrNL {
public static void main(String[] args) {
try {
// Create environment
GRBEnv env = new GRBEnv("genconstrnl.log");
// Create empty model
GRBModel model = new GRBModel(env);
// Create variables, only y variables have an objective coefficient
GRBVar y1 = model.addVar(-GRB.INFINITY, GRB.INFINITY, 1.0, GRB.CONTINUOUS,
"y1");
GRBVar y2 = model.addVar(-GRB.INFINITY, GRB.INFINITY, 1.0, GRB.CONTINUOUS,
"y2");
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");
// Array representation of first expression tree for x2 ^2
int opcode1[] = new int[] {GRB.OPCODE_POW, GRB.OPCODE_VARIABLE, GRB.OPCODE_CONSTANT};
double data1[] = new double[] {-1.0, x2.index(), 2.0};
int parent1[] = new int[] {-1, 0, 0};
// Add first nonlinear constraint y1 = x2 ^2
model.addGenConstrNL(y1, opcode1, data1, parent1, "univariate_constraint");
// Array representation of second expression tree for sin(2.5 * x1) + x2
int opcode2[] = new int[] {GRB.OPCODE_PLUS, GRB.OPCODE_SIN, GRB.OPCODE_MULTIPLY,
GRB.OPCODE_CONSTANT, GRB.OPCODE_VARIABLE,
GRB.OPCODE_VARIABLE};
double data2[] = new double[] {-1.0, -1.0, -1.0, 2.5, x1.index(), x2.index()};
int parent2[] = new int[] {-1, 0, 1, 2, 2, 0};
// Add second nonlinear constraint y2 = sin(2.5*x1) + x2
model.addGenConstrNL(y2, opcode2, data2, parent2, "multivariate_constraint");
// Optimize model
model.optimize();
System.out.println(y1.get(GRB.StringAttr.VarName)
+ " " +y1.get(GRB.DoubleAttr.X));
System.out.println(y2.get(GRB.StringAttr.VarName)
+ " " +y2.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 y1 + y2
# s.t. y1 = x2 ^2
# y2 = sin(2.5 * x1) + x2
# y1, y2 free
# -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 variables for nonlinear constraints
y1 = model.addVar(lb=-float("inf"), obj=1.0, name="y1")
y2 = model.addVar(lb=-float("inf"), obj=1.0, name="y2")
# First nonlinear constraint y1 = x2 ^2
model.addGenConstrNL(y1, x2**2, name="univariate_constraint")
# Second nonlinear constraint y2 = sin(2.5 * x1) + x2
model.addGenConstrNL(y2, nlfunc.sin(2.5 * x1) + x2, name="multivariate_constraint")
model.optimize()
print(f"x1={x1.X} x2={x2.X} y1={y1.X} y2={y2.X} obj={model.ObjVal}")
' Copyright 2025, Gurobi Optimization, LLC
'
' This example formulates and solves the following simple nonlinear model:
'
' minimize y1 + y2
' subject to y1 = x2 ^2
' y2 = sin(2.5 * x1) + x2
' y1, y2 free
' -1 <= x1, x2 <= 1
Imports System
Imports Gurobi
Class mip1_vb
Shared Sub Main()
Try
Dim env As GRBEnv = New GRBEnv("genconstrnl.log")
Dim model As GRBModel = New GRBModel(env)
' Create variables, only y variables have an objective coefficient
Dim y1 As GRBVar = model.AddVar(-GRB.INFINITY, GRB.INFINITY, 1.0,
GRB.CONTINUOUS, "y1")
Dim y2 As GRBVar = model.AddVar(-GRB.INFINITY, GRB.INFINITY, 1.0,
GRB.CONTINUOUS, "y2")
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")
' Array representation of first expression tree for x2 ^2
Dim opcode1 As Integer() = new Integer() {GRB.OPCODE_POW,
GRB.OPCODE_VARIABLE, GRB.OPCODE_CONSTANT}
Dim data1 As Double() = new Double() {-1.0, x2.Index, 2.0}
Dim parent1 As Integer() = new Integer() {-1, 0, 0}
' Add second nonlinear constraint y1 = x2 ^2
model.AddGenConstrNL(y1, opcode1, data1, parent1, "univariate_constraint")
' Array representation of second expression tree for sin(2.5 * x1) + x2
Dim opcode2 As Integer() = new Integer() {GRB.OPCODE_PLUS,
GRB.OPCODE_SIN, GRB.OPCODE_MULTIPLY, GRB.OPCODE_CONSTANT,
GRB.OPCODE_VARIABLE, GRB.OPCODE_VARIABLE}
Dim data2 As Double() = new Double() {-1.0, -1.0, -1.0, 2.5, x1.Index, x2.Index}
Dim parent2 As Integer() = new Integer() {-1, 0, 1, 2, 2, 0}
' Add second nonlinear constraint y2 = sin(2.5 * x1) + x2
model.AddGenConstrNL(y2, opcode2, data2, parent2, "multivariate_constraint")
' Optimize model
model.Optimize()
Console.WriteLine(y1.VarName & " " & y1.X)
Console.WriteLine(y2.VarName & " " & y2.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