-1

I want to use Choco solver to write a Java program that generates math problems that satisfy a number of constraints. The problems have to take the following form:

x @ y ∆ z = r

Where:

  • x, y & z are positive integers, not necessarily different from one another, with one of them being 2 digits long and the other two 1 digit long

  • @ & are operators +, - or * (note that both can also stand for the same operator)

  • r is a positive 1-digit integer

I want to generate these math problems "on demand" (as in, one at the time) and they need to be randomised (i.e. there no should be no pattern or fixed order across the generated problems).

It has been well over 10 years since I have done constraint (satisfaction) programming, but I believe C(S)P (preferably by means of Choco), is a suitable tool to apply here. Is this correct and can someone help me get started?

Matthias
  • 569
  • 1
  • 9
  • 30
  • What is your question? – azurefrog Nov 15 '17 at 21:10
  • @azurefrog Whether this is possible and how to go about it (i.e. some code to help me get started). – Matthias Nov 15 '17 at 21:12
  • You need to be more specific. How do the existing javadoc, user guide and tutorial examples not provide what you need? We don't know what you've tried and what isn't working if you don't tell us. – azurefrog Nov 15 '17 at 21:14
  • To be honest I have only just started reading the documentation. But the terminology is holding me back. So example code would help. – Matthias Nov 15 '17 at 21:16
  • I'll edit my post with the code of my initial attempt tomorrow. – Matthias Nov 15 '17 at 21:30

2 Answers2

5

You certainly can model such a problem with a CP solver like choco. You need to have a look at the documentation and tutorials for more details, but you will need :

  • to create a Model,
  • to declare integer variables (IntVar) for x, y and z,
  • to link them with constraints, I would suggest to use IntVar expression API to ease declaration. Such API allows to build arithmetical, logical and relational expressions like.

Here is a simple example:

Model model = new Model();
IntVar x = model.intVar(0, 9);
IntVar y = model.intVar(0, 9);
IntVar z = model.intVar(0, 9);
int r = 10;
x.add(y).sub(z).eq(r).post(); 
model.getSolver().showSolutions(
    () -> String.format("%d + %d - %d = %d",
            x.getValue(), y.getValue(), z.getValue(), r));
model.getSolver().findAllSolutions();

The way add,sub and mul are combined depends on your random selector. Then, you can try to encode the expression with a table constraints or let the solver choose.

cprudhom
  • 111
  • 4
1

Everything is in the documentation and official tutorials (http://choco-tuto.readthedocs.io/en/latest/). If you feel this is too complex, look at this ultra simple example (https://www.cosling.com/choco-solver/hello-world). It shows how to make a+b<8, from that you can do: x@y=A A delta z =r (through arithm and/or times constraint)