1 // 2 // Copyright 2012 Hakan Kjellerstrand 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 using System; 17 using Google.OrTools.ConstraintSolver; 18 19 public class Xkcd 20 { 21 /** 22 * 23 * Solve the xkcd problem 24 * See http://www.hakank.org/google_or_tools/xkcd.py 25 * 26 */ Solve()27 private static void Solve() 28 { 29 Solver solver = new Solver("Xkcd"); 30 31 // 32 // Constants, inits 33 // 34 int n = 6; 35 // for price and total: multiplied by 100 to be able to use integers 36 int[] price = { 215, 275, 335, 355, 420, 580 }; 37 int total = 1505; 38 39 // 40 // Decision variables 41 // 42 IntVar[] x = solver.MakeIntVarArray(n, 0, 10, "x"); 43 44 // 45 // Constraints 46 // 47 solver.Add(x.ScalProd(price) == total); 48 49 // 50 // Search 51 // 52 DecisionBuilder db = solver.MakePhase(x, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MIN_VALUE); 53 54 solver.NewSearch(db); 55 while (solver.NextSolution()) 56 { 57 for (int i = 0; i < n; i++) 58 { 59 Console.Write(x[i].Value() + " "); 60 } 61 Console.WriteLine(); 62 } 63 64 Console.WriteLine("\nSolutions: {0}", solver.Solutions()); 65 Console.WriteLine("WallTime: {0} ms", solver.WallTime()); 66 Console.WriteLine("Failures: {0}", solver.Failures()); 67 Console.WriteLine("Branches: {0}", solver.Branches()); 68 69 solver.EndSearch(); 70 } 71 Main(String[] args)72 public static void Main(String[] args) 73 { 74 Solve(); 75 } 76 } 77