Simple Arithmetic Problem

There are 3 integer variables X, Y, and Z with possible values from 1 to 10 that satisfy conditions: X < Y and X + Y = Z. Find a solution that minimizes/maximizes the objective 3XY – 4Z. Find all possible solutions.

The class XYZ extends JavaSolver and defines this problem in the following method:

First it creates 3  constrained integer variables x, y, and z with possible values from 1 to 10. Then it posts two required constraints which look intuitive.  Then it defines the objective 3XY – 4Z.

The main method of the class XYZ calls the methods define() and then standard JavaSolver’s methods minimize(), maximize(), and solveAll():

Here are the execution results for minimize():

 

Here are the execution results for maximize():

Here is the list of all possible solutions produced by solveAll():

We may manually control the solution search by adding these optional lines:

problem.setValueSelector(ValueSelectorType.MAX); // will select possible values starting with the maximal one

problem.setMaxNumberOfSolutions(10); // will consider no more than 10 solutions