Gc_pwl Examples#
This section includes source code for all of the Gurobi gc_pwl 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 model
with PWL constraints:
maximize
sum c[j] * x[j]
subject to
sum A[i,j] * x[j] <= 0, for i = 0, ..., m-1
sum y[j] <= 3
y[j] = pwl(x[j]), for j = 0, ..., n-1
x[j] free, y[j] >= 0, for j = 0, ..., n-1
where pwl(x) = 0, if x = 0
= 1+|x|, if x != 0
Note
1. sum pwl(x[j]) <= b is to bound x vector and also to favor sparse x vector.
Here b = 3 means that at most two x[j] can be nonzero and if two, then
sum x[j] <= 1
2. pwl(x) jumps from 1 to 0 and from 0 to 1, if x moves from negative 0 to 0,
then to positive 0, so we need three points at x = 0. x has infinite bounds
on both sides, the piece defined with two points (-1, 2) and (0, 1) can
extend x to -infinite. Overall we can use five points (-1, 2), (0, 1),
(0, 0), (0, 1) and (1, 2) to define y = pwl(x)
*/
#include <stdlib.h>
#include <stdio.h>
#include "gurobi_c.h"
int
main(int argc,
char *argv[])
{
GRBenv *env = NULL;
GRBmodel *model = NULL;
int *cbeg = NULL;
int *clen = NULL;
int *cind = NULL;
double *cval = NULL;
double *rhs = NULL;
char *sense = NULL;
double *lb = NULL;
double *obj = NULL;
int nz, i, j;
int status;
double objval;
int error = 0;
int n = 5;
int m = 5;
double c[] = { 0.5, 0.8, 0.5, 0.1, -1 };
double A[][5] = { {0, 0, 0, 1, -1},
{0, 0, 1, 1, -1},
{1, 1, 0, 0, -1},
{1, 0, 1, 0, -1},
{1, 0, 0, 1, -1} };
int npts = 5;
double xpts[] = {-1, 0, 0, 0, 1};
double ypts[] = {2, 1, 0, 1, 2};
/* Create environment */
error = GRBloadenv(&env, NULL);
if (error) goto QUIT;
/* Allocate memory and build the model */
nz = n; /* count nonzeros for n y variables */
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (A[i][j] != 0.0) nz++;
}
}
cbeg = (int *) malloc(2*n*sizeof(int));
clen = (int *) malloc(2*n*sizeof(int));
cind = (int *) malloc(nz*sizeof(int));
cval = (double *) malloc(nz*sizeof(double));
rhs = (double *) malloc((m+1)*sizeof(double));
sense = (char *) malloc((m+1)*sizeof(char));
lb = (double *) malloc(2*n*sizeof(double));
obj = (double *) malloc(2*n*sizeof(double));
for (j = 0; j < n; j++) {
/* for x variables */
lb[j] = -GRB_INFINITY;
obj[j] = c[j];
/* for y variables */
lb[j+n] = 0.0;
obj[j+n] = 0.0;
}
for (i = 0; i < m; i++) {
rhs[i] = 0.0;
sense[i] = GRB_LESS_EQUAL;
}
sense[m] = GRB_LESS_EQUAL;
rhs[m] = 3;
nz = 0;
for (j = 0; j < n; j++) {
cbeg[j] = nz;
for (i = 0; i < m; i++) {
if (A[i][j] != 0.0 ) {
cind[nz] = i;
cval[nz] = A[i][j];
nz++;
}
}
clen[j] = nz - cbeg[j];
}
for (j = 0; j < n; j++) {
cbeg[n+j] = nz;
clen[n+j] = 1;
cind[nz] = m;
cval[nz] = 1.0;
nz++;
}
error = GRBloadmodel(env, &model, "gc_pwl_c", 2*n, m+1,
GRB_MAXIMIZE, 0.0, obj, sense, rhs,
cbeg, clen, cind, cval, lb, NULL,
NULL, NULL, NULL);
if (error) goto QUIT;
/* Add piecewise constraints */
for (j = 0; j < n; j++) {
error = GRBaddgenconstrPWL(model, NULL, j, n+j, npts, xpts, ypts);
if (error) goto QUIT;
}
/* Optimize model */
error = GRBoptimize(model);
if (error) goto QUIT;
for (j = 0; j < n; j++) {
double x;
error = GRBgetdblattrelement(model, "X", j, &x);
if (error) goto QUIT;
printf("x[%d] = %g\n", j, x);
}
/* Report the result */
error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &status);
if (error) goto QUIT;
if (status != GRB_OPTIMAL) {
fprintf(stderr, "Error: it isn't optimal\n");
goto QUIT;
}
error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
if (error) goto QUIT;
printf("Obj: %g\n", objval);
QUIT:
/* Error reporting */
if (error) {
printf("ERROR: %s\n", GRBgeterrormsg(env));
exit(1);
}
/* Free data */
if (cbeg) free(cbeg);
if (clen) free(clen);
if (cind) free(cind);
if (cval) free(cval);
if (rhs) free(rhs);
if (sense) free(sense);
if (lb) free(lb);
if (obj) free(obj);
/* Free model */
GRBfreemodel(model);
/* Free environment */
GRBfreeenv(env);
return 0;
}
/* Copyright 2024, Gurobi Optimization, LLC
This example formulates and solves the following simple model
with PWL constraints:
maximize
sum c[j] * x[j]
subject to
sum A[i,j] * x[j] <= 0, for i = 0, ..., m-1
sum y[j] <= 3
y[j] = pwl(x[j]), for j = 0, ..., n-1
x[j] free, y[j] >= 0, for j = 0, ..., n-1
where pwl(x) = 0, if x = 0
= 1+|x|, if x != 0
Note
1. sum pwl(x[j]) <= b is to bound x vector and also to favor sparse x vector.
Here b = 3 means that at most two x[j] can be nonzero and if two, then
sum x[j] <= 1
2. pwl(x) jumps from 1 to 0 and from 0 to 1, if x moves from negative 0 to 0,
then to positive 0, so we need three points at x = 0. x has infinite bounds
on both sides, the piece defined with two points (-1, 2) and (0, 1) can
extend x to -infinite. Overall we can use five points (-1, 2), (0, 1),
(0, 0), (0, 1) and (1, 2) to define y = pwl(x)
*/
#include "gurobi_c++.h"
#include <sstream>
using namespace std;
int
main(int argc,
char *argv[])
{
int n = 5;
int m = 5;
double c[] = { 0.5, 0.8, 0.5, 0.1, -1 };
double A[][5] = { {0, 0, 0, 1, -1},
{0, 0, 1, 1, -1},
{1, 1, 0, 0, -1},
{1, 0, 1, 0, -1},
{1, 0, 0, 1, -1} };
int npts = 5;
double xpts[] = {-1, 0, 0, 0, 1};
double ypts[] = {2, 1, 0, 1, 2};
GRBEnv* env = 0;
GRBVar* x = 0;
GRBVar* y = 0;
try {
// Env and model
env = new GRBEnv();
GRBModel model = GRBModel(*env);
model.set(GRB_StringAttr_ModelName, "gc_pwl_c++");
// Add variables, set bounds and obj coefficients
x = model.addVars(n);
for (int i = 0; i < n; i++) {
x[i].set(GRB_DoubleAttr_LB, -GRB_INFINITY);
x[i].set(GRB_DoubleAttr_Obj, c[i]);
}
y = model.addVars(n);
// Set objective to maximize
model.set(GRB_IntAttr_ModelSense, GRB_MAXIMIZE);
// Add linear constraints
for (int i = 0; i < m; i++) {
GRBLinExpr le = 0;
for (int j = 0; j < n; j++) {
le += A[i][j] * x[j];
}
model.addConstr(le <= 0);
}
GRBLinExpr le1 = 0;
for (int j = 0; j < n; j++) {
le1 += y[j];
}
model.addConstr(le1 <= 3);
// Add piecewise constraints
for (int j = 0; j < n; j++) {
model.addGenConstrPWL(x[j], y[j], npts, xpts, ypts);
}
// Optimize model
model.optimize();
for (int j = 0; j < n; j++) {
cout << "x[" << j << "] = " << x[j].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;
}
delete[] x;
delete[] y;
delete env;
return 0;
}
/* Copyright 2024, Gurobi Optimization, LLC
This example formulates and solves the following simple model
with PWL constraints:
maximize
sum c[j] * x[j]
subject to
sum A[i,j] * x[j] <= 0, for i = 0, ..., m-1
sum y[j] <= 3
y[j] = pwl(x[j]), for j = 0, ..., n-1
x[j] free, y[j] >= 0, for j = 0, ..., n-1
where pwl(x) = 0, if x = 0
= 1+|x|, if x != 0
Note
1. sum pwl(x[j]) <= b is to bound x vector and also to favor sparse x vector.
Here b = 3 means that at most two x[j] can be nonzero and if two, then
sum x[j] <= 1
2. pwl(x) jumps from 1 to 0 and from 0 to 1, if x moves from negative 0 to 0,
then to positive 0, so we need three points at x = 0. x has infinite bounds
on both sides, the piece defined with two points (-1, 2) and (0, 1) can
extend x to -infinite. Overall we can use five points (-1, 2), (0, 1),
(0, 0), (0, 1) and (1, 2) to define y = pwl(x)
*/
using System;
using Gurobi;
public class gc_pwl_cs {
public static void Main() {
try {
int n = 5;
int m = 5;
double[] c = new double[] { 0.5, 0.8, 0.5, 0.1, -1 };
double[,] A = new double[,] { {0, 0, 0, 1, -1},
{0, 0, 1, 1, -1},
{1, 1, 0, 0, -1},
{1, 0, 1, 0, -1},
{1, 0, 0, 1, -1} };
double[] xpts = new double[] {-1, 0, 0, 0, 1};
double[] ypts = new double[] {2, 1, 0, 1, 2};
// Env and model
GRBEnv env = new GRBEnv();
GRBModel model = new GRBModel(env);
model.ModelName = "gc_pwl_cs";
// Add variables, set bounds and obj coefficients
GRBVar[] x = model.AddVars(n, GRB.CONTINUOUS);
for (int i = 0; i < n; i++) {
x[i].LB = -GRB.INFINITY;
x[i].Obj = c[i];
}
GRBVar[] y = model.AddVars(n, GRB.CONTINUOUS);
// Set objective to maximize
model.ModelSense = GRB.MAXIMIZE;
// Add linear constraints
for (int i = 0; i < m; i++) {
GRBLinExpr le = 0.0;
for (int j = 0; j < n; j++) {
le.AddTerm(A[i,j], x[j]);
}
model.AddConstr(le, GRB.LESS_EQUAL, 0, "cx" + i);
}
GRBLinExpr le1 = 0.0;
for (int j = 0; j < n; j++) {
le1.AddTerm(1.0, y[j]);
}
model.AddConstr(le1, GRB.LESS_EQUAL, 3, "cy");
// Add piecewise constraints
for (int j = 0; j < n; j++) {
model.AddGenConstrPWL(x[j], y[j], xpts, ypts, "pwl" + j);
}
// Optimize model
model.Optimize();
for (int j = 0; j < n; j++) {
Console.WriteLine("x[" + j + "] = " + x[j].X);
}
Console.WriteLine("Obj: " + model.ObjVal);
// Dispose of model and environment
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 model
with PWL constraints:
maximize
sum c[j] * x[j]
subject to
sum A[i,j] * x[j] <= 0, for i = 0, ..., m-1
sum y[j] <= 3
y[j] = pwl(x[j]), for j = 0, ..., n-1
x[j] free, y[j] >= 0, for j = 0, ..., n-1
where pwl(x) = 0, if x = 0
= 1+|x|, if x != 0
Note
1. sum pwl(x[j]) <= b is to bound x vector and also to favor sparse x vector.
Here b = 3 means that at most two x[j] can be nonzero and if two, then
sum x[j] <= 1
2. pwl(x) jumps from 1 to 0 and from 0 to 1, if x moves from negative 0 to 0,
then to positive 0, so we need three points at x = 0. x has infinite bounds
on both sides, the piece defined with two points (-1, 2) and (0, 1) can
extend x to -infinite. Overall we can use five points (-1, 2), (0, 1),
(0, 0), (0, 1) and (1, 2) to define y = pwl(x)
*/
import com.gurobi.gurobi.*;
import java.util.*;
public class GCPWL {
public static void main(String[] args) {
try {
int n = 5;
int m = 5;
double c[] = { 0.5, 0.8, 0.5, 0.1, -1 };
double A[][] = { {0, 0, 0, 1, -1},
{0, 0, 1, 1, -1},
{1, 1, 0, 0, -1},
{1, 0, 1, 0, -1},
{1, 0, 0, 1, -1} };
double xpts[] = {-1, 0, 0, 0, 1};
double ypts[] = {2, 1, 0, 1, 2};
// Env and model
GRBEnv env = new GRBEnv();
GRBModel model = new GRBModel(env);
model.set(GRB.StringAttr.ModelName, "GCPWL");
// Add variables, set bounds and obj coefficients
GRBVar[] x = model.addVars(n, GRB.CONTINUOUS);
for (int i = 0; i < n; i++) {
x[i].set(GRB.DoubleAttr.LB, -GRB.INFINITY);
x[i].set(GRB.DoubleAttr.Obj, c[i]);
}
GRBVar[] y = model.addVars(n, GRB.CONTINUOUS);
// Set objective to maximize
model.set(GRB.IntAttr.ModelSense, GRB.MAXIMIZE);
// Add linear constraints
for (int i = 0; i < m; i++) {
GRBLinExpr le = new GRBLinExpr();
for (int j = 0; j < n; j++) {
le.addTerm(A[i][j], x[j]);
}
model.addConstr(le, GRB.LESS_EQUAL, 0, "cx" + i);
}
GRBLinExpr le1 = new GRBLinExpr();
for (int j = 0; j < n; j++) {
le1.addTerm(1.0, y[j]);
}
model.addConstr(le1, GRB.LESS_EQUAL, 3, "cy");
// Add piecewise constraints
for (int j = 0; j < n; j++) {
model.addGenConstrPWL(x[j], y[j], xpts, ypts, "pwl" + j);
}
// Optimize model
model.optimize();
for (int j = 0; j < n; j++) {
System.out.println("x[" + j + "] = " + x[j].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());
}
}
}
function gc_pwl
% Copyright 2024, Gurobi Optimization, LLC
%
% This example formulates and solves the following simple model
% with PWL constraints:
%
% maximize
% sum c(j) * x(j)
% subject to
% sum A(i,j) * x(j) <= 0, for i = 1, ..., m
% sum y(j) <= 3
% y(j) = pwl(x(j)), for j = 1, ..., n
% x(j) free, y(j) >= 0, for j = 1, ..., n
%
% where pwl(x) = 0, if x = 0
% = 1+|x|, if x != 0
%
% Note
% 1. sum pwl(x(j)) <= b is to bound x vector and also to favor sparse x vector.
% Here b = 3 means that at most two x(j) can be nonzero and if two, then
% sum x(j) <= 1
% 2. pwl(x) jumps from 1 to 0 and from 0 to 1, if x moves from negative 0 to 0,
% then to positive 0, so we need three points at x = 0. x has infinite bounds
% on both sides, the piece defined with two points (-1, 2) and (0, 1) can
% extend x to -infinite. Overall we can use five points (-1, 2), (0, 1),
% (0, 0), (0, 1) and (1, 2) to define y = pwl(x)
n = 5;
% A x <= 0
A1 = [
0, 0, 0, 1, -1;
0, 0, 1, 1, -1;
1, 1, 0, 0, -1;
1, 0, 1, 0, -1;
1, 0, 0, 1, -1;
];
% sum y(j) <= 3
A2 = [1, 1, 1, 1, 1];
% Constraint matrix altogether
model.A = sparse(blkdiag(A1, A2));
% Right-hand-side coefficient vector
model.rhs = [zeros(n,1); 3];
% Objective function (x coefficients arbitrarily chosen)
model.obj = [0.5, 0.8, 0.5, 0.1, -1, zeros(1, n)];
% It's a maximization model
model.modelsense = 'max';
% Lower bounds for x and y
model.lb = [-inf*ones(n,1); zeros(n,1)];
% PWL constraints
for k = 1:n
model.genconpwl(k).xvar = k;
model.genconpwl(k).yvar = n + k;
model.genconpwl(k).xpts = [-1, 0, 0, 0, 1];
model.genconpwl(k).ypts = [2, 1, 0, 1, 2];
end
result = gurobi(model);
for k = 1:n
fprintf('x(%d) = %g\n', k, result.x(k));
end
fprintf('Objective value: %g\n', result.objval);
end
#!/usr/bin/env python3.11
# Copyright 2024, Gurobi Optimization, LLC
# This example formulates and solves the following simple model
# with PWL constraints:
#
# maximize
# sum c[j] * x[j]
# subject to
# sum A[i,j] * x[j] <= 0, for i = 0, ..., m-1
# sum y[j] <= 3
# y[j] = pwl(x[j]), for j = 0, ..., n-1
# x[j] free, y[j] >= 0, for j = 0, ..., n-1
# where pwl(x) = 0, if x = 0
# = 1+|x|, if x != 0
#
# Note
# 1. sum pwl(x[j]) <= b is to bound x vector and also to favor sparse x vector.
# Here b = 3 means that at most two x[j] can be nonzero and if two, then
# sum x[j] <= 1
# 2. pwl(x) jumps from 1 to 0 and from 0 to 1, if x moves from negative 0 to 0,
# then to positive 0, so we need three points at x = 0. x has infinite bounds
# on both sides, the piece defined with two points (-1, 2) and (0, 1) can
# extend x to -infinite. Overall we can use five points (-1, 2), (0, 1),
# (0, 0), (0, 1) and (1, 2) to define y = pwl(x)
#
import gurobipy as gp
from gurobipy import GRB
try:
n = 5
m = 5
c = [0.5, 0.8, 0.5, 0.1, -1]
A = [
[0, 0, 0, 1, -1],
[0, 0, 1, 1, -1],
[1, 1, 0, 0, -1],
[1, 0, 1, 0, -1],
[1, 0, 0, 1, -1],
]
# Create a new model
model = gp.Model("gc_pwl")
# Create variables
x = model.addVars(n, lb=-GRB.INFINITY, name="x")
y = model.addVars(n, name="y")
# Set objective
model.setObjective(gp.quicksum(c[j] * x[j] for j in range(n)), GRB.MAXIMIZE)
# Add Constraints
for i in range(m):
model.addConstr(gp.quicksum(A[i][j] * x[j] for j in range(n)) <= 0)
model.addConstr(y.sum() <= 3)
for j in range(n):
model.addGenConstrPWL(x[j], y[j], [-1, 0, 0, 0, 1], [2, 1, 0, 1, 2])
# Optimize model
model.optimize()
for j in range(n):
print(f"{x[j].VarName} = {x[j].X:g}")
print(f"Obj: {model.ObjVal:g}")
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 formulates and solves the following simple model
# with PWL constraints:
#
# maximize
# sum c(j) * x(j)
# subject to
# sum A(i,j) * x(j) <= 0, for i = 1, ..., m
# sum y(j) <= 3
# y(j) = pwl(x(j)), for j = 1, ..., n
# x(j) free, y(j) >= 0, for j = 1, ..., n
#
# where pwl(x) = 0, if x = 0
# = 1+|x|, if x != 0
#
# Note
# 1. sum pwl(x(j)) <= b is to bound x vector and also to favor sparse x vector.
# Here b = 3 means that at most two x(j) can be nonzero and if two, then
# sum x(j) <= 1
# 2. pwl(x) jumps from 1 to 0 and from 0 to 1, if x moves from negative 0 to 0,
# then to positive 0, so we need three points at x = 0. x has infinite bounds
# on both sides, the piece defined with two points (-1, 2) and (0, 1) can
# extend x to -infinite. Overall we can use five points (-1, 2), (0, 1),
# (0, 0), (0, 1) and (1, 2) to define y = pwl(x)
library(gurobi)
library(Matrix)
n = 5
# A x <= 0
A <- rbind(c(0, 0, 0, 1, -1),
c(0, 0, 1, 1, -1),
c(1, 1, 0, 0, -1),
c(1, 0, 1, 0, -1),
c(1, 0, 0, 1, -1))
# sum y(j) <= 3
y <- rbind(c(1, 1, 1, 1, 1))
# Initialize model
model <- list()
# Constraint matrix
model$A <- bdiag(A, y)
# Right-hand-side coefficient vector
model$rhs <- c(rep(0, n), 3)
# Objective function (x coefficients arbitrarily chosen)
model$obj <- c(0.5, 0.8, 0.5, 0.1, -1, rep(0, n))
# It's a maximization model
model$modelsense <- "max"
# Lower bounds for x and y
model$lb <- c(rep(-Inf, n), rep(0, n))
# PWL constraints
model$genconpwl <- list()
for (k in 1:n) {
model$genconpwl[[k]] <- list()
model$genconpwl[[k]]$xvar <- k
model$genconpwl[[k]]$yvar <- n + k
model$genconpwl[[k]]$xpts <- c(-1, 0, 0, 0, 1)
model$genconpwl[[k]]$ypts <- c(2, 1, 0, 1, 2)
}
# Solve the model and collect the results
result <- gurobi(model)
# Display solution values for x
for (k in 1:n)
print(sprintf('x(%d) = %g', k, result$x[k]))
print(sprintf('Objective value: %g', result$objval))
# Clear space
rm(model, result)
' Copyright 2024, Gurobi Optimization, LLC
'
' This example formulates and solves the following simple model
' with PWL constraints:
'
' maximize
' sum c[j] * x[j]
' subject to
' sum A[i,j] * x[j] <= 0, for i = 0, ..., m-1
' sum y[j] <= 3
' y[j] = pwl(x[j]), for j = 0, ..., n-1
' x[j] free, y[j] >= 0, for j = 0, ..., n-1
' where pwl(x) = 0, if x = 0
' = 1+|x|, if x != 0
'
' Note
' 1. sum pwl(x[j]) <= b is to bound x vector and also to favor sparse x vector.
' Here b = 3 means that at most two x[j] can be nonzero and if two, then
' sum x[j] <= 1
' 2. pwl(x) jumps from 1 to 0 and from 0 to 1, if x moves from negatie 0 to 0,
' then to positive 0, so we need three points at x = 0. x has infinite bounds
' on both sides, the piece defined with two points (-1, 2) and (0, 1) can
' extend x to -infinite. Overall we can use five points (-1, 2), (0, 1),
' (0, 0), (0, 1) and (1, 2) to define y = pwl(x)
Imports System
Imports Gurobi
Class gc_pwl_vb
Shared Sub Main()
Try
Dim n As Integer = 5
Dim m As Integer = 5
Dim c As Double() = New Double() {0.5, 0.8, 0.5, 0.1, -1}
Dim A As Double(,) = New Double(,) {{0, 0, 0, 1, -1}, _
{0, 0, 1, 1, -1}, _
{1, 1, 0, 0, -1}, _
{1, 0, 1, 0, -1}, _
{1, 0, 0, 1, -1}}
Dim xpts As Double() = New Double() {-1, 0, 0, 0, 1}
Dim ypts As Double() = New Double() {2, 1, 0, 1, 2}
' Env and model
Dim env As GRBEnv = New GRBEnv()
Dim model As GRBModel = New GRBModel(env)
model.ModelName = "gc_pwl_cs"
' Add variables, set bounds and obj coefficients
Dim x As GRBVar() = model.AddVars(n, GRB.CONTINUOUS)
For i As Integer = 0 To n - 1
x(i).LB = -GRB.INFINITY
x(i).Obj = c(i)
Next
Dim y As GRBVar() = model.AddVars(n, GRB.CONTINUOUS)
' Set objective to maximize
model.ModelSense = GRB.MAXIMIZE
' Add linear constraints
For i As Integer = 0 To m - 1
Dim le As GRBLinExpr = 0.0
For j As Integer = 0 To n - 1
le.AddTerm(A(i, j), x(j))
Next
model.AddConstr(le, GRB.LESS_EQUAL, 0, "cx" & i)
Next
Dim le1 As GRBLinExpr = 0.0
For j As Integer = 0 To n - 1
le1.AddTerm(1.0, y(j))
Next
model.AddConstr(le1, GRB.LESS_EQUAL, 3, "cy")
' Add piecewise constraints
For j As Integer = 0 To n - 1
model.AddGenConstrPWL(x(j), y(j), xpts, ypts, "pwl" & j)
Next
' Optimize model
model.Optimize()
For j As Integer = 0 To n - 1
Console.WriteLine("x[" & j & "] = " & x(j).X)
Next
Console.WriteLine("Obj: " & model.ObjVal)
' Dispose of model and environment
model.Dispose()
env.Dispose()
Catch e As GRBException
Console.WriteLine("Error code: " & e.ErrorCode & ". " & e.Message)
End Try
End Sub
End Class