gc_funcnonlinear_c++.cpp


/* Copyright 2023, 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;
}

Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.

Search