DragonBreath
An Optimization Engine based on Constraint Programming and Local Search

[HOMEPAGE] -> [Documentation] -> [Interfaces to the Engine] -> [Java] -> [Costs] -> [Cost Functions]


Cost Functions

To use cost functions, at least the following classes must be imported:

import com.ai_center.dragonbreath.GlobalSearchControl;
import com.ai_center.dragonbreath.costs.CostFunction;

Creation

If a new cost function is created, its priority is set as higher than the cost functions created before (with the exception that the cost function for structural constraints always has the very highest priority).

New cost functions are created for a specific GlobalSearchControl object and are given a user-defined name. The creation is done by the following member function of the GlobalSearchControl object:

CostFunction createCostFunction(String name);

The currently selected cost mapping is taken as the function's default cost mapping.

There are multiple options how a cost function can select one of its constraints for improvement. This selection scheme can be set by a cost function's following function:

void setConstraintSelectionMethod(int selectionMethod);

The selectionMethod represents one of the available selection schemes:

The default selection scheme is the selection of the constraint with the highest costs. The maximization cost function is an exception; its default is set to the random scheme.

Here is an example, using a GlobalSearchControl object gsc:

CostFunction costFunction = gsc.createCostFunction("My cost function");
costFunction.setConstraintSelectionMethod(CostFunction.SELECTION_RANDOM);

Information Retrieval

A cost function's name and more detailed description can be accessed by the CostFunction object's following methods:

String getName();
String toString();

A cost function's (objective) cost value can be retrieved by the CostFunction object's following method:

BigInteger getCosts();

The more detailed descriptions of all cost functions of a GlobalSearchControl object (in the order of increasing priority) can be retrieved by its following method:

StringBuffer getCostFunctionInfo();

Example:

GlobalSearchControl gsc = new GlobalSearchControl(0);
CostMapping costMapping = 
  gsc.createCostMapping(Registry.FACTOR_BINARY_COST_MAPPING, "My cost mapping");

gsc.selectCostMapping(costMapping);
CostFunction costFunction1 = gsc.createCostFunction("My first cost function");
System.out.println(costFunction1.getName());
System.out.println(costFunction1.getCosts());

gsc.selectDefaultCostMapping();
CostFunction costFunction2 = gsc.createCostFunction("My second cost function");
System.out.println(costFunction2);

System.out.print("\n" + gsc.getCostFunctionInfo());

The execution results in (remember that FactorCostMapping is the default for the GlobalSearchControl's initialization):

My first cost function
0
costs[My second cost function]<Factorized Cost Mapping> = 0

costs[Maximization Function]<Factorized Cost Mapping> = 0
costs[My first cost function]<My cost mapping> = 0
costs[My second cost function]<Factorized Cost Mapping> = 0
costs[Structural Costs]<Factorized Cost Mapping> = 0

A listing of all cost entries (including objective costs, subjective costs, constraint, constraint's cost ID and cost mapping name for each entry) involved a cost function can be retrieved by the CostFunction object's following method:

String costListToString();

Selection

Similar to cost mappings, a cost function needs to be selected for constraint creation. This is done by the following member function of the GlobalSearchControl object:

void selectCostFunction(CostFunction costFunction);

Engine Developers Only: If this function is to be called from inside the engine, you should use the following version for speed optimization:

void selectCostFunction_internal(CostFunction costFunction);

WARNING: No consistency checks will be applied in this version if Registry.SAFE_MODE is set to false!

When a cost function is created, the selected cost mapping at this time gets the function's default cost mapping.

In contrast to cost mappings, a default cost function is not created at the time of initialization of the GlobalSearchControl object. The default cost function is only created if a constraint is created without the selectCostFunction being called before. The default cost function can be retrieved by a GlobalSearchControl object's following method (returning null if not yet created):

CostFunction getDefaultCostFunction();

Here is an example:

GlobalSearchControl gsc = new GlobalSearchControl(0);

/* Defining a new cost function */

CostFunction costFunction = gsc.createCostFunction("My cost function");

/* Defining variables */

Variable varA = gsc.createVariable("a");
Variable varB = gsc.createVariable("b");
gsc.setVariable(varA, 101);
gsc.setVariable(varB, 1);

/* Defining constraints */

Embedding embedding = new Embedding(2);
gsc.extendEmbedding(embedding, varA, Registry.LESS_EDGE, Registry.INCOMING);
gsc.extendEmbedding(embedding, varB, Registry.MORE_EDGE, Registry.OUTGOING);
System.out.println(gsc.getDefaultCostFunction());
ConventionalConstraint leConstraint = 
  gsc.createConstraint(Registry.LESSEQUAL_CONSTRAINT, embedding, new BigDecimal("2"));
System.out.println(gsc.getDefaultCostFunction() + "\n");

gsc.selectCostFunction(costFunction);

embedding = new Embedding(2);
gsc.extendEmbedding(embedding, varA, Registry.ADD_EDGE, Registry.INCOMING);
gsc.extendEmbedding(embedding, varB, Registry.SUM_EDGE, Registry.OUTGOING);
ConventionalConstraint sumConstraint = 
  gsc.createConstraint(Registry.SUM_CONSTRAINT, embedding);

System.out.print(gsc.getCostFunctionInfo());

The execution results in:

null
costs[Default Cost Function]<Factorized Cost Mapping> = 200

costs[Maximization Function]<Factorized Cost Mapping> = 0
costs[My cost function]<Factorized Cost Mapping> = 100
costs[Default Cost Function]<Factorized Cost Mapping> = 200
costs[Structural Costs]<Factorized Cost Mapping> = 0

(The Sum and the LessEqual constraint both producing subjective costs of 100.)


[HOMEPAGE] -> [Documentation] -> [Interfaces to the Engine] -> [Java] -> [Costs] -> [Cost Functions]


For questions, comments or suggestions, please visit our feedback forum.

Last update:
August 23, 2002 by Alexander Nareyek