-
Notifications
You must be signed in to change notification settings - Fork 72
Custom Constraints
A new project automatically includes its own module that is loaded into the EUROPA engine and stub C++ files to hold custom code (see makeproject). Therefore, extending EUROPA components simply requires C++ code be added to those existing files. Consider the Example project here, which can be downloaded with:
svn co http://europa-pso.googlecode.com/svn/benchmarks/tags/EUROPA-2.2/ExampleCustomConstraint ExampleCustomConstraint
We have added an 'ExampleConstraint' that restricts a variable to have integer bounds. To create and use the constraint involved these steps:
- Declare and define the constraint in ExampleCustomConstraintCustomCode.hh and ExampleCustomConstraintCustomCode.cc.
- Register the constraint. In ModuleExampleCustomConstraint.cc:
CESchema* schema = (CESchema*)engine->getComponent("CESchema");
REGISTER_CONSTRAINT(schema, ExampleConstraint, "example", "Default");
- Use the constraint. In ExampleCustomConstraint-initial-state.nddl:
float x = [3.5 0.5];
example(x); // will be constrained to [1 3]
The result is that the global variable x
will have range [1 3]
once EUROPA has been run. This range is output in the 'RUN_ExampleCustomConstraint-planner_g_rt.ExampleCustomConstraint-initial-state.xml.PlannerConfig.xml.output' log file; notice these lines in ExampleCustomConstraint-Main.cc:
PSVariable* v = engine->getVariableByName("x");
std::cout << "x lower bound: " << v->getLowerBound() << std::endl;
std::cout << "x upper bound: " << v->getUpperBound() << std::endl;
For many other examples of how to write EUROPA constraints, see Constraints.cc.