Gc_funcnonlinear Examples#
This section includes source code for all of the Gurobi gc_funcnonlinear examples.
The same source code can be found in the examples
directory of the
Gurobi distribution.
/* Copyright 2024, Gurobi Optimization, LLC
This example considers the following nonconvex nonlinear problem
minimize sin(x) + cos(2*x) + 1
subject to 0.25*exp(x) - x <= 0
-1 <= x <= 4
We show you two approaches to solve it as a nonlinear model:
1) Set the paramter FuncNonlinear = 1 to handle all general function
constraints as true nonlinear functions.
2) Set the attribute FuncNonlinear = 1 for each general function
constraint to handle these as true nonlinear functions.
*/
#include <stdlib.h>
#include <stdio.h>
#include "gurobi_c.h"
static int
printsol(GRBmodel *m)
{
double x[1];
double vio;
int error = 0;
error = GRBgetdblattrarray(m, "X", 0, 1, x);
if (error) goto QUIT;
printf("x = %g", x[0]);
QUIT:
return error;
}
int
main(int argc,
char *argv[])
{
GRBenv *env = NULL;
GRBmodel *model = NULL;
int error = 0;
int attrs[] = {1, 1, 1};
int ind[2];
double val[2];
/* Create environment */
error = GRBloadenv(&env, NULL);
if (error) goto QUIT;
/* Create a new model */
error = GRBnewmodel(env, &model, NULL, 0, NULL, NULL, NULL, NULL, NULL);
if (error) goto QUIT;
/* Add variables */
error = GRBaddvar(model, 0, NULL, NULL, 0.0, -1.0, 4.0, GRB_CONTINUOUS, "x");
if (error) goto QUIT;
error = GRBaddvar(model, 0, NULL, NULL, 0.0, -2.0, 8.0, GRB_CONTINUOUS, "twox");
if (error) goto QUIT;
error = GRBaddvar(model, 0, NULL, NULL, 0.0, -1.0, 1.0, GRB_CONTINUOUS, "sinx");
if (error) goto QUIT;
error = GRBaddvar(model, 0, NULL, NULL, 0.0, -1.0, 1.0, GRB_CONTINUOUS, "cos2x");
if (error) goto QUIT;
error = GRBaddvar(model, 0, NULL, NULL, 0.0, 0.0, GRB_INFINITY, GRB_CONTINUOUS, "expx");
if (error) goto QUIT;
/* Add constant term to objective */
error = GRBsetdblattr(model, "ObjCon", 1.0);
/* Add linear constraint: 0.25*expx - x <= 0 */
ind[0] = 4; ind[1] = 0;
val[0] = 0.25; val[1] = -1.0;
error = GRBaddconstr(model, 2, ind, val, GRB_LESS_EQUAL, 0.0, "c1");
if (error) goto QUIT;
/* Add linear constraint: 2*x - twox = 0 */
ind[0] = 0; ind[1] = 1;
val[0] = 2; val[1] = -1.0;
error = GRBaddconstr(model, 2, ind, val, GRB_EQUAL, 0.0, "c2");
if (error) goto QUIT;
/* Add general function constraint: sinx = sin(x) */
error = GRBaddgenconstrSin(model, "gcf1", 0, 2, NULL);
if (error) goto QUIT;
/* Add general function constraint: cos2x = cos(twox) */
error = GRBaddgenconstrCos(model, "gcf2", 1, 3, NULL);
if (error) goto QUIT;
/* Add general function constraint: expx = exp(x) */
error = GRBaddgenconstrExp(model, "gcf3", 0, 4, NULL);
if (error) goto QUIT;
/* Approach 1) Set FuncNonlinear parameter */
error = GRBsetintparam(GRBgetenv(model), "FuncNonlinear", 1);
if (error) goto QUIT;
/* Optimize the model and print solution */
error = GRBoptimize(model);
if (error) goto QUIT;
error = printsol(model);
if (error) goto QUIT;
/* Restore unsolved state */
error = GRBreset(model, 0);
if (error) goto QUIT;
/* Set FuncNonlinear parater back to its default value */
error = GRBsetintparam(GRBgetenv(model), "FuncNonlinear", 0);
if (error) goto QUIT;
/* Approach 2) Set FuncNonlinear attribute for every
general function constraint */
error = GRBsetintattrarray(model, "FuncNonlinear", 0, 3, attrs);
if (error) goto QUIT;
/* Optimize the model and print solution */
error = GRBoptimize(model);
if (error) goto QUIT;
error = printsol(model);
if (error) goto QUIT;
QUIT:
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 considers the following nonconvex nonlinear problem
minimize sin(x) + cos(2*x) + 1
subject to 0.25*exp(x) - x <= 0
-1 <= x <= 4
We show you two approaches to solve it as a nonlinear model:
1) Set the paramter FuncNonlinear = 1 to handle all general function
constraints as true nonlinear functions.
2) Set the attribute FuncNonlinear = 1 for each general function
constraint to handle these as true nonlinear functions.
*/
#if defined (WIN32) || defined (WIN64)
#include <Windows.h>
#endif
#include "gurobi_c++.h"
using namespace std;
static void
printsol(GRBModel& m, GRBVar& x)
{
cout << "x = " << x.get(GRB_DoubleAttr_X) << endl;
cout << "Obj = " << m.get(GRB_DoubleAttr_ObjVal) << endl;
}
int
main(int argc, char* argv[])
{
try {
// Create environment
GRBEnv env = GRBEnv();
// Create a new model
GRBModel m = GRBModel(env);
// Create variables
GRBVar x = m.addVar(-1.0, 4.0, 0.0, GRB_CONTINUOUS, "x");
GRBVar twox = m.addVar(-2.0, 8.0, 0.0, GRB_CONTINUOUS, "twox");
GRBVar sinx = m.addVar(-1.0, 1.0, 0.0, GRB_CONTINUOUS, "sinx");
GRBVar cos2x = m.addVar(-1.0, 1.0, 0.0, GRB_CONTINUOUS, "cos2x");
GRBVar expx = m.addVar(0.0, GRB_INFINITY, 0.0, GRB_CONTINUOUS, "expx");
// Set objective
m.setObjective(sinx + cos2x + 1, GRB_MINIMIZE);
// Add linear constraints
m.addConstr(0.25*expx - x <= 0, "l1");
m.addConstr(2*x - twox == 0, "l2");
// Add general function constraints
// sinx = sin(x)
GRBGenConstr gcf1 = m.addGenConstrSin(x, sinx, "gcf1");
// cos2x = cos(twox)
GRBGenConstr gcf2 = m.addGenConstrCos(twox, cos2x, "gcf2");
// expx = exp(x)
GRBGenConstr gcf3 = m.addGenConstrExp(x, expx, "gcf3");
// Approach 1) Set FuncNonlinear parameter
m.set(GRB_IntParam_FuncNonlinear, 1);
// Optimize the model and print solution
m.optimize();
printsol(m, x);
// Restore unsolved state and reset FuncNonlinear parameter to its
// default value
m.reset();
m.set(GRB_IntParam_FuncNonlinear, 0);
// Approach 2) Set FuncNonlinear attribute for every
// general function constraint
gcf1.set(GRB_IntAttr_FuncNonlinear, 1);
gcf2.set(GRB_IntAttr_FuncNonlinear, 1);
gcf3.set(GRB_IntAttr_FuncNonlinear, 1);
// Optimize the model and print solution
m.optimize();
printsol(m, x);
} 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 considers the following nonconvex nonlinear problem
minimize sin(x) + cos(2*x) + 1
subject to 0.25*exp(x) - x <= 0
-1 <= x <= 4
We show you two approaches to solve it as a nonlinear model:
1) Set the paramter FuncNonlinear = 1 to handle all general function
constraints as true nonlinear functions.
2) Set the attribute FuncNonlinear = 1 for each general function
constraint to handle these as true nonlinear functions.
*/
using System;
using Gurobi;
class gc_funcnonlinear_cs {
private static void printsol(GRBModel m, GRBVar x) {
Console.WriteLine("x = " + x.X);
Console.WriteLine("Obj = " + m.ObjVal);
}
static void Main() {
try {
// Create environment
GRBEnv env = new GRBEnv();
// Create a new model
GRBModel m = new GRBModel(env);
GRBVar x = m.AddVar(-1.0, 4.0, 0.0, GRB.CONTINUOUS, "x");
GRBVar twox = m.AddVar(-2.0, 8.0, 0.0, GRB.CONTINUOUS, "twox");
GRBVar sinx = m.AddVar(-1.0, 1.0, 0.0, GRB.CONTINUOUS, "sinx");
GRBVar cos2x = m.AddVar(-1.0, 1.0, 0.0, GRB.CONTINUOUS, "cos2x");
GRBVar expx = m.AddVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "expx");
// Set objective
m.SetObjective(sinx + cos2x + 1, GRB.MINIMIZE);
// Add linear constraints
m.AddConstr(0.25*expx - x <= 0, "l1");
m.AddConstr(2*x - twox == 0, "l2");
// Add general function constraints
// sinx = sin(x)
GRBGenConstr gcf1 = m.AddGenConstrSin(x, sinx, "gcf1", "");
// cos2x = cos(twox)
GRBGenConstr gcf2 = m.AddGenConstrCos(twox, cos2x, "gcf2", "");
// expx = exp(x)
GRBGenConstr gcf3 = m.AddGenConstrExp(x, expx, "gcf3", "");
// Approach 1) Set FuncNonlinear parameter
m.Parameters.FuncNonlinear = 1;
// Optimize the model and print solution
m.Optimize();
printsol(m, x);
// Restore unsolved state and set parameter Funcnonlinear to its
// default value
m.Reset();
m.Parameters.FuncNonlinear = 0;
// Approach 2) Set FuncNonlinear attribute for every
// general function constraint
gcf1.Set(GRB.IntAttr.FuncNonlinear, 1);
gcf2.Set(GRB.IntAttr.FuncNonlinear, 1);
gcf3.Set(GRB.IntAttr.FuncNonlinear, 1);
// Optimize the model and print solution
m.Optimize();
printsol(m, x);
// Dispose of model and environment
m.Dispose();
env.Dispose();
} catch (GRBException e) {
Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message);
}
}
}
/* Copyright 2024, Gurobi Optimization, LLC
This example considers the following nonconvex nonlinear problem
minimize sin(x) + cos(2*x) + 1
subject to 0.25*exp(x) - x <= 0
-1 <= x <= 4
We show you two approaches to solve it as a nonlinear model:
1) Set the paramter FuncNonlinear = 1 to handle all general function
constraints as true nonlinear functions.
2) Set the attribute FuncNonlinear = 1 for each general function
constraint to handle these as true nonlinear functions.
*/
import com.gurobi.gurobi.*;
public class GCFuncnonlinear {
private static void printsol(GRBModel m, GRBVar x)
throws GRBException {
assert(m.get(GRB.IntAttr.Status) == GRB.OPTIMAL);
System.out.println("x = " + x.get(GRB.DoubleAttr.X));
System.out.println("Obj = " + m.get(GRB.DoubleAttr.ObjVal));
}
public static void main(String[] args) {
try {
// Create environment
GRBEnv env = new GRBEnv();
// Create a new model
GRBModel m = new GRBModel(env);
// Create variables
GRBVar x = m.addVar(-1.0, 4.0, 0.0, GRB.CONTINUOUS, "x");
GRBVar twox = m.addVar(-2.0, 8.0, 0.0, GRB.CONTINUOUS, "twox");
GRBVar sinx = m.addVar(-1.0, 1.0, 0.0, GRB.CONTINUOUS, "sinx");
GRBVar cos2x = m.addVar(-1.0, 1.0, 0.0, GRB.CONTINUOUS, "cos2x");
GRBVar expx = m.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "expx");
// Set objective
GRBLinExpr obj = new GRBLinExpr();
obj.addTerm(1.0, sinx); obj.addTerm(1.0, cos2x); obj.addConstant(1.0);
m.setObjective(obj, GRB.MINIMIZE);
// Add linear constraints
GRBLinExpr expr = new GRBLinExpr();
expr.addTerm(0.25, expx); expr.addTerm(-1.0, x);
m.addConstr(expr, GRB.LESS_EQUAL, 0.0, "l1");
expr = new GRBLinExpr();
expr.addTerm(2.0, x); expr.addTerm(-1.0, twox);
m.addConstr(expr, GRB.EQUAL, 0.0, "l2");
// Add general function constraints
// sinx = sin(x)
GRBGenConstr gcf1 = m.addGenConstrSin(x, sinx, "gcf1", null);
// cos2x = cos(twox)
GRBGenConstr gcf2 = m.addGenConstrCos(twox, cos2x, "gcf2", null);
// expx = exp(x)
GRBGenConstr gcf3 = m.addGenConstrExp(x, expx, "gcf3", null);
// Approach 1) Set FuncNonlinear parameter
m.set(GRB.IntParam.FuncNonlinear, 1);
// Optimize the model and print solution
m.optimize();
printsol(m, x);
// Restore unsolved state and set parameter FuncNonlinear to
// its default value
m.reset();
m.set(GRB.IntParam.FuncNonlinear, 0);
// Approach 2) Set FuncNonlinear attribute for every
// general function constraint
gcf1.set(GRB.IntAttr.FuncNonlinear, 1);
gcf2.set(GRB.IntAttr.FuncNonlinear, 1);
gcf3.set(GRB.IntAttr.FuncNonlinear, 1);
// Optimize the model and print solution
m.optimize();
printsol(m, x);
// Dispose of model and environment
m.dispose();
env.dispose();
} catch (GRBException e) {
System.out.println("Error code: " + e.getErrorCode() + ". " +
e.getMessage());
}
}
}
function gc_funcnonlinear
% Copyright 2024, Gurobi Optimization, LLC
%
% This example considers the following nonconvex nonlinear problem
%
% minimize sin(x) + cos(2*x) + 1
% subject to 0.25*exp(x) - x <= 0
% -1 <= x <= 4
%
% We show you two approaches to solve it as a nonlinear model:
%
% 1) Set the paramter FuncNonlinear = 1 to handle all general function
% constraints as true nonlinear functions.
%
% 2) Set the attribute FuncNonlinear = 1 for each general function
% constraint to handle these as true nonlinear functions.
%
% Five variables, two linear constraints
m.varnames = {'x', 'twox', 'sinx', 'cos2x', 'expx'};
m.lb = [-1 -2 -1 -1 0];
m.ub = [4 8 1 1 +inf];
m.A = sparse([-1 0 0 0 0.25; 2 -1 0 0 0]);
m.rhs = [0; 0];
% Objective
m.modelsense = 'min';
m.obj = [0; 0; 1; 1; 0];
% Add general function constraints
% sinx = sin(x)
m.genconsin.xvar = 1;
m.genconsin.yvar = 3;
m.genconsin.name = 'gcf1';
m.genconcos.xvar = 2;
m.genconcos.yvar = 4;
m.genconcos.name = 'gcf2';
m.genconexp.xvar = 1;
m.genconexp.yvar = 5;
m.genconexp.name = 'gcf3';
% First approach: Set FuncNonlinear parameter
params.FuncNonlinear = 1;
% Solve and print solution
result = gurobi(m, params);
printsol(result.objval, result.x(1));
% Second approach: Set FuncNonlinear attribute for every
% general function constraint
m.genconsin.funcnonlinear = 1
m.genconcos.funcnonlinear = 1
m.genconexp.funcnonlinear = 1
% Solve and print solution
result = gurobi(m);
printsol(result.objval, result.x(1));
end
function printsol(objval, x)
fprintf('x = %g\n', x);
fprintf('Obj = %g\n', objval);
end
#!/usr/bin/env python3.11
# Copyright 2024, Gurobi Optimization, LLC
# This example considers the following nonconvex nonlinear problem
#
# minimize sin(x) + cos(2*x) + 1
# subject to 0.25*exp(x) - x <= 0
# -1 <= x <= 4
#
# We show you two approaches to solve it as a nonlinear model:
#
# 1) Set the paramter FuncNonlinear = 1 to handle all general function
# constraints as true nonlinear functions.
#
# 2) Set the attribute FuncNonlinear = 1 for each general function
# constraint to handle these as true nonlinear functions.
#
import gurobipy as gp
from gurobipy import GRB
def printsol(m, x):
print(f"x = {x.X}")
print(f"Obj = {m.ObjVal}")
try:
# Create a new model
m = gp.Model()
# Create variables
x = m.addVar(lb=-1, ub=4, name="x")
twox = m.addVar(lb=-2, ub=8, name="2x")
sinx = m.addVar(lb=-1, ub=1, name="sinx")
cos2x = m.addVar(lb=-1, ub=1, name="cos2x")
expx = m.addVar(name="expx")
# Set objective
m.setObjective(sinx + cos2x + 1, GRB.MINIMIZE)
# Add linear constraints
lc1 = m.addConstr(0.25 * expx - x <= 0)
lc2 = m.addConstr(2.0 * x - twox == 0)
# Add general function constraints
# sinx = sin(x)
gc1 = m.addGenConstrSin(x, sinx, "gc1")
# cos2x = cos(twox)
gc2 = m.addGenConstrCos(twox, cos2x, "gc2")
# expx = exp(x)
gc3 = m.addGenConstrExp(x, expx, "gc3")
# Approach 1) Set FuncNonlinear parameter
m.params.FuncNonlinear = 1
# Optimize the model
m.optimize()
printsol(m, x)
# Restore unsolved state and set parameter FuncNonlinear to
# its default value
m.reset()
m.resetParams()
# Approach 2) Set FuncNonlinear attribute for every
# general function constraint
gc1.FuncNonlinear = 1
gc2.FuncNonlinear = 1
gc3.FuncNonlinear = 1
m.optimize()
printsol(m, x)
except gp.GurobiError as e:
print(f"Error code {e.errno}: {e}")
except AttributeError:
print("Encountered an attribute error")
# Copyright 2024, Gurobi Optimization, LLC
#
# This example considers the following nonconvex nonlinear problem
#
# minimize sin(x) + cos(2*x) + 1
# subject to 0.25*exp(x) - x <= 0
# -1 <= x <= 4
#
# We show you two approaches to solve it as a nonlinear model:
#
# 1) Set the paramter FuncNonlinear = 1 to handle all general function
# constraints as true nonlinear functions.
#
# 2) Set the attribute FuncNonlinear = 1 for each general function
# constraint to handle these as true nonlinear functions.
library(gurobi)
printsol <- function(model, result) {
print(sprintf('%s = %g',
model$varnames[1], result$x[1]))
print(sprintf('Obj = %g', + result$objval))
}
model <- list()
# Five variables, two linear constraints
model$varnames <- c('x', 'twox', 'sinx', 'cos2x', 'expx')
model$lb <- c(-1, -2, -1, -1, 0)
model$ub <- c(4, 8, 1, 1, Inf)
model$A <- matrix(c(-1, 0, 0, 0, 0.25, 2, -1, 0, 0, 0), nrow=2, ncol=5, byrow=T)
model$rhs <- c(0, 0)
# Objective
model$modelsense <- 'min'
model$obj <- c(0, 0, 1, 1, 0)
model$objcon <- 1
# Set sinx = sin(x)
model$genconsin <- list()
model$genconsin[[1]] <- list()
model$genconsin[[1]]$xvar <- 1L
model$genconsin[[1]]$yvar <- 3L
model$genconsin[[1]]$name <- 'gcf1'
# Set cos2x = cos(twox)
model$genconcos <- list()
model$genconcos[[1]] <- list()
model$genconcos[[1]]$xvar <- 2L
model$genconcos[[1]]$yvar <- 4L
model$genconcos[[1]]$name <- 'gcf2'
# Set expx = exp(x)
model$genconexp <- list()
model$genconexp[[1]] <- list()
model$genconexp[[1]]$xvar <- 1L
model$genconexp[[1]]$yvar <- 5L
model$genconexp[[1]]$name <- 'gcf3'
# First approach: Set Funcnonlinear parameter
params <- list()
params$FuncNonlinear <- 1
# Solve and print solution
result = gurobi(model, params)
printsol(model, result)
# Second approach: Set FuncNonlinear attribute for every
# general function constraint
model$genconsin[[1]]$funcnonlinear <- 1
model$genconcos[[1]]$funcnonlinear <- 1
model$genconexp[[1]]$funcnonlinear <- 1
# Solve and print solution
result = gurobi(model)
printsol(model, result)
# Clear space
rm(model, result)
' Copyright 2024, Gurobi Optimization, LLC
'
' This example considers the following nonconvex nonlinear problem
'
' minimize sin(x) + cos(2*x) + 1
' subject to 0.25*exp(x) - x <= 0
' -1 <= x <= 4
'
' We show you two approaches to solve it as a nonlinear model:
'
' 1) Set the paramter FuncNonlinear = 1 to handle all general function
' constraints as true nonlinear functions.
'
' 2) Set the attribute FuncNonlinear = 1 for each general function
' constraint to handle these as true nonlinear functions.
'
Imports System
Imports Gurobi
Class gc_funcnonlinear_vb
Shared Sub printsol(m As GRBModel, x As GRBVar)
Console.WriteLine("x = " & x.X)
Console.WriteLine("Obj = " & m.ObjVal)
End Sub
Shared Sub Main()
Try
' Create environment
Dim env As New GRBEnv()
' Create a new m
Dim m As New GRBModel(env)
Dim x As GRBVar = m.AddVar(-1.0, 4.0, 0.0, GRB.CONTINUOUS, "x")
Dim twox As GRBVar = m.AddVar(-2.0, 8.0, 0.0, GRB.CONTINUOUS, "twox")
Dim sinx As GRBVar = m.AddVar(-1.0, 1.0, 0.0, GRB.CONTINUOUS, "sinx")
Dim cos2x As GRBVar = m.AddVar(-1.0, 1.0, 0.0, GRB.CONTINUOUS, "cos2x")
Dim expx As GRBVar = m.AddVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "expx")
' Set objective
m.SetObjective(sinx + cos2x + 1, GRB.MINIMIZE)
' Add linear constraints
m.AddConstr(0.25*expx - x <= 0, "l1")
m.AddConstr(2*x - twox = 0, "l2")
' Add general function constraints
' sinx = sin(x)
Dim gcf1 As GRBGenConstr = m.AddGenConstrSin(x, sinx, "gcf1", "")
' cos2x = cos(twox)
Dim gcf2 As GRBGenConstr = m.AddGenConstrCos(twox, cos2x, "gcf2", "")
' expx = exp(x)
Dim gcf3 As GRBGenConstr = m.AddGenConstrExp(x, expx, "gcf3", "")
' Approach 1) Set FuncNonlinear parameter
m.Parameters.FuncNonlinear = 1
' Optimize the model and print solution
m.Optimize()
printsol(m, x)
' Restore unsolved state and set parameter FuncNonlinear to
' its default value
m.Reset()
m.Parameters.FuncNonlinear = 0
' Approach 2) Set FuncNonlinear attribute for every
' general function constraint
gcf1.Set(GRB.IntAttr.FuncNonlinear, 1)
gcf2.Set(GRB.IntAttr.FuncNonlinear, 1)
gcf3.Set(GRB.IntAttr.FuncNonlinear, 1)
' Optimize the model and print solution
m.Optimize()
printsol(m, x)
' Dispose of model and environment
m.Dispose()
env.Dispose()
Catch e As GRBException
Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message)
End Try
End Sub
End Class