Defining Optimization Problem

Create Your Own Solver Based on JavaSolver

To define your optimization problem, you need to create a Java subclass  of the standard class JavaSolver similarly to the ProblemZoo in the Introductory Example.  Then you will write its method “define()” using the constraint satisfaction problem csp already created by JavaSolver. This object has the standard type javax.constraints.Problem and you can use its methods as described in the JSR-331 User Manual but you don’t have to read it as we will explain the most frequently used methods by the examples below.

Creating Constrained Variables

To create a constrained integer variable with the name “Amount” and possible values between 0 and 1000, you may write:

Var amountVar = csp.variable(“Amount”, 0, 1000);

To create a constrained variable with the name “Assigned” and possible values between 0 or 1, you may write:

VarBool assignVar = csp.variableBool(“Assigned”);

Creating Constrained Expressions

To create a constrained variable equal to (amountVar plus 50) , you may write:

Var plus5 =amountVar.plus(50);

If you have 3 variables X, Y, and Z already defined, you may write the expression XY-Z as follows:

Var objective = x.multiply(y).minus(z);

Creating Arrays, Sums, and Scalar Products

Let’s say you want to create variables that represent working hour during 7 days of a week (0-Mon, 1-Tue,…, 6-Sun). You may create an array with the name “DailyHours” that contains 7 constrained integer variables with possible values between 0 and 8:

Var[] dailyHours = csp.variableArray(“DailyHours”, 0, 8, 7);

To create a sum of all variables in this array, you may write:

Var totalNumberOfWorkHours = csp.sum(dailyHours);

You may define the hourly costs for every day of the week:

int[] costs = new int[] { 40, 40, 40, 40, 40, 70, 70 };

Then a person’s weekly earning can be expressed as the following scalar product:

Var weeklyEarning = csp.scalProd(costs, dailyHours);

Posting Arithmetic and Logical Constraints

To state that our amountVar should be more or equal to 500, you may post this constraint:

csp.post(amountVar, “>=”, 500);

To state that X + Y = Z, you may post this constraint:

csp.post(x.plus(y), “=”, z); 

To state that a person cannot work more than 40 hours per week, you may post the constraint:

csp.post(totalNumberOfWorkHours, “<=”, 40);

To state that a person cannot work on Sunday (day# 6), you may post the constraint:

csp.post(dailyHours[6], “=”, 0);

To state that a person should earn at least $1,280, you may post the constraint:

csp.post(weeklyEarning, “>=”, 1280);

To state that if a person works on Saturday she cannot work on Sunday, you may write:

Constraint worksOnSat = csp.linear(dailyHours[5], “>”, 0);
Constraint doesNotWorkOnSun = csp.linear(dailyHours[6], “=”, 0);
csp.postIfThen(worksOnSat, doesNotWorkOnSun);

To state that all integer variables of the array variables must take different values from each other, you may write: 

csp.postAllDifferent(variables);

You may find more complex predefined constraints in the JSR-331 User Manual.

Defining Optimization Objective

You may make any constrained variable to be your optimization objective, e.g,

setObjective(weeklyEarning);

will make your objective that you want to maximize or minimize – see Solving Optimization Problem.