Formulating a problem

Modeling objects, such as objectives, variables and constraints, are contained in a model.

A model is a container of modeling objects, such as objectives, variables and constraints. You must explicitly add a constraint to the model or the CP Optimizer search engine will not use it in the search for a solution.

In the C++ API of CP Optimizer, a model is an instance of the class IloModel. Decision variables, objectives and constraints can be added to the model with the method IloModel::add.

In the Java™ API of CP Optimizer, the methods for adding object to the model are defined in the interfaces IloModel and IloModeler and implemented in the class IloCP. In particular, IloCP.add is one function for adding objects to the model.

Likewise, in the C# API of CP Optimizer, the methods for adding object to the model are defined in the interface IModeler and IModelImpl and implemented in the class CP. In particular, CP.Add is one function for adding objects to the model.

To create a model using the C++ API, the first step is to create an instance of the class IloEnv:


  IloEnv env;

(Note that creating an environment is not necessary in the C# and the Java APIs.)

The initialization of the environment creates internal data structures to be used in the rest of the code. Once this is done, you can create model objects; here illustrated in the C++ API:


  IloIntVar x(env, 0, 10);
  IloConstraint ct = (x != 0);

In the Java API, you must create the IloCP object before creating the modeling objects:


      IloCP cp = new IloCP();
      IloIntVar x = cp.intVar(0, 10);
      IloConstraint ct = cp.neq(x, 0);

Likewise in the C# API, you must create the CP object before creating the modeling objects:


      CP cp = new CP();
      IIntVar x = cp.IntVar(0, 10);
      IConstraint ct = cp.Neq(x, 0);

Once this is done, you can create model and fill it with modeling objects; here illustrated in the C++ API:


  IloModel model(env);
  model.add(ct);

In the Java API, you have already created the IloCP object before creating the modeling objects, so adding the constraint to the model is simply:


      cp.add(ct);

Likewise in the C# API, you have already created the CP object before creating the modeling objects, so adding the constraint to the model is simply:


      cp.Add(ct);

As soon as the model is completed, you are ready to solve the problem. The processes for searching for solutions to a model are introduced in Search in CP Optimizer.

When your problem solving is finished, you can reclaim memory for all modeling objects and clean up internal data structures by calling IloEnv::end for every environment you have created. This should be always be done before you exit your application.

To free memory used by a model in the Java API, you use the method IloCP.end. To free memory used by a model in the C# API, you use the method CP.End.

Note:

Model

A model is a container for modeling objects such as decision variables, objectives and constraints.