Defining Optimization Problem

Create Your Own Solver Based on JavaSolver

First you need to create your own Java class  inherited from JavaSolver similar 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 1,000,000, you may write:

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

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 equals amountVar plus 5, you may write:

Var plus5 =amountVar.plus(5);

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

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

Creating Arrays, Sums, and Scalar Products

Let’s say we want to create variables that represent working hour during 7 days of a week (0-Mon, 1-Tue,…, 6-Sun). We 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);

We 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 less or equal to 50000, you may post this constraint:

csp.post(amountVar, “<=”, 50000);

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, we may post the constraint:

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

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

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

To state that a person should earn at least $1,280, we 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 doesNotWorksOnSun = csp.linear(dailyHours[6], “=”, 0);
csp.postIfThen(worksOnSat, doesNotWorksOnSun);

You also may post predefined global constraints such as “AllDifferent”:

csp.postAllDifferent(dailyHours);

It will force a person to work different hours every day of the week.

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.

Solving Optimization Problem

In the Introductory Example after we used the following main method to define the ProblemZoo and to find an optimal solution that minimizes its objective:

Minimize/Maximize Objective

You can call the standard JavaSolver’s methods minimize() or maximize() to find an optimal solution of already defined optimization problem.