VarBool Help

48 views
Skip to first unread message

Jaggy

unread,
Nov 16, 2011, 11:45:21 AM11/16/11
to jsr...@googlegroups.com
Hi,
I understand that “VarBool” method of a “Constraint” interface, should return true if the Constraint is satisfied or false if Constraint is violated. But I don’t see how I can implement that in a program. For example take a look at the problem below.

Problem p = ProblemFactory.newProblem("ifthen");

Var a = p.variable("a",0, 10);

Var b = p.variable("b",0, 10);

 

Constraint c1 = p.linear(a,"=",3);

Var aBool = c1.asBool();

Constraint c2 = p.linear(b,"=",7);

Var bBool = c2.asBool();

c1.implies(c2).post();

c1.post();

 

p.add(aBool);

p.add(bBool);

             

Solver solver = p.getSolver();

Solution solution = solver.findSolution();

assertNotNull(solution);

assertEquals(solution.getValue("abool"),true);

assertEquals(solution.getValue("a"),3);

assertEquals(solution.getValue("b"),7);

solution.log();


The above code is simply to assign a=3 and b=7. VarBools, "aBool" and "bBool" is to check whether the constraint was satisfied or not. I was expecting "aBool" to say either true or false. But instead I got an unassigned value exception. Could someone help me please to solve this problem. Thank you.

JacobFeldman

unread,
Nov 16, 2011, 11:58:41 AM11/16/11
to jsr...@googlegroups.com
It seems in your example you simply did not specify the names of your boolean variables.
I've just added the following example to the JSR-331 TCK samples:

package org.jcp.jsr331.samples;

import javax.constraints.*;

public class TestIfThen {

public static void main(String[] args) {
Problem p = ProblemFactory.newProblem("ifthen");
Var a = p.variable("a",0, 10);
Var b = p.variable("b",0, 10);
 
Constraint c1 = p.linear(a,"=",3);
Var aBool = c1.asBool();
Constraint c2 = p.linear(b,"=",7);
Var bBool = c2.asBool();
c1.implies(c2).post();
 
p.add("aBool",aBool);
p.add("bBool",bBool);
            
Solver solver = p.getSolver();
Solution solution = solver.findSolution();
solution.log();
}
}

It produced the following results:

Solution #1:
a[3] b[7] aBool[1] bBool[1]

It seems this is exactly what you may expect.

Jaggy

unread,
Nov 16, 2011, 1:55:06 PM11/16/11
to jsr...@googlegroups.com
Thank you Jacob. I should have thought of that. I got confused with the snippet suggestions for "add" - 

add(VarBool)

and 

add(String, Var)

Thank you anyway for your help

-Prasanna

Jaggy

unread,
Nov 16, 2011, 2:42:20 PM11/16/11
to jsr...@googlegroups.com
If I violate the condition I was expecting the "VarBool" to say false. For example

Var a = p.variable("a",0, 10);
Constraint c1 = p.linear(a,"=",12);
Var aBool = c1.asBool();

I was expecting "aBool" to say false or "aBool[0]". Instead c1.post() throws a "Linear" exception. I must be doing something wrong. Could you please tell me? Thanks

-Prasanna

JacobFeldman

unread,
Nov 16, 2011, 3:04:13 PM11/16/11
to jsr...@googlegroups.com
The statement

Constraint c1 = p.linear(a,"=",12);

creates a constraint without posting. To catch posting errors you may write something like:

try {
   c1.post();
}
catch(Exception e) {
    System.out.prinltln("Error posting c1");
}

You may see how to control constraint violations with boolean variables in the standard example MapColoringWithViolations - the source code is attached.
MapColoringWithViolations.java

Jaggy

unread,
Nov 16, 2011, 4:16:21 PM11/16/11
to jsr...@googlegroups.com
Thanks Jacob, that is what I had in my mind too :)
-Prasanna
Reply all
Reply to author
Forward
0 new messages