Gc_funcnonlinear Examples#
This section includes source code for the gc_funcnonlinear example for Matlab and
R. The same source code can be found in the examples
directory of the Gurobi
distribution. For a nonlinear example with the APIs C, C++, Java, Python, and
Visual Basic refer to Genconstrnl Examples.
function gc_funcnonlinear
% Copyright 2025, 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 parameter 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];
m.sense = '<=';
% 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
# Copyright 2025, 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 parameter 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)
model$sense <- c('<', '=')
# 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)