Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
operations_research::sat::Model Class Reference

#include <model.h>

Public Member Functions

 Model ()=default
 
 ~Model ()
 
 Model (std::string name)
 
 Model (const Model &)=delete
 This type is neither copyable nor movable.
 
Modeloperator= (const Model &)=delete
 
template<typename T >
Add (std::function< T(Model *)> f)
 
template<typename T >
Get (std::function< T(const Model &)> f) const
 Similar to Add() but this is const.
 
template<typename T >
T * GetOrCreate ()
 
template<typename T >
const T * Get () const
 
template<typename T >
T * Mutable () const
 
template<typename T >
T * TakeOwnership (T *t)
 
template<typename T >
T * Create ()
 
template<typename T >
void Register (T *non_owned_class)
 
const std::string & Name () const
 

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 mechanism to wire them together.

Definition at line 42 of file model.h.

Constructor & Destructor Documentation

◆ Model() [1/3]

operations_research::sat::Model::Model ( )
default

◆ ~Model()

operations_research::sat::Model::~Model ( )
inline

The order of deletion seems to be platform dependent. We force a reverse order on the cleanup vector.

Definition at line 46 of file model.h.

◆ Model() [2/3]

operations_research::sat::Model::Model ( std::string name)
inlineexplicit

When there is more than one model in an application, it makes sense to name them for debugging or logging.

Definition at line 58 of file model.h.

◆ Model() [3/3]

operations_research::sat::Model::Model ( const Model & )
delete

This type is neither copyable nor movable.

Member Function Documentation

◆ Add()

template<typename T >
T operations_research::sat::Model::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(constraint_args, &model);
  • model.Add(ConstraintCreationFunction(constraint_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(constraint_args) {
return [=] (Model* model) {
... the same code ...
};
}
GRBmodel * model

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));
std::function< BooleanVariable(Model *)> NewBooleanVariable()
Definition integer.h:1893
std::function< IntegerVariable(Model *)> NewWeightedSum(const VectorInt &coefficients, const std::vector< IntegerVariable > &vars)

Definition at line 89 of file model.h.

◆ Create()

template<typename T >
T * operations_research::sat::Model::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 167 of file model.h.

◆ Get() [1/2]

template<typename T >
const T * operations_research::sat::Model::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 135 of file model.h.

◆ Get() [2/2]

template<typename T >
T operations_research::sat::Model::Get ( std::function< T(const Model &)> f) const
inline

Similar to Add() but this is const.

Definition at line 95 of file model.h.

◆ GetOrCreate()

template<typename T >
T * operations_research::sat::Model::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.

New element.

Todo
(user): directly store std::unique_ptr<> in singletons_?

Definition at line 114 of file model.h.

◆ Mutable()

template<typename T >
T * operations_research::sat::Model::Mutable ( ) const
inline

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

Definition at line 145 of file model.h.

◆ Name()

const std::string & operations_research::sat::Model::Name ( ) const
inline

Definition at line 185 of file model.h.

◆ operator=()

Model & operations_research::sat::Model::operator= ( const Model & )
delete

◆ Register()

template<typename T >
void operations_research::sat::Model::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 179 of file model.h.

◆ TakeOwnership()

template<typename T >
T * operations_research::sat::Model::TakeOwnership ( T * t)
inline

Gives ownership of a pointer to this model.

It will be destroyed when the model is.

Definition at line 156 of file model.h.


The documentation for this class was generated from the following file: