### Verify Choco Solver Installation with Java Code Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/003.installation.rst A basic Java code example to confirm that Choco Solver is correctly configured in your project. This code creates a simple model and prints its name to the console. ```java public static void main(String[] args) { Model model = new Model("A first model"); System.out.println(model.getName()); } ``` -------------------------------- ### Run Choco-solver Sample Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/README.md Example of how to run a specific Choco-solver sample class with provided program arguments. This demonstrates executing a constraint satisfaction problem solver. ```java org.chocosolver.samples.integer.Nonogram -f -d rabbit ``` -------------------------------- ### Aircraft Landing Problem Model Setup in Choco Solver Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/503.code.rst This snippet sets up the Choco Solver model for the aircraft landing problem. It defines variables for planes, earliness, tardiness, and total deviation, and posts constraints for landing times and separations. ```java // number of planes int N = 10; // Times per plane: // {earliest landing time, target landing time, latest landing time} int[][] LT = { {129, 155, 559}, {195, 258, 744}, {89, 98, 510}, {96, 106, 521}, {110, 123, 555}, {120, 135, 576}, {124, 138, 577}, {126, 140, 573}, {135, 150, 591}, {160, 180, 657}}; // penalty cost penalty cost per unit of time per plane: // {for landing before target, after target} int[][] PC = { {10, 10}, {10, 10}, {30, 30}, {30, 30}, {30, 30}, {30, 30}, {30, 30}, {30, 30}, {30, 30}, {30, 30}}; // Separation time required after i lands before j can land int[][] ST = { {99999, 3, 15, 15, 15, 15, 15, 15, 15, 15}, {3, 99999, 15, 15, 15, 15, 15, 15, 15, 15}, {15, 15, 99999, 8, 8, 8, 8, 8, 8, 8}, {15, 15, 8, 99999, 8, 8, 8, 8, 8, 8}, {15, 15, 8, 8, 99999, 8, 8, 8, 8, 8}, {15, 15, 8, 8, 8, 99999, 8, 8, 8, 8}, {15, 15, 8, 8, 8, 8, 99999, 8, 8, 8}, {15, 15, 8, 8, 8, 8, 8, 999999, 8, 8}, {15, 15, 8, 8, 8, 8, 8, 8, 99999, 8}, {15, 15, 8, 8, 8, 8, 8, 8, 8, 99999}}; Model model = new Model("Aircraft landing"); // Variables declaration IntVar[] planes = IntStream .range(0, N) .mapToObj(i -> model.intVar("plane #" + i, LT[i][0], LT[i][2], false)) .toArray(IntVar[]::new); IntVar[] earliness = IntStream .range(0, N) .mapToObj(i -> model.intVar("earliness #" + i, 0, LT[i][1] - LT[i][0], false)) .toArray(IntVar[]::new); IntVar[] tardiness = IntStream .range(0, N) .mapToObj(i -> model.intVar("tardiness #" + i, 0, LT[i][2] - LT[i][1], false)) .toArray(IntVar[]::new); IntVar tot_dev = model.intVar("tot_dev", 0, IntVar.MAX_INT_BOUND); // Constraint posting // one plane per runway at a time: model.allDifferent(planes).post(); // for each plane 'i' for (int i = 0; i < N; i++) { // maintain earliness earliness[i].eq((planes[i].neg().add(LT[i][1])).max(0)).post(); // and tardiness tardiness[i].eq((planes[i].sub(LT[i][1])).max(0)).post(); // disjunctions: 'i' lands before 'j' or 'j' lands before 'i' for (int j = i + 1; j < N; j++) { Constraint iBeforej = model.arithm(planes[i], "<=", planes[j], "-", ST[i][j]); Constraint jBeforei = model.arithm(planes[j], "<=", planes[i], "-", ST[j][i]); model.addClausesBoolNot(iBeforej.reify(), jBeforei.reify()); // no need to post } } // prepare coefficients of the scalar product int[] cs = new int[N * 2]; for (int i = 0; i < N; i++) { cs[i] = PC[i][0]; cs[i + N] = PC[i][1]; } model.scalar(ArrayUtils.append(earliness, tardiness), cs, "=", tot_dev).post(); // Resolution process Solver solver = model.getSolver(); ``` -------------------------------- ### Build Project with Maven Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/README.md Command to clean, install, and skip tests for a Maven project. This is a common step to build Java projects managed by Maven. ```bash mvn clean install -DskipTests ``` -------------------------------- ### Monitor Solutions with Choco Solver Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/503.code.rst This snippet shows how to plug a solution monitor to the Choco Solver to interact with each solution found. The monitor prints the landing time and deviation for each plane. ```java solver.plugMonitor((IMonitorSolution) () -> { for (int i = 0; i < N; i++) { System.out.printf("%s lands at %d (%d)\n", planes[i].getName(), planes[i].getValue(), planes[i].getValue() - LT[i][1]); } System.out.printf("Deviation cost: %d\n", tot_dev.getValue()); }); ``` -------------------------------- ### Choco-Solver: Adding Search Hints Source: https://github.com/keithwodehous/choco-solver/blob/master/CHANGES.md This code example demonstrates how to add hints to the solver to guide the search process in finding the first solution. This feature was introduced in version 4.10.9. ```java It is now possible to declare hints to help the search finding a first solution. See `solver.addHint(var, val)`. ``` -------------------------------- ### Setting a Specific Search Strategy - Java Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/303.code.rst Configures the Choco Solver to use a specific search strategy. This example uses the inputOrderLBSearch, which selects variables in the order they are provided (S, E, N, D, M, O, R, Y) and assigns them to their current lower bound. ```java solver.setSearch( Search.inputOrderLBSearch(S, E, N, D, M, O, R, Y)); ``` -------------------------------- ### Finding and Displaying a Single Solution - Java Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/303.code.rst Demonstrates how to find a single solution to a Choco Solver model and then retrieve and print the values of specific variables. This method is useful when only one solution is needed. ```java model.getSolver().showStatistics(); if (model.getSolver().solve()) { System.out.printf("%s = %d\n", S.getName(), S.getValue()); System.out.printf("%s = %d\n", E.getName(), E.getValue()); // ... } ``` -------------------------------- ### LNS with Solution as Bootstrap Source: https://github.com/keithwodehous/choco-solver/blob/master/CHANGES.md Large Neighborhood Search (LNS) can now be initialized with a provided solution. This allows for more directed and potentially faster optimization when a good starting point is known. ```java solver.addStrategy(new RestartSmallCoefficient(initialSolution)); ``` -------------------------------- ### Configure Solution Monitor and Search Strategy Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/503.code.rst This snippet shows how to plug a custom monitor to observe solutions during the search and set up an integer variable search strategy. The strategy prioritizes variables based on their proximity to a 'closest' value determined by a map. It uses `IntStream` for mapping and `Collectors.toMap` for collection. ```java solver.plugMonitor((IMonitorSolution) () -> { for (int i = 0; i < N; i++) { System.out.printf("%s lands at %d (%d)\n", planes[i].getName(), planes[i].getValue(), planes[i].getValue() - LT[i][1]); } System.out.printf("Deviation cost: %d\n", tot_dev.getValue()); }); Map map = IntStream .range(0, N) .boxed() .collect(Collectors.toMap(i -> planes[i], i -> LT[i][1])); solver.setSearch(Search.intVarSearch( variables -> Arrays.stream(variables) .filter(v -> !v.isInstantiated()) .min((v1, v2) -> closest(v2, map) - closest(v1, map)) .orElse(null), var -> closest(var, map), DecisionOperatorFactory.makeIntEq(), planes )); ``` -------------------------------- ### Choco Solver Resolution Statistics (Initial Strategy) Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/503.code.rst This block displays the console output from the Choco solver, detailing the best solution found, deviation cost, and overall resolution statistics. It includes information on the number of solutions, total resolution time, nodes explored, backtracks, and fails. ```text plane #0 lands at 165 (10) plane #1 lands at 258 (0) plane #2 lands at 98 (0) plane #3 lands at 106 (0) plane #4 lands at 118 (-5) plane #5 lands at 134 (-1) plane #6 lands at 126 (-12) plane #7 lands at 142 (2) plane #8 lands at 150 (0) plane #9 lands at 180 (0) Deviation cost: 700 Model[Aircraft landing], 7 Solutions, Minimize tot_dev = 700, Resolution time 0,326s, 906 Nodes (2 781,1 n/s), 1756 Backtracks, 883 Fails, 0 Restarts Model[Aircraft landing], 7 Solutions, Minimize tot_dev = 700, Resolution time 12,608s, 246096 Nodes (19 519,6 n/s), 492179 Backtracks, 246083 Fails, 0 Restarts ``` -------------------------------- ### Find Optimal Solution using Choco Solver Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/503.code.rst This snippet demonstrates how to find an optimal solution for the 'tot_dev' variable using the Choco Solver. It minimizes the 'tot_dev' variable, returning the best solution found. ```java // Find a solution that minimizes 'tot_dev' Solution best = solver.findOptimalSolution(tot_dev, false); ``` -------------------------------- ### Choco Solver Resolution Statistics (Reversed Strategy) Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/503.code.rst This block shows the console output after reversing the plane selection strategy in the Choco solver. It highlights the differences in resolution statistics, including more intermediate solutions found, increased time to find the best solution, but faster optimality proof. ```text Model[Aircraft landing], 12 Solutions, Minimize tot_dev = 700, Resolution time 0,514s, 2147 Nodes (4 180,8 n/s), 4222 Backtracks, 2119 Fails, 0 Restarts Model[Aircraft landing], 12 Solutions, Minimize tot_dev = 700, Resolution time 4,505s, 71596 Nodes (15 892,4 n/s), 143169 Backtracks, 71573 Fails, 0 Restarts ``` -------------------------------- ### Choco Solver Model Creation and Constraint Posting in Java Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/503.code.rst Demonstrates how to create a Choco Solver model, declare integer variables for plane landing times, and post constraints such as allDifferent, earliness, tardiness, and disjunctions to model scheduling problems. It also shows how to define a global objective variable and post a scalar constraint. ```java Model model = new Model("Aircraft landing"); IntVar[] planes = IntStream .range(0, N) .mapToObj(i -> model.intVar("plane #" + i, LT[i][0], LT[i][2], false)) .toArray(IntVar[]::new); IntVar[] earliness = IntStream .range(0, N) .mapToObj(i -> model.intVar("earliness #" + i, 0, LT[i][1] - LT[i][0], false)) .toArray(IntVar[]::new); IntVar[] tardiness = IntStream .range(0, N) .mapToObj(i -> model.intVar("tardiness #" + i, 0, LT[i][2] - LT[i][1], false)) .toArray(IntVar[]::new); IntVar tot_dev = model.intVar("tot_dev", 0, IntVar.MAX_INT_BOUND); // Constraint posting // one plane per runway at a time: model.allDifferent(planes).post(); // for each plane 'i' for(int i = 0; i < N; i++){ // maintain earliness earliness[i].eq((planes[i].neg().add(LT[i][1])).max(0)).post(); // and tardiness tardiness[i].eq((planes[i].sub(LT[i][1])).max(0)).post(); // disjunctions: 'i' lands before 'j' or 'j' lands before 'i' for(int j = i+1; j < N; j++){ Constraint iBeforej = model.arithm(planes[i], "<=", planes[j], "-", ST[i][j]); Constraint jBeforei = model.arithm(planes[j], "<=", planes[i], "-", ST[j][i]); model.addClausesBoolNot(iBeforej.reify(), jBeforei.reify()); } } // prepare coefficients of the scalar product int[] cs = new int[N*2]; for(int i = 0 ; i < N; i++){ cs[i] = PC[i][0]; cs[i + N] = PC[i][1]; } model.scalar(ArrayUtils.append(earliness, tardiness), cs, "=", tot_dev).post(); ``` -------------------------------- ### List Issues Since Last Tag Source: https://github.com/keithwodehous/choco-solver/blob/master/scripts/RELEASE.md Retrieves a list of issues (GitHub issue numbers) from the Git log since the last tagged release. Requires Git to be installed and configured. ```bash ltag=`git describe --abbrev=0 --tags`;git log ${ltag}..master | grep "#[0-9]" ``` -------------------------------- ### List Unique Authors Since Last Tag Source: https://github.com/keithwodehous/choco-solver/blob/master/scripts/RELEASE.md Lists the unique email addresses of authors who contributed to the project since the last tagged release. Requires Git to be installed and configured. ```bash ltag=`git describe --abbrev=0 --tags`;git log --format='%aE' ${ltag}..master|sort -u ``` -------------------------------- ### Check Maven Dependency Updates Source: https://github.com/keithwodehous/choco-solver/blob/master/scripts/RELEASE.md Commands to check for available updates for project dependencies and plugins using Maven. Requires Maven to be installed and configured. ```bash $ mvn -U versions:display-dependency-updates ``` ```bash $ mvn -U versions:display-plugin-updates ``` -------------------------------- ### Mapping Planes to Target Landing Times (Java) Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/503.code.rst Illustrates how to create a mapping between integer variables representing plane landing times and their respective target landing times using Java Streams and Collectors. This map is used by the search strategy to determine the 'closest' value. ```java Map map = IntStream.range(0, N).boxed().collect(Collectors.toMap(i -> planes[i], i -> LT[i][1])); ``` -------------------------------- ### Add API for Cumulative Constraint with Variable Starts in Choco Solver Source: https://github.com/keithwodehous/choco-solver/blob/master/CHANGES.md Details the addition of an API for the `cumulative` constraint when only the start times are variables in the Choco Solver. This simplifies modeling of resource-constrained scheduling problems. ```Java Add API for `cumulative` when only starts are variable ``` -------------------------------- ### Choco Solver DFA State Transition (Java) Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/603.code.rst This Java snippet illustrates the step-by-step construction of a Deterministic Finite Automaton (DFA) within the Choco Solver. It shows how to add states and transitions, including self-loops and transitions to final states, which is crucial for defining regular constraints. ```java int ii0 = auto.addState(); auto.addTransition(from, ii0, 0); auto.addTransition(ii0, ii0, 0); // the word can end with '1' or '0' if(i == m - 1){ auto.setFinal(from, ii0); } from = ii0; } model.regular(cells, auto).post(); } ``` -------------------------------- ### Improve Initialization of CT+ and CT* in Choco Solver Source: https://github.com/keithwodehous/choco-solver/blob/master/CHANGES.md Highlights improvements made to the initialization process of CT+ and CT* (likely Constraint Trees or similar structures) in the Choco Solver, potentially leading to faster setup times. ```Java Improve initialization of CT+ and CT* ``` -------------------------------- ### Choco Solver DFA and Regular Constraint Example (Java) Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/603.code.rst This Java code demonstrates the use of the Choco Solver library to define and solve a Nonogram puzzle. It involves setting up boolean variables for cells, defining regular constraints using DFAs based on block patterns, and printing the solution. ```java // number of columns int N = 15; // number of rows int M = 15; // sequence of shaded blocks int[][][] BLOCKS = new int[][][]{{ {2}, {4, 2}, {1, 1, 4}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 2, 2, 1}, {1, 3, 1}, {2, 1}, {1, 1, 1, 2}, {2, 1, 1, 1}, {1, 2}, {1, 2, 1}, }, { {3}, {3}, {10}, {2}, {2}, {8, 2}, {2}, {1, 2, 1}, {2, 1}, {7}, {2}, {2}, {10}, {3}, {2}}}; Model model = new Model("Nonogram"); // Variables declaration BoolVar[][] cells = model.boolVarMatrix("c", N, M); // Constraint declaration // one regular per row for (int i = 0; i < M; i++) { dfa(cells[i], BLOCKS[0][i], model); } for (int j = 0; j < N; j++) { dfa(ArrayUtils.getColumn(cells, j), BLOCKS[1][j], model); } if(model.getSolver().solve()){ for (int i = 0; i < cells.length; i++) { System.out.printf("\t"); for (int j = 0; j < cells[i].length; j++) { System.out.printf(cells[i][j].getValue() == 1 ? "#" : " "); } System.out.printf("\n"); } } ``` -------------------------------- ### Build DFA Constructively in Java Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/603.code.rst This Java method builds a DFA step-by-step by adding states and transitions. It's designed for sequences of integers. The method initializes the automaton, sets the initial state, and adds transitions based on the input sequence, creating states and transitions for each element in the sequence. ```java private void dfa2(BoolVar[] cells, int[] seq, Model model) { FiniteAutomaton auto = new FiniteAutomaton(); int si = auto.addState(); auto.setInitialState(si); // declare it as initial state int i0 = auto.addState(); auto.addTransition(si, i0, 0); // first transition auto.addTransition(i0, i0, 0); // second transition int from = i0; int m = seq.length; for (int i = 0; i < m; i++) { int ii = auto.addState(); // word can start with '1' if(i == 0){ auto.addTransition(si, ii, 1); } auto.addTransition(from, ii, 1); from = ii; for(int j = 1; j < seq[i]; j++){ int jj = auto.addState(); auto.addTransition(from, jj, 1); from = jj; } } } ``` -------------------------------- ### Java: Specifying Propagation Conditions for a Propagator Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/801.constraints.rst Defines the conditions under which a propagator should be triggered. This example indicates that the propagator is interested in instantiation events and lower bound inclusion events, potentially leading to better performance by avoiding unnecessary propagations. ```java @Override public int getPropagationConditions(int vIdx) { return IntEventType.combine(IntEventType.INSTANTIATE, IntEventType.INCLOW); } ``` -------------------------------- ### Choco-Solver: Flatzinc, XCSP3, and MPS Updates Source: https://github.com/keithwodehous/choco-solver/blob/master/CHANGES.md This snippet summarizes updates related to parsers and default settings for Flatzinc, XCSP3, and MPS formats in Choco-Solver, including changes in default search, interval handling, ANSI output, grammar updates, warm starts, and ignored search annotations. ```java - Change default search of Flatzinc - Increase interval for Flatzinc unbounded intvar - Remove default ANSI in parsers - Update FZN grammar to deal with 'set of int' - Flatzinc: deal with warm_start - Add ignored search annotation warning in FGoal - Update XCSPParser + add `model.amongDec` ``` -------------------------------- ### Choco Solver Warehouse Location Model Setup in Java Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/403.code.rst Complete Java code to set up the warehouse location problem using Choco Solver. This includes defining variables, constraints, objective function, and solver configuration. ```java // load parameters // number of warehouses int W = 5; // number of stores int S = 10; // maintenance cost int C = 30; // capacity of each warehouse int[] K = new int[]{1, 4, 2, 1, 3}; // matrix of supply costs, store x warehouse int[][] P = new int[][]{ {20, 24, 11, 25, 30}, {28, 27, 82, 83, 74}, {74, 97, 71, 96, 70}, {2, 55, 73, 69, 61}, {46, 96, 59, 83, 4}, {42, 22, 29, 67, 59}, {1, 5, 73, 59, 56}, {10, 73, 13, 43, 96}, {93, 35, 63, 85, 46}, {47, 65, 55, 71, 95}}; // A new model instance Model model = new Model("WarehouseLocation"); // VARIABLES // a warehouse is either open or closed BoolVar[] open = model.boolVarArray("o", W); // which warehouse supplies a store IntVar[] supplier = model.intVarArray("supplier", S, 1, W, false); // supplying cost per store IntVar[] cost = model.intVarArray("cost", S, 1, 96, true); // Total of all costs IntVar tot_cost = model.intVar("tot_cost", 0, 99999, true); // CONSTRAINTS for (int j = 0; j < S; j++) { // a warehouse is 'open', if it supplies to a store model.element(model.intVar(1), open, supplier[j], 1).post(); // Compute 'cost' for each store model.element(cost[j], P[j], supplier[j], 1).post(); } for (int i = 0; i < W; i++) { // additional variable 'occ' is created on the fly // its domain includes the constraint on capacity IntVar occ = model.intVar("occur_" + i, 0, K[i], true); // for-loop starts at 0, warehouse index starts at 1 // => we count occurrences of (i+1) in 'supplier' model.count(i+1, supplier, occ).post(); // redundant link between 'occ' and 'open' for better propagation occ.ge(open[i]).post(); } // Prepare the constraint that maintains 'tot_cost' int[] coeffs = new int[W + S]; Arrays.fill(coeffs, 0, W, C); Arrays.fill(coeffs, W, W + S, 1); // then post it model.scalar(ArrayUtils.append(open, cost), coeffs, "=", tot_cost).post(); model.setObjective(Model.MINIMIZE, tot_cost); Solver solver = model.getSolver(); solver.setSearch(Search.intVarSearch( new VariableSelectorWithTies<>( new FirstFail(model), new Smallest()), new IntDomainMiddle(false), ArrayUtils.append(supplier, cost, open))) ); solver.showShortStatistics(); while(solver.solve()){ prettyPrint(model, open, W, supplier, S, tot_cost); } ``` -------------------------------- ### Create Integer Less Than or Equal View in Choco Solver Source: https://github.com/keithwodehous/choco-solver/blob/master/CHANGES.md Provides an example of creating a less than or equal to view for an integer variable in the Choco Solver. This is used for defining upper bound constraints. ```Java model.intLeView(x,c); ``` -------------------------------- ### Constructor and Variable Initialization for Incremental F - Java Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/801.constraints.rst Initializes backtrackable integers for 'F' and an array to store previous lower bounds of variables. This setup is crucial for incrementally updating 'F' within the choco-solver. ```java /** * The constant the sum cannot be greater than */ final int b; /** * object to store F in an incremental way. * Corresponds to a backtrackable int. */ final IStateInt F; /** * array to store variables' previous lower bound. * each cell is a backtrackable int. */ final IStateInt[] prev_lbs; /** * Constructor of the specific sum propagator : x1 + x2 + ... + xn <= b * @param x array of integer variables * @param b a constant */ public MyPropagator(IntVar[] x, int b) { super(x, PropagatorPriority.LINEAR, false); this.b = b; this.F = this.model.getEnvironment().makeInt(0); this.prev_lbs = new IStateInt[x.length]; for(int i = 0 ; i < x.length; i++){ prev_lbs[i] = this.model.getEnvironment().makeInt(0); } } ``` -------------------------------- ### Define Search Strategy for Golomb Ruler Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/703.code.rst This snippet shows how to define a search strategy for the Golomb ruler problem using Choco-Solver. It utilizes the `inputOrderLBSearch` method to guide the solver by selecting mark variables in lexicographical order and instantiating them to their lower bounds. ```java Solver solver = model.getSolver(); solver.setSearch(Search.inputOrderLBSearch(ticks)); ``` -------------------------------- ### Continuous Integration Fix in Choco Solver Source: https://github.com/keithwodehous/choco-solver/blob/master/CHANGES.md Reports a fix related to the continuous integration setup for the Choco Solver, ensuring smoother automated builds and tests. ```Java Continuous integration fixed ``` -------------------------------- ### SEND+MORE=MONEY Model (Global Interpretation) - Java Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/303.code.rst Implements the SEND+MORE=MONEY cryptarithmetic puzzle using a global interpretation with Choco Solver. It defines integer variables for each letter, posts an all-different constraint, and then uses a scalar constraint to represent the equation. The solver is configured to show statistics and solutions. ```java Model model = new Model("SEND+MORE=MONEY"); IntVar S = model.intVar("S", 1, 9, false); IntVar E = model.intVar("E", 0, 9, false); IntVar N = model.intVar("N", 0, 9, false); IntVar D = model.intVar("D", 0, 9, false); IntVar M = model.intVar("M", 1, 9, false); IntVar O = model.intVar("0", 0, 9, false); IntVar R = model.intVar("R", 0, 9, false); IntVar Y = model.intVar("Y", 0, 9, false); model.allDifferent(new IntVar[]{S, E, N, D, M, O, R, Y}).post(); IntVar[] ALL = new IntVar[]{ S, E, N, D, M, O, R, E, M, O, N, E, Y}; int[] COEFFS = new int[]{ 1000, 100, 10, 1, 1000, 100, 10, 1, -10000, -1000, -100, -10, -1}; model.scalar(ALL, COEFFS, "=", 0).post(); Solver solver = model.getSolver(); solver.showStatistics(); solver.showSolutions(); solver.findSolution(); ``` -------------------------------- ### Choco Solver Search Strategy: Variable Selection (Java) Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/503.code.rst Defines a custom search strategy for Choco Solver. This snippet focuses on the variable selection heuristic, which prioritizes variables based on their distance to a target landing time, aiming to resolve the most constrained variables first. ```java solver.setSearch(Search.intVarSearch( variables -> Arrays.stream(variables) .filter(v -> !v.isInstantiated()) .min((v1, v2) -> closest(v2, map) - closest(v1, map)) .orElse(null), var -> closest(var, map), DecisionOperator.int_eq, planes )); ``` -------------------------------- ### Alternative Disjunction Posting in Choco Solver (Java) Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/503.code.rst Presents an alternative way to post disjunction constraints in Choco Solver. Instead of using `addClausesBoolNot` with reified constraints, it shows how to directly use the `or` method to combine two constraints, which internally transforms the logical expression into a sum constraint. ```java model.or(iBeforej,jBeforei).post(); ``` -------------------------------- ### SEND+MORE=MONEY Model (Local Interpretation) - Java Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/303.code.rst Implements the SEND+MORE=MONEY puzzle using a local interpretation with Choco Solver. This approach introduces additional boolean variables for carries and defines local equations to represent the addition column by column. ```java // additional variables: the carries BoolVar[] r = model.boolVarArray(3); // declare local equations D.add(E).eq(Y.add(r[0].mul(10))).post(); r[0].add(N).add(R).eq(E.add(r[1].mul(10))).post(); r[1].add(E).add(O).eq(N.add(r[2].mul(10))).post(); r[2].add(S).add(M).eq(O.add(M.mul(10))).post(); ``` -------------------------------- ### Add Choco Solver as Maven Dependency Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/003.installation.rst This XML snippet shows how to add the Choco Solver library as a dependency in your Maven project's pom.xml file. Replace X.y.z with the desired version of Choco Solver. ```xml org.choco-solver choco-solver X.y.z ``` -------------------------------- ### New Constructors for Task Objects Source: https://github.com/keithwodehous/choco-solver/blob/master/CHANGES.md Introduces new constructors for `Task` objects, providing more flexibility in defining and initializing task variables within scheduling problems. This includes options for specifying start, duration, and end variables. ```java Task task = new Task(startVar, durationVar, endVar); ``` -------------------------------- ### Set Search Strategy with domOverWDegSearch in Java Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/203.firstexample2.rst Configures the Choco solver to use a specific search strategy, 'domOverWDegSearch', which prioritizes variables with the smallest domain and the highest weighted degree. This is a common technique for guiding the solver's exploration of the search space. ```java solver.setSearch(Search.domOverWDegSearch(vars)); ``` -------------------------------- ### Set Time Limit for Choco Solver Resolution in Java Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/403.code.rst This Java example demonstrates how to set a time limit for the Choco Solver's resolution process. A 10-second limit is applied using `solver.limitTime("10s")` before initiating the search for an optimal solution. ```java solver.limitTime("10s"); // then run the resolution Solution best = solver.findOptimalSolution(tot_cost, false); ``` -------------------------------- ### Helper Function for Closest Value in Choco Solver (Java) Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/503.code.rst Provides a helper function `closest` that finds the value in an IntVar's domain closest to a given target value. It handles cases where the target value itself is present in the domain and finds the nearest preceding or succeeding value if not. ```java private static int closest(IntVar var, Map map) { int target = map.get(var); if (var.contains(target)) { return target; } else { int p = var.previousValue(target); int n = var.nextValue(target); return Math.abs(target - p) < Math.abs(n - target) ? p : n; } } ``` -------------------------------- ### Build DFA from Regular Expression in Java Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/603.code.rst This Java method constructs a DFA using a regular expression string. It takes an array of Boolean variables representing cells, an array of integers for sequence lengths, and a Choco Model. The DFA is then posted to the model to enforce the regular language constraint. ```java private void dfa(BoolVar[] cells, int[] rest, Model model) { StringBuilder regexp = new StringBuilder("0*"); int m = rest.length; for (int i = 0; i < m; i++) { regexp.append('1').append('{').append(rest[i]).append('}'); regexp.append('0'); regexp.append(i == m - 1 ? '*' : '+'); } IAutomaton auto = new FiniteAutomaton(regexp.toString()); model.regular(cells, auto).post(); } ``` -------------------------------- ### Run Docker Image Source: https://github.com/keithwodehous/choco-solver/blob/master/parsers/src/main/minizinc/docker/README.md Runs the previously built Docker image to execute a MiniZinc model with specified data files. The command uses the image tag and includes arguments for the model and data. ```bash docker run --rm chocoteam/choco-solver-mzn: minizinc fd.mpc /minizinc/test.mzn /minizinc/2.dzn ``` -------------------------------- ### Choco Solver: Objective Function Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/702.mathmodel.rst Sets the objective for the solver to minimize the position of the last mark. This guides the solver towards finding an optimal solution. ```Choco Solver model.setObjective(Model.MINIMIZE, tick_m); ``` -------------------------------- ### Build Docker Image Source: https://github.com/keithwodehous/choco-solver/blob/master/parsers/src/main/minizinc/docker/README.md Builds a Docker image using the specified Dockerfile and tags it for the choco-solver project. Requires the path to the choco-solver repository. ```bash docker build -f ./Dockerfile.dms -t chocoteam/choco-solver-mzn: ``` -------------------------------- ### Benchmark Execution with Python Source: https://github.com/keithwodehous/choco-solver/blob/master/parsers/src/main/python/README.md This command executes the benchmark script, pointing it to the specified YAML configuration file. The script will then orchestrate the Choco-Solver evaluations based on the provided configuration. ```shell python3 benchmark.py -c configuration.yaml ``` -------------------------------- ### Build Choco-Solver from Source Source: https://github.com/keithwodehous/choco-solver/blob/master/README.md This command initiates the build process for Choco-Solver from its source code using Maven. It cleans the project, packages it, and skips running tests. ```bash mvn clean package -DskipTests ``` -------------------------------- ### Declare Choco Integer Variables (IntVar) Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/202.objects.rst Provides examples of declaring Integer Variables (IntVar) in Choco Solver. This includes creating a constant variable, a variable with a bounded domain, and a variable with an enumerated domain. ```java // A variable with a unique value in its domain, in other words, a constant IntVar two = model.intVar("TWO", 2); // Any value in [1..4] can be assigned to this variable IntVar x = model.intVar("X", 1, 4); // Only the values 1, 3 and 4 can be assigned to this variable IntVar y = model.intVar("X", new int[]{1, 3, 4}); ``` -------------------------------- ### Generate IDE Configuration with Maven Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/README.md Maven command to generate IDE-specific project files, such as for IntelliJ IDEA. This helps in setting up the development environment. ```bash mvn idea:idea ``` -------------------------------- ### Conflict History Search Source: https://github.com/keithwodehous/choco-solver/blob/master/CHANGES.md Implements Conflict History Search, a method for constraint satisfaction problems based on the research by Habet and Terrioux. This approach uses historical conflict information to guide the search process more effectively. ```java solver.setSearch(ConflictHistorySearch.lastConflict()); ``` -------------------------------- ### Basic Constraint Programming Model in Java Source: https://github.com/keithwodehous/choco-solver/blob/master/README.md This snippet demonstrates the fundamental steps of creating a constraint programming model using Choco-solver. It includes model creation, variable definition, constraint posting, solver configuration, and problem resolution. ```java Model model = new Model("my first problem"); IntVar x = model.intVar("X", 0, 5); IntVar y = model.intVar("Y", 0, 5); model.element(x, new int[]{5,0,4,1,3,2}, y).post(); x.add(y).lt(5).post(); Solver solver = model.getSolver(); solver.setSearch(Search.inputOrderLBSearch(x, y)); solver.solve(); solver.printStatistics(); ``` -------------------------------- ### Initialize and Solve Model - Java Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/202.objects.rst This snippet shows the basic process of creating a Choco model, declaring variables and constraints (commented out), obtaining the Solver, and finding a solution. ```java Model model = new Model("My problem"); // variables declaration // constraints declaration Solver solver = model.getSolver(); Solution solution = solver.findSolution(); ``` -------------------------------- ### Default Solution Creation Source: https://github.com/keithwodehous/choco-solver/blob/master/CHANGES.md Introduces `solver.defaultSolution()` which lazily creates a solution object, records everything, plugs it, and returns it. This is convenient when a Solution object is needed in multiple places. ```Java solver.defaultSolution() ``` -------------------------------- ### Complete Golomb Ruler Solution and Execution Trace Source: https://github.com/keithwodehous/choco-solver/blob/master/examples/docs/source/src/703.code.rst This snippet presents the complete Java code for solving the Golomb ruler problem with Choco-Solver, integrating model creation, constraint posting, search strategy, and optimization. It also includes a sample execution trace showing the progression of finding optimal solutions and their associated statistics. ```java int m = 10; // A new model instance Model model = new Model("Golomb ruler"); // VARIABLES // set of marks that should be put on the ruler IntVar[] ticks = ticks = model.intVarArray("a", m, 0, 999, false); // set of distances between two distinct marks IntVar[] diffs = model.intVarArray("d", (m * (m - 1)) / 2, 0, 999, false); // CONSTRAINTS // the first mark is set to 0 model.arithm(ticks[0], "=", 0).post(); for (int i = 0, k = 0 ; i < m - 1; i++) { // // the mark variables are ordered model.arithm(ticks[i + 1], ">", ticks[i]).post(); for (int j = i + 1; j < m; j++, k++) { // declare the distance constraint between two distinct marks model.scalar(new IntVar[]{ticks[j], ticks[i]}, new int[]{1, -1}, "=", diffs[k]).post(); // redundant constraints on bounds of diffs[k] model.arithm(diffs[k], ">=", (j - i) * (j - i + 1) / 2).post(); model.arithm(diffs[k], "<=", ticks[m - 1], "-", ((m - 1 - j + i) * (m - j + i)) / 2).post(); } } // all distances must be distinct model.allDifferent(diffs, "BC").post(); //symmetry-breaking constraints model.arithm(diffs[0], "<", diffs[diffs.length - 1]).post(); Solver solver = model.getSolver(); solver.setSearch(Search.inputOrderLBSearch(ticks)); // show resolution statistics solver.showShortStatistics(); // Find a solution that minimizes the last mark solver.findOptimalSolution(ticks[m - 1], false); ``` ```text Model[Golomb ruler], 1 Solutions, MINIMIZE a[9] = 80, Resolution time 0,017s, 10 Nodes (593,7 n/s), 0 Backtracks, 0 Fails, 0 Restarts Model[Golomb ruler], 2 Solutions, MINIMIZE a[9] = 75, Resolution time 0,026s, 18 Nodes (696,8 n/s), 14 Backtracks, 7 Fails, 0 Restarts Model[Golomb ruler], 3 Solutions, MINIMIZE a[9] = 73, Resolution time 0,032s, 30 Nodes (949,9 n/s), 36 Backtracks, 17 Fails, 0 Restarts Model[Golomb ruler], 4 Solutions, MINIMIZE a[9] = 72, Resolution time 0,040s, 53 Nodes (1 324,0 n/s), 80 Backtracks, 40 Fails, 0 Restarts Model[Golomb ruler], 5 Solutions, MINIMIZE a[9] = 70, Resolution time 0,054s, 95 Nodes (1 773,2 n/s), 162 Backtracks, 79 Fails, 0 Restarts Model[Golomb ruler], 6 Solutions, MINIMIZE a[9] = 68, Resolution time 0,065s, 161 Nodes (2 487,9 n/s), 292 Backtracks, 144 Fails, 0 Restarts Model[Golomb ruler], 7 Solutions, MINIMIZE a[9] = 66, Resolution time 0,082s, 288 Nodes (3 529,9 n/s), 546 Backtracks, 269 Fails, 0 Restarts Model[Golomb ruler], 8 Solutions, MINIMIZE a[9] = 62, Resolution time 0,092s, 374 Nodes (4 075,8 n/s), 712 Backtracks, 353 Fails, 0 Restarts Model[Golomb ruler], 9 Solutions, MINIMIZE a[9] = 60, Resolution time 0,210s, 1354 Nodes (6 435,1 n/s), 2670 Backtracks, 1331 Fails, 0 Restarts ``` -------------------------------- ### Execute Release Script Source: https://github.com/keithwodehous/choco-solver/blob/master/scripts/RELEASE.md Executes the release script located in the ./scripts directory. This script is assumed to handle the final release process. ```bash ./scripts/release.sh ``` -------------------------------- ### Deprecated API - IOutputFactory.outputSearchTreeToCPProfiler Source: https://github.com/keithwodehous/choco-solver/blob/master/CHANGES.md The `outputSearchTreeToCPProfiler(boolean domain)` method in `IOutputFactory` is deprecated and will be removed in the next release. Users should migrate to alternative methods if available. ```Java IOutputFactory.outputSearchTreeToCPProfiler(boolean domain) ``` -------------------------------- ### Iterate Over Solutions with Ibex in Choco Solver Source: https://github.com/keithwodehous/choco-solver/blob/master/CHANGES.md Shows how to use Ibex to iterate over solutions in the Choco Solver once all integer variables have been instantiated. This is a specific solving strategy. ```Java Search.ibexSolving(model); ```