C++ Reference

C++ Reference: CP-SAT

Model

Detailed Description

Class that owns everything related to a particular optimization model.

This class is actually a fully generic wrapper that can hold any type of constraints, watchers, solvers and provide a mecanism to wire them together.

Definition at line 38 of file model.h.

Public Member Functions

 Model ()
 
template<typename T >
Add (std::function< T(Model *)> f)
 This makes it possible to have a nicer API on the client side, and it allows both of these forms: More...
 
template<typename T >
Get (std::function< T(const Model &)> f) const
 Similar to Add() but this is const. More...
 
template<typename T >
T * GetOrCreate ()
 Returns an object of type T that is unique to this model (like a "local" singleton). More...
 
template<typename T >
const T * Get () const
 Likes GetOrCreate() but do not create the object if it is non-existing. More...
 
template<typename T >
T * Mutable () const
 Same as Get(), but returns a mutable version of the object. More...
 
template<typename T >
void TakeOwnership (T *t)
 Gives ownership of a pointer to this model. More...
 
template<typename T >
T * Create ()
 This returns a non-singleton object owned by the model and created with the T(Model* model) constructor if it exist or the T() constructor otherwise. More...
 
template<typename T >
void Register (T *non_owned_class)
 Register a non-owned class that will be "singleton" in the model. More...
 

Constructor & Destructor Documentation

◆ Model()

Model ( )
inline

Definition at line 40 of file model.h.

Member Function Documentation

◆ Add()

T Add ( std::function< T(Model *)>  f)
inline

This makes it possible to have a nicer API on the client side, and it allows both of these forms:

  • ConstraintCreationFunction(contraint_args, &model);
  • model.Add(ConstraintCreationFunction(contraint_args));

The second form is a bit nicer for the client and it also allows to store constraints and add them later. However, the function creating the constraint is slighly more involved.

std::function<void(Model*)> ConstraintCreationFunction(contraint_args) {
return [=] (Model* model) {
... the same code ...
};
}

We also have a templated return value for the functions that need it like

const BooleanVariable b = model.Add(NewBooleanVariable());
const IntegerVariable i = model.Add(NewWeightedSum(weights, variables));

Definition at line 67 of file model.h.

◆ Create()

T* Create ( )
inline

This returns a non-singleton object owned by the model and created with the T(Model* model) constructor if it exist or the T() constructor otherwise.

It is just a shortcut to new + TakeOwnership().

Definition at line 143 of file model.h.

◆ Get() [1/2]

const T* Get ( ) const
inline

Likes GetOrCreate() but do not create the object if it is non-existing.

This returns a const version of the object.

Definition at line 113 of file model.h.

◆ Get() [2/2]

T Get ( std::function< T(const Model &)>  f) const
inline

Similar to Add() but this is const.

Definition at line 73 of file model.h.

◆ GetOrCreate()

T* GetOrCreate ( )
inline

Returns an object of type T that is unique to this model (like a "local" singleton).

This returns an already created instance or create a new one if needed using the T(Model* model) constructor if it exist or T() otherwise.

This works a bit like in a dependency injection framework and allows to really easily wire all the classes that make up a solver together. For instance a constraint can depends on the LiteralTrail, or the IntegerTrail or both, it can depend on a Watcher class to register itself in order to be called when needed and so on.

IMPORTANT: the Model* constructor functions shouldn't form a cycle between each other, otherwise this will crash the program.

Definition at line 92 of file model.h.

◆ Mutable()

T* Mutable ( ) const
inline

Same as Get(), but returns a mutable version of the object.

Definition at line 122 of file model.h.

◆ Register()

void Register ( T *  non_owned_class)
inline

Register a non-owned class that will be "singleton" in the model.

It is an error to call this on an already registered class.

Definition at line 155 of file model.h.

◆ TakeOwnership()

void TakeOwnership ( T *  t)
inline

Gives ownership of a pointer to this model.

It will be destroyed when the model is.

Definition at line 133 of file model.h.


The documentation for this class was generated from the following file:
Model()
Definition: model.h:40