From: Guillaume R. <ro...@gm...> - 2007-06-05 08:10:46
|
Hi, Well, this is not really an available feature right now. But you can simulate it easily. For instance, you can do a fake constraint which only watch events on all variables. For instance you can use the following code. Hope it helps, Guillaume =============== package mestests; import choco.integer.IntDomainVar; import choco.integer.constraints.AbstractLargeIntConstraint; import choco.ContradictionException; import choco.Problem; /** * Created by IntelliJ IDEA. * User: grochart * Date: 5 juin 2007 * Time: 09:58:16 * To change this template use File | Settings | File Templates. */ public class TestVarListener { static class VarListener extends AbstractLargeIntConstraint { public VarListener(IntDomainVar[] vars) { super(vars); } public void propagate() throws ContradictionException { // Nothing is done ! } public void awakeOnInf(int idx) throws ContradictionException { System.out.println("Lower bound of " + getIntVar(idx) + " was increased !"); } public void awakeOnSup(int idx) throws ContradictionException { System.out.println("Upper bound of " + getIntVar(idx) + " was decreased !"); } public void awakeOnInst(int idx) throws ContradictionException { System.out.println(getIntVar(idx) + " was instantiated !"); } public void awakeOnRem(int idx, int x) throws ContradictionException { System.out.println("Value " + x + " of " + getIntVar(idx) + " was removed !"); } public boolean isSatisfied() { return true; } } public static final int NB_REINES = 8; public static void main(String[] args) { long time = System.currentTimeMillis(); System.out.println("NReine model 2"); // ******* 0. Problem creation ********** Problem pb = new Problem(); // ******* 1. Variables creations ********* IntDomainVar[] vars = createVariables(pb); // ******* 2. Constraints posting ********* postConstraints(pb, vars); // ******* 3. Heuristic ********* setHeuristic(pb); // ******* 4. Solving ********* pb.solve(); // ******* 5. Displaying solution ********* displayResult(pb, vars); System.out.println("Time ellapsed: " + (System.currentTimeMillis() - time) + "ms."); } private static IntDomainVar[] createVariables(Problem pb) { IntDomainVar[] vars = new IntDomainVar[NB_REINES]; for (int i = 0; i < NB_REINES; i++) { vars[i] = pb.makeEnumIntVar("x" + i, 0, NB_REINES - 1); } return vars; } private static void postConstraints(Problem pb, IntDomainVar[] vars) { postConstraints1(pb, vars); postConstraints2(pb, vars); pb.post(new VarListener(vars)); } private static void postConstraints1(Problem pb, IntDomainVar[] vars) { for(int i = 0; i < NB_REINES; i++) { for(int j = i+1; j < NB_REINES; j++) { pb.post(pb.neq(vars[i], vars[j])); } } } private static void postConstraints2(Problem pb, IntDomainVar[] vars) { for (int i = 0; i < NB_REINES; i++) { for (int j = i + 1; j < NB_REINES; j++) { int k = j - i; pb.post(pb.neq(vars[i], pb.plus(vars[j], k))); pb.post(pb.neq(vars[i], pb.minus(vars[j], k))); } } } private static void setHeuristic(Problem pb) { //pb.getSolver().setValIterator(new DecreasingDomain()); } private static void displayResult(Problem pb, IntDomainVar[] vars) { if (pb.getSolver().getNbSolutions() > 0) { System.out.println("Found solution : "); for (int i = 0; i < NB_REINES; i++) { int val = vars[i].getVal(); for (int j = 0; j < NB_REINES; j++) { System.out.print(val == j ? "R " : ". "); } System.out.println(""); } } else { System.out.println("No solution found !!"); } } } =============== On 6/5/07, Malcolm Ryan <mal...@cs...> wrote: > Hi, > > I've been doing some constraint programming in ECLiPSe prolog, but > the language is beginning to really annoy me and I've been thinking > about migrating to a different platform. Choco looks like a > possibility, but I need to make sure it can do the things I need. > Eclipse has an event model whereby you can set an action to occur > when a particular event occurs on a variable. Defined events include > an event whenever the domain is constrained and an event when the > variable is instantiated. Is there an equivalent functionality in Choco? > > Malcolm > > -- > Many clever men like you have trusted to civilisation. > Many clever Babylonians, many clever Egyptians, > Many clever men at the end of Rome. > - G.K.Chesterton, The Napoleon of > Notting Hill > > > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 express and take > control of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Choco-users mailing list > Cho...@li... > https://lists.sourceforge.net/lists/listinfo/choco-users > |