本文整理汇总了C#中Solver.MakeIntVar方法的典型用法代码示例。如果您正苦于以下问题:C# Solver.MakeIntVar方法的具体用法?C# Solver.MakeIntVar怎么用?C# Solver.MakeIntVar使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Solver
的用法示例。
在下文中一共展示了Solver.MakeIntVar方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Solve
/**
*
* Solve the Least diff problem
* For more info, see http://www.hakank.org/google_or_tools/least_diff.py
*
*/
private static void Solve()
{
Solver solver = new Solver("LeastDiff");
//
// Decision variables
//
IntVar A = solver.MakeIntVar(0, 9, "A");
IntVar B = solver.MakeIntVar(0, 9, "B");
IntVar C = solver.MakeIntVar(0, 9, "C");
IntVar D = solver.MakeIntVar(0, 9, "D");
IntVar E = solver.MakeIntVar(0, 9, "E");
IntVar F = solver.MakeIntVar(0, 9, "F");
IntVar G = solver.MakeIntVar(0, 9, "G");
IntVar H = solver.MakeIntVar(0, 9, "H");
IntVar I = solver.MakeIntVar(0, 9, "I");
IntVar J = solver.MakeIntVar(0, 9, "J");
IntVar[] all = new IntVar[] {A,B,C,D,E,F,G,H,I,J};
int[] coeffs = {10000,1000,100,10,1};
IntVar x = new IntVar[]{A,B,C,D,E}.ScalProd(coeffs).Var();
IntVar y = new IntVar[]{F,G,H,I,J}.ScalProd(coeffs).Var();
IntVar diff = (x - y).VarWithName("diff");
//
// Constraints
//
solver.Add(all.AllDifferent());
solver.Add(A > 0);
solver.Add(F > 0);
solver.Add(diff > 0);
//
// Objective
//
OptimizeVar obj = diff.Minimize(1);
//
// Search
//
DecisionBuilder db = solver.MakePhase(all,
Solver.CHOOSE_PATH,
Solver.ASSIGN_MIN_VALUE);
solver.NewSearch(db, obj);
while (solver.NextSolution()) {
Console.WriteLine("{0} - {1} = {2} ({3}",x.Value(), y.Value(), diff.Value(), diff.ToString());
}
Console.WriteLine("\nSolutions: {0}", solver.Solutions());
Console.WriteLine("WallTime: {0}ms", solver.WallTime());
Console.WriteLine("Failures: {0}", solver.Failures());
Console.WriteLine("Branches: {0} ", solver.Branches());
solver.EndSearch();
}
示例2: CPisFun
// We don't need helper functions here
// Csharp syntax is easier than C++ syntax!
private static void CPisFun (int kBase)
{
// Constraint Programming engine
Solver solver = new Solver ("CP is fun!");
// Decision variables
IntVar c = solver.MakeIntVar (1, kBase - 1, "C");
IntVar p = solver.MakeIntVar (0, kBase - 1, "P");
IntVar i = solver.MakeIntVar (1, kBase - 1, "I");
IntVar s = solver.MakeIntVar (0, kBase - 1, "S");
IntVar f = solver.MakeIntVar (1, kBase - 1, "F");
IntVar u = solver.MakeIntVar (0, kBase - 1, "U");
IntVar n = solver.MakeIntVar (0, kBase - 1, "N");
IntVar t = solver.MakeIntVar (1, kBase - 1, "T");
IntVar r = solver.MakeIntVar (0, kBase - 1, "R");
IntVar e = solver.MakeIntVar (0, kBase - 1, "E");
// We need to group variables in a vector to be able to use
// the global constraint AllDifferent
IntVar[] letters = new IntVar[] { c, p, i, s, f, u, n, t, r, e};
// Check if we have enough digits
if (kBase < letters.Length) {
throw new Exception("kBase < letters.Length");
}
// Constraints
solver.Add (letters.AllDifferent ());
// CP + IS + FUN = TRUE
solver.Add (p + s + n + kBase * (c + i + u) + kBase * kBase * f ==
e + kBase * u + kBase * kBase * r + kBase * kBase * kBase * t);
SolutionCollector all_solutions = solver.MakeAllSolutionCollector();
// Add the interesting variables to the SolutionCollector
all_solutions.Add(c);
all_solutions.Add(p);
// Create the variable kBase * c + p
IntVar v1 = solver.MakeSum(solver.MakeProd(c, kBase), p).Var();
// Add it to the SolutionCollector
all_solutions.Add(v1);
// Decision Builder: hot to scour the search tree
DecisionBuilder db = solver.MakePhase (letters,
Solver.CHOOSE_FIRST_UNBOUND,
Solver.ASSIGN_MIN_VALUE);
solver.Solve(db, all_solutions);
// Retrieve the solutions
int numberSolutions = all_solutions.SolutionCount();
Console.WriteLine ("Number of solutions: " + numberSolutions);
for (int index = 0; index < numberSolutions; ++index) {
Assignment solution = all_solutions.Solution(index);
Console.WriteLine ("Solution found:");
Console.WriteLine ("v1=" + solution.Value(v1));
}
}
示例3: Solve
/**
*
* Dudeney numbers
* From Pierre Schaus blog post
* Dudeney number
* http://cp-is-fun.blogspot.com/2010/09/test-python.html
* """
* I discovered yesterday Dudeney Numbers
* A Dudeney Numbers is a positive integer that is a perfect cube such that the sum
* of its decimal digits is equal to the cube root of the number. There are only six
* Dudeney Numbers and those are very easy to find with CP.
* I made my first experience with google cp solver so find these numbers (model below)
* and must say that I found it very convenient to build CP models in python!
* When you take a close look at the line:
* solver.Add(sum([10**(n-i-1)*x[i] for i in range(n)]) == nb)
* It is difficult to argue that it is very far from dedicated
* optimization languages!
* """
*
* Also see: http://en.wikipedia.org/wiki/Dudeney_number
*
*/
private static void Solve()
{
Solver solver = new Solver("DudeneyNumbers");
//
// data
//
int n = 6;
//
// Decision variables
//
IntVar[] x = solver.MakeIntVarArray(n, 0, 9, "x");
IntVar nb = solver.MakeIntVar(3, (int)Math.Pow(10,n), "nb");
IntVar s = solver.MakeIntVar(1,9*n+1,"s");
//
// Constraints
//
solver.Add(nb == s*s*s);
solver.Add(x.Sum() == s);
// solver.Add(ToNum(x, nb, 10));
// alternative
solver.Add((from i in Enumerable.Range(0, n)
select (x[i]*(int)Math.Pow(10,n-i-1)).Var()).
ToArray().Sum() == nb);
//
// Search
//
DecisionBuilder db = solver.MakePhase(x,
Solver.INT_VAR_DEFAULT,
Solver.INT_VALUE_DEFAULT);
solver.NewSearch(db);
while (solver.NextSolution()) {
Console.WriteLine(nb.Value());
}
Console.WriteLine("\nSolutions: {0}", solver.Solutions());
Console.WriteLine("WallTime: {0}ms", solver.WallTime());
Console.WriteLine("Failures: {0}", solver.Failures());
Console.WriteLine("Branches: {0} ", solver.Branches());
solver.EndSearch();
}
示例4: MyMod
/**
*
* A simple propagator for modulo constraint.
*
* This implementation is based on the ECLiPSe version
* mentioned in "A Modulo propagator for ECLiPSE"
* http://www.hakank.org/constraint_programming_blog/2010/05/a_modulo_propagator_for_eclips.html
* The ECLiPSe Prolog source code:
* http://www.hakank.org/eclipse/modulo_propagator.ecl
*
*/
public static void MyMod(Solver solver, IntVar x, IntVar y, IntVar r) {
long lbx = x.Min();
long ubx = x.Max();
long ubx_neg = -ubx;
long lbx_neg = -lbx;
int min_x = (int)Math.Min(lbx, ubx_neg);
int max_x = (int)Math.Max(ubx, lbx_neg);
IntVar d = solver.MakeIntVar(min_x, max_x, "d");
// r >= 0
solver.Add(r >= 0);
// x*r >= 0
solver.Add( x*r >= 0);
// -abs(y) < r
solver.Add(-y.Abs() < r);
// r < abs(y)
solver.Add(r < y.Abs());
// min_x <= d, i.e. d > min_x
solver.Add(d > min_x);
// d <= max_x
solver.Add(d <= max_x);
// x == y*d+r
solver.Add(x - (y*d + r) == 0);
}
示例5: Solve
/**
* Solves the rabbits + pheasants problem. We are seing 20 heads
* and 56 legs. How many rabbits and how many pheasants are we thus
* seeing?
*/
private static void Solve()
{
Solver solver = new Solver("RabbitsPheasants");
IntVar rabbits = solver.MakeIntVar(0, 100, "rabbits");
IntVar pheasants = solver.MakeIntVar(0, 100, "pheasants");
solver.Add(rabbits + pheasants == 20);
solver.Add(rabbits * 4 + pheasants * 2 == 56);
DecisionBuilder db =
new AssignFirstUnboundToMin(new IntVar[] {rabbits, pheasants});
solver.NewSearch(db);
solver.NextSolution();
Console.WriteLine(
"Solved Rabbits + Pheasants in {0} ms, and {1} search tree branches.",
solver.WallTime(), solver.Branches());
Console.WriteLine(rabbits.ToString());
Console.WriteLine(pheasants.ToString());
solver.EndSearch();
}
示例6: Solve
/**
*
* Solve the SEND+MORE=MONEY problem
*
*/
private static void Solve()
{
Solver solver = new Solver("SendMoreMoney");
//
// Decision variables
//
IntVar S = solver.MakeIntVar(0, 9, "S");
IntVar E = solver.MakeIntVar(0, 9, "E");
IntVar N = solver.MakeIntVar(0, 9, "N");
IntVar D = solver.MakeIntVar(0, 9, "D");
IntVar M = solver.MakeIntVar(0, 9, "M");
IntVar O = solver.MakeIntVar(0, 9, "O");
IntVar R = solver.MakeIntVar(0, 9, "R");
IntVar Y = solver.MakeIntVar(0, 9, "Y");
// for AllDifferent()
IntVar[] x = new IntVar[] {S,E,N,D,M,O,R,Y};
//
// Constraints
//
solver.Add(x.AllDifferent());
solver.Add(S*1000 + E*100 + N*10 + D + M*1000 + O*100 + R*10 + E ==
M*10000 + O*1000 + N*100 + E*10 + Y);
solver.Add(S > 0);
solver.Add(M > 0);
//
// Search
//
DecisionBuilder db = solver.MakePhase(x,
Solver.CHOOSE_FIRST_UNBOUND,
Solver.ASSIGN_MIN_VALUE);
solver.NewSearch(db);
while (solver.NextSolution()) {
for(int i = 0; i < 8; i++) {
Console.Write(x[i].ToString() + " ");
}
Console.WriteLine();
}
Console.WriteLine("\nWallTime: " + solver.WallTime() + "ms ");
Console.WriteLine("Failures: " + solver.Failures());
Console.WriteLine("Branches: " + solver.Branches());
solver.EndSearch();
}
示例7: Solve
/**
*
* Subset sum problem.
*
* From Katta G. Murty: 'Optimization Models for Decision Making', page 340
* http://ioe.engin.umich.edu/people/fac/books/murty/opti_model/junior-7.pdf
* """
* Example 7.8.1
*
* A bank van had several bags of coins, each containing either
* 16, 17, 23, 24, 39, or 40 coins. While the van was parked on the
* street, thieves stole some bags. A total of 100 coins were lost.
* It is required to find how many bags were stolen.
* """
*
* Also see http://www.hakank.org/or-tools/subset_sum.py
*
*/
private static void Solve(int[] coins, int total)
{
Solver solver = new Solver("SubsetSum");
int n = coins.Length;
Console.Write("Coins: ");
for(int i = 0; i < n; i++) {
Console.Write(coins[i] + " ");
}
Console.WriteLine("\nTotal: {0}", total);
//
// Variables
//
// number of coins
IntVar s = solver.MakeIntVar(0, coins.Sum(), "s");
//
// Constraints
//
IntVar[] x = subset_sum(solver, coins, total);
solver.Add(x.Sum() == s);
//
// Search
//
DecisionBuilder db = solver.MakePhase(x,
Solver.CHOOSE_FIRST_UNBOUND,
Solver.ASSIGN_MIN_VALUE);
solver.NewSearch(db);
while (solver.NextSolution()) {
Console.Write("x: ");
for(int i = 0; i < n; i++) {
Console.Write(x[i].Value() + " ");
}
Console.WriteLine(" s: {0}", s.Value());
}
Console.WriteLine("\nSolutions: {0}", solver.Solutions());
Console.WriteLine("WallTime: {0}ms", solver.WallTime());
Console.WriteLine("Failures: {0}", solver.Failures());
Console.WriteLine("Branches: {0} ", solver.Branches());
solver.EndSearch();
}
示例8: Solve
/**
*
* Implements toNum: channeling between a number and an array.
* See http://www.hakank.org/or-tools/toNum.py
*
*/
private static void Solve()
{
Solver solver = new Solver("ToNum");
int n = 5;
int bbase = 10;
//
// Decision variables
//
IntVar[] x = solver.MakeIntVarArray(n, 0, bbase - 1, "x");
IntVar num = solver.MakeIntVar(0, (int)Math.Pow(bbase, n) - 1, "num");
//
// Constraints
//
solver.Add(x.AllDifferent());
solver.Add(ToNum(x, num, bbase));
// extra constraint (just for fun)
// second digit should be 7
// solver.Add(x[1] == 7);
//
// Search
//
DecisionBuilder db = solver.MakePhase(x,
Solver.CHOOSE_FIRST_UNBOUND,
Solver.ASSIGN_MIN_VALUE);
solver.NewSearch(db);
while (solver.NextSolution()) {
Console.Write("\n" + num.Value() + ": ");
for(int i = 0; i < n; i++) {
Console.Write(x[i].Value() + " ");
}
}
Console.WriteLine("\nSolutions: {0}", solver.Solutions());
Console.WriteLine("WallTime: {0}ms", solver.WallTime());
Console.WriteLine("Failures: {0}", solver.Failures());
Console.WriteLine("Branches: {0} ", solver.Branches());
solver.EndSearch();
}
示例9: ConstructorsTest
static void ConstructorsTest()
{
Console.WriteLine("TestConstructors");
Solver solver = new Solver("test");
IntVar x = solver.MakeIntVar(0, 10, "x");
Constraint c1 = x == 2;
Console.WriteLine(c1.ToString());
Constraint c2 = x >= 2;
Console.WriteLine(c2.ToString());
Constraint c3 = x > 2;
Console.WriteLine(c3.ToString());
Constraint c4 = x <= 2;
Console.WriteLine(c4.ToString());
Constraint c5 = x < 2;
Console.WriteLine(c5.ToString());
Constraint c6 = x != 2;
Console.WriteLine(c6.ToString());
}
示例10: Solve
/**
*
* Set covering.
*
* Example from Steven Skiena, The Stony Brook Algorithm Repository
* http://www.cs.sunysb.edu/~algorith/files/set-cover.shtml
* """
* Input Description: A set of subsets S_1, ..., S_m of the
* universal set U = {1,...,n}.
*
* Problem: What is the smallest subset of subsets T subset S such
* that \cup_{t_i in T} t_i = U?
* """
* Data is from the pictures INPUT/OUTPUT.
*
*
* Also see http://www.hakank.org/or-tools/set_covering_skiena.py
*
*/
private static void Solve()
{
Solver solver = new Solver("SetCoveringSkiena");
int num_sets = 7;
int num_elements = 12;
IEnumerable<int> Sets = Enumerable.Range(0, num_sets);
IEnumerable<int> Elements = Enumerable.Range(0, num_elements);
// Which element belongs to which set
int[,] belongs =
{
// 1 2 3 4 5 6 7 8 9 0 1 2 elements
{1,1,0,0,0,0,0,0,0,0,0,0}, // Set 1
{0,1,0,0,0,0,0,1,0,0,0,0}, // 2
{0,0,0,0,1,1,0,0,0,0,0,0}, // 3
{0,0,0,0,0,1,1,0,0,1,1,0}, // 4
{0,0,0,0,0,0,0,0,1,1,0,0}, // 5
{1,1,1,0,1,0,0,0,1,1,1,0}, // 6
{0,0,1,1,0,0,1,1,0,0,1,1} // 7
};
//
// Decision variables
//
IntVar[] x = solver.MakeIntVarArray(num_sets, 0, 1, "x");
IntVar z = x.Sum().VarWithName("z");
// total number of elements in the choosen sets
IntVar tot_elements = solver.MakeIntVar(0, num_sets*num_elements, "tot_elements");
//
// Constraints
//
// all sets must be used
foreach(int j in Elements) {
solver.Add( (from i in Sets select belongs[i,j] * x[i])
.ToArray().Sum() >= 1);
}
// number of used elements
solver.Add((from i in Sets from j in Elements select x[i] * belongs[i,j])
.ToArray().Sum() == tot_elements);
//
// Objective
//
OptimizeVar obj = z.Minimize(1);
//
// Search
//
DecisionBuilder db = solver.MakePhase(x,
Solver.INT_VAR_DEFAULT,
Solver.INT_VALUE_DEFAULT);
solver.NewSearch(db, obj);
while (solver.NextSolution()) {
Console.WriteLine("z: {0}", z.Value());
Console.WriteLine("tot_elements: {0}", tot_elements.Value());
Console.WriteLine(
"x: {0}",
String.Join(" ", (from i in Enumerable.Range(0, num_sets)
select x[i].Value().ToString()).ToArray()));
}
Console.WriteLine("\nSolutions: {0}", solver.Solutions());
Console.WriteLine("WallTime: {0}ms", solver.WallTime());
Console.WriteLine("Failures: {0}", solver.Failures());
Console.WriteLine("Branches: {0} ", solver.Branches());
solver.EndSearch();
}
示例11: Solve
/**
*
* Solve the SEND+MORE=MONEY problem
* using scalar product.
*
*/
private static void Solve()
{
Solver solver = new Solver("SendMoreMoney");
//
// Decision variables
//
IntVar S = solver.MakeIntVar(0, 9, "S");
IntVar E = solver.MakeIntVar(0, 9, "E");
IntVar N = solver.MakeIntVar(0, 9, "N");
IntVar D = solver.MakeIntVar(0, 9, "D");
IntVar M = solver.MakeIntVar(0, 9, "M");
IntVar O = solver.MakeIntVar(0, 9, "O");
IntVar R = solver.MakeIntVar(0, 9, "R");
IntVar Y = solver.MakeIntVar(0, 9, "Y");
// for AllDifferent()
IntVar[] x = new IntVar[] {S,E,N,D,M,O,R,Y};
//
// Constraints
//
solver.Add(x.AllDifferent());
/*
solver.Add(S*1000 + E*100 + N*10 + D + M*1000 + O*100 + R*10 + E ==
M*10000 + O*1000 + N*100 + E*10 + Y);
*/
// Here we use scalar product instead.
int[] s1 = new int[] {1000,100,10,1};
int[] s2 = new int[] {10000,1000,100,10,1};
solver.Add(new IntVar[] {S,E,N,D}.ScalProd(s1) +
new IntVar[] {M,O,R,E}.ScalProd(s1) ==
new IntVar[] {M,O,N,E,Y}.ScalProd(s2));
solver.Add(S > 0);
solver.Add(M > 0);
//
// Search
//
DecisionBuilder db = solver.MakePhase(x,
Solver.CHOOSE_FIRST_UNBOUND,
Solver.ASSIGN_MIN_VALUE);
solver.NewSearch(db);
while (solver.NextSolution()) {
for(int i = 0; i < 8; i++) {
Console.Write(x[i].Value() + " ");
}
Console.WriteLine();
}
Console.WriteLine("\nSolutions: {0}", solver.Solutions());
Console.WriteLine("WallTime: {0}ms", solver.WallTime());
Console.WriteLine("Failures: {0}", solver.Failures());
Console.WriteLine("Branches: {0}", solver.Branches());
solver.EndSearch();
}
示例12: Solve
/**
*
* Solves the Zebra problem.
*
* This is a port of the or-tools/Python model zebra.py
*
*
* """
* This is the zebra problem as invented by Lewis Caroll.
*
* There are five houses.
* The Englishman lives in the red house.
* The Spaniard owns the dog.
* Coffee is drunk in the green house.
* The Ukrainian drinks tea.
* The green house is immediately to the right of the ivory house.
* The Old Gold smoker owns snails.
* Kools are smoked in the yellow house.
* Milk is drunk in the middle house.
* The Norwegian lives in the first house.
* The man who smokes Chesterfields lives in the house next to the man
* with the fox.
* Kools are smoked in the house next to the house where the horse is kept.
* The Lucky Strike smoker drinks orange juice.
* The Japanese smokes Parliaments.
* The Norwegian lives next to the blue house.
*
* Who owns a zebra and who drinks water?
* """
*
*/
private static void Solve()
{
Solver solver = new Solver("Zebra");
int n = 5;
//
// Decision variables
//
// Colors
IntVar red = solver.MakeIntVar(1, n, "red");
IntVar green = solver.MakeIntVar(1, n, "green");
IntVar yellow = solver.MakeIntVar(1, n, "yellow");
IntVar blue = solver.MakeIntVar(1, n, "blue");
IntVar ivory = solver.MakeIntVar(1, n, "ivory");
// Nationality
IntVar englishman = solver.MakeIntVar(1, n, "englishman");
IntVar spaniard = solver.MakeIntVar(1, n, "spaniard");
IntVar japanese = solver.MakeIntVar(1, n, "japanese");
IntVar ukrainian = solver.MakeIntVar(1, n, "ukrainian");
IntVar norwegian = solver.MakeIntVar(1, n, "norwegian");
// Animal
IntVar dog = solver.MakeIntVar(1, n, "dog");
IntVar snails = solver.MakeIntVar(1, n, "snails");
IntVar fox = solver.MakeIntVar(1, n, "fox");
IntVar zebra = solver.MakeIntVar(1, n, "zebra");
IntVar horse = solver.MakeIntVar(1, n, "horse");
// Drink
IntVar tea = solver.MakeIntVar(1, n, "tea");
IntVar coffee = solver.MakeIntVar(1, n, "coffee");
IntVar water = solver.MakeIntVar(1, n, "water");
IntVar milk = solver.MakeIntVar(1, n, "milk");
IntVar fruit_juice = solver.MakeIntVar(1, n, "fruit juice");
// Smoke
IntVar old_gold = solver.MakeIntVar(1, n, "old gold");
IntVar kools = solver.MakeIntVar(1, n, "kools");
IntVar chesterfields = solver.MakeIntVar(1, n, "chesterfields");
IntVar lucky_strike = solver.MakeIntVar(1, n, "lucky strike");
IntVar parliaments = solver.MakeIntVar(1, n, "parliaments");
// for search
IntVar[] all_vars =
{parliaments, kools, chesterfields, lucky_strike, old_gold,
englishman, spaniard, japanese, ukrainian, norwegian,
dog, snails, fox, zebra, horse,
tea, coffee, water, milk, fruit_juice,
red, green, yellow, blue, ivory};
//
// Constraints
//
// Alldifferents
solver.Add(new IntVar[]
{red, green, yellow, blue, ivory}.AllDifferent());
solver.Add(new IntVar[]
{englishman, spaniard, japanese, ukrainian, norwegian}.AllDifferent());
solver.Add(new IntVar[]
{dog, snails, fox, zebra, horse}.AllDifferent());
solver.Add(new IntVar[]
{tea, coffee, water, milk, fruit_juice}.AllDifferent());
solver.Add(new IntVar[]
{parliaments, kools, chesterfields, lucky_strike, old_gold}.AllDifferent());
//.........这里部分代码省略.........
示例13: Solve
/**
*
* Photo problem.
*
* Problem statement from Mozart/Oz tutorial:
* http://www.mozart-oz.org/home/doc/fdt/node37.html#section.reified.photo
* """
* Betty, Chris, Donald, Fred, Gary, Mary, and Paul want to align in one
* row for taking a photo. Some of them have preferences next to whom
* they want to stand:
*
* 1. Betty wants to stand next to Gary and Mary.
* 2. Chris wants to stand next to Betty and Gary.
* 3. Fred wants to stand next to Mary and Donald.
* 4. Paul wants to stand next to Fred and Donald.
*
* Obviously, it is impossible to satisfy all preferences. Can you find
* an alignment that maximizes the number of satisfied preferences?
* """
*
* Oz solution:
* 6 # alignment(betty:5 chris:6 donald:1 fred:3 gary:7 mary:4 paul:2)
* [5, 6, 1, 3, 7, 4, 2]
*
*
* Also see http://www.hakank.org/or-tools/photo_problem.py
*
*/
private static void Solve(int show_all_max=0)
{
Solver solver = new Solver("PhotoProblem");
//
// Data
//
String[] persons = {"Betty", "Chris", "Donald", "Fred", "Gary", "Mary", "Paul"};
int n = persons.Length;
IEnumerable<int> RANGE = Enumerable.Range(0, n);
int[,] preferences = {
// 0 1 2 3 4 5 6
// B C D F G M P
{ 0,0,0,0,1,1,0 }, // Betty 0
{ 1,0,0,0,1,0,0 }, // Chris 1
{ 0,0,0,0,0,0,0 }, // Donald 2
{ 0,0,1,0,0,1,0 }, // Fred 3
{ 0,0,0,0,0,0,0 }, // Gary 4
{ 0,0,0,0,0,0,0 }, // Mary 5
{ 0,0,1,1,0,0,0 } // Paul 6
};
Console.WriteLine("Preferences:");
Console.WriteLine("1. Betty wants to stand next to Gary and Mary.");
Console.WriteLine("2. Chris wants to stand next to Betty and Gary.");
Console.WriteLine("3. Fred wants to stand next to Mary and Donald.");
Console.WriteLine("4. Paul wants to stand next to Fred and Donald.\n");
//
// Decision variables
//
IntVar[] positions = solver.MakeIntVarArray(n, 0, n-1, "positions");
// successful preferences (to Maximize)
IntVar z = solver.MakeIntVar(0, n*n, "z");
//
// Constraints
//
solver.Add(positions.AllDifferent());
// calculate all the successful preferences
solver.Add( ( from i in RANGE
from j in RANGE
where preferences[i,j] == 1
select (positions[i] - positions[j]).Abs() == 1
).ToArray().Sum() == z);
//
// Symmetry breaking (from the Oz page):
// Fred is somewhere left of Betty
solver.Add(positions[3] < positions[0]);
//
// Objective
//
OptimizeVar obj = z.Maximize(1);
if (show_all_max > 0) {
Console.WriteLine("Showing all maximum solutions (z == 6).\n");
solver.Add(z == 6);
}
//
// Search
//
DecisionBuilder db = solver.MakePhase(positions,
Solver.CHOOSE_FIRST_UNBOUND,
//.........这里部分代码省略.........
示例14: Solve
/**
*
* Crypto problem in Google CP Solver.
*
* Martin Gardner (February 1967):
* """
* The integers 1,3,8, and 120 form a set with a remarkable property: the
* product of any two integers is one less than a perfect square. Find
* a fifth number that can be added to the set without destroying
* this property.
* """
*
* Also see, http://www.hakank.org/or-tools/curious_set_of_integers.py
*
*/
private static void Solve()
{
Solver solver = new Solver("CuriousSetOfIntegers");
//
// data
//
int n = 5;
int max_val = 10000;
//
// Decision variables
//
IntVar[] x = solver.MakeIntVarArray(n, 0, max_val, "x");
//
// Constraints
//
solver.Add(x.AllDifferent());
for(int i = 0; i < n - 1; i++) {
for(int j = i + 1; j < n; j++) {
IntVar p = solver.MakeIntVar(0, max_val);
solver.Add((p.Square() - 1) - (x[i] * x[j]) == 0);
}
}
// Symmetry breaking
Decreasing(solver, x);
// This is the original problem
// Which is the fifth number?
int[] v = {1,3,8,120};
IntVar[] b = (from i in Enumerable.Range(0, n)
select x[i].IsMember(v)).ToArray();
solver.Add(b.Sum() == 4);
//
// Search
//
DecisionBuilder db = solver.MakePhase(x,
Solver.CHOOSE_MIN_SIZE_LOWEST_MIN,
Solver.ASSIGN_MIN_VALUE);
solver.NewSearch(db);
while (solver.NextSolution()) {
for(int i = 0; i < n; i++) {
Console.Write(x[i].Value() + " ");
}
Console.WriteLine();
}
Console.WriteLine("\nSolutions: {0}", solver.Solutions());
Console.WriteLine("WallTime: {0}ms", solver.WallTime());
Console.WriteLine("Failures: {0}", solver.Failures());
Console.WriteLine("Branches: {0} ", solver.Branches());
solver.EndSearch();
}
示例15: Solve
/**
*
* Eq 20 in Google CP Solver.
*
* Standard benchmark problem.
*
* Also see http://hakank.org/or-tools/eq20.py
*
*/
private static void Solve()
{
Solver solver = new Solver("Eq20");
int n = 7;
//
// Decision variables
//
IntVar X0 = solver.MakeIntVar(0, 10, "X0");
IntVar X1 = solver.MakeIntVar(0, 10, "X1");
IntVar X2 = solver.MakeIntVar(0, 10, "X2");
IntVar X3 = solver.MakeIntVar(0, 10, "X3");
IntVar X4 = solver.MakeIntVar(0, 10, "X4");
IntVar X5 = solver.MakeIntVar(0, 10, "X5");
IntVar X6 = solver.MakeIntVar(0, 10, "X6");
IntVar[] X = {X0,X1,X2,X3,X4,X5,X6};
//
// Constraints
//
solver.Add(-76706*X0 + 98205*X1 + 23445*X2 + 67921*X3 + 24111*X4 +
-48614*X5 + -41906*X6 == 821228);
solver.Add(87059*X0 + -29101*X1 + -5513*X2 + -21219*X3 + 22128*X4 +
7276*X5 + 57308*X6 == 22167);
solver.Add(-60113*X0 + 29475*X1 + 34421*X2 + -76870*X3 + 62646*X4 +
29278*X5 + -15212*X6 == 251591);
solver.Add(49149*X0 + 52871*X1 + -7132*X2 + 56728*X3 + -33576*X4 +
-49530*X5 + -62089*X6 == 146074);
solver.Add(-10343*X0 + 87758*X1 + -11782*X2 + 19346*X3 + 70072*X4 +
-36991*X5 + 44529*X6 == 740061);
solver.Add(85176*X0 + -95332*X1 + -1268*X2 + 57898*X3 + 15883*X4 +
50547*X5 + 83287*X6 == 373854);
solver.Add(-85698*X0 + 29958*X1 + 57308*X2 + 48789*X3 + -78219*X4 +
4657*X5 + 34539*X6 == 249912);
solver.Add(-67456*X0 + 84750*X1 + -51553*X2 + 21239*X3 + 81675*X4 +
-99395*X5 + -4254*X6 == 277271);
solver.Add(94016*X0 + -82071*X1 + 35961*X2 + 66597*X3 + -30705*X4 +
-44404*X5 + -38304*X6 == 25334);
solver.Add(-60301*X0 + 31227*X1 + 93951*X2 + 73889*X3 + 81526*X4 +
-72702*X5 + 68026*X6 == 1410723);
solver.Add(-16835*X0 + 47385*X1 + 97715*X2 + -12640*X3 + 69028*X4 +
76212*X5 + -81102*X6 == 1244857);
solver.Add(-43277*X0 + 43525*X1 + 92298*X2 + 58630*X3 + 92590*X4 +
-9372*X5 + -60227*X6 == 1503588);
solver.Add(-64919*X0 + 80460*X1 + 90840*X2 + -59624*X3 + -75542*X4 +
25145*X5 + -47935*X6 == 18465);
solver.Add(-45086*X0 + 51830*X1 + -4578*X2 + 96120*X3 + 21231*X4 +
97919*X5 + 65651*X6 == 1198280);
solver.Add(85268*X0 + 54180*X1 + -18810*X2 + -48219*X3 + 6013*X4 +
78169*X5 + -79785*X6 == 90614);
solver.Add(8874*X0 + -58412*X1 + 73947*X2 + 17147*X3 + 62335*X4 +
16005*X5 + 8632*X6 == 752447);
solver.Add(71202*X0 + -11119*X1 + 73017*X2 + -38875*X3 + -14413*X4 +
-29234*X5 + 72370*X6 == 129768);
solver.Add(1671*X0 + -34121*X1 + 10763*X2 + 80609*X3 + 42532*X4 +
93520*X5 + -33488*X6 == 915683);
solver.Add(51637*X0 + 67761*X1 + 95951*X2 + 3834*X3 + -96722*X4 +
59190*X5 + 15280*X6 == 533909);
solver.Add(-16105*X0 + 62397*X1 + -6704*X2 + 43340*X3 + 95100*X4 +
-68610*X5 + 58301*X6 == 876370);
//
// Search
//
DecisionBuilder db = solver.MakePhase(X,
Solver.INT_VAR_DEFAULT,
Solver.INT_VALUE_DEFAULT);
solver.NewSearch(db);
while (solver.NextSolution()) {
for(int i = 0; i < n; i++) {
Console.Write(X[i].ToString() + " ");
}
Console.WriteLine();
}
Console.WriteLine("\nSolutions: " + solver.Solutions());
Console.WriteLine("WallTime: " + solver.WallTime() + "ms ");
Console.WriteLine("Failures: " + solver.Failures());
Console.WriteLine("Branches: " + solver.Branches());
solver.EndSearch();
}