本文整理汇总了C#中Solver.Solutions方法的典型用法代码示例。如果您正苦于以下问题:C# Solver.Solutions方法的具体用法?C# Solver.Solutions怎么用?C# Solver.Solutions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Solver
的用法示例。
在下文中一共展示了Solver.Solutions方法的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: Solve
/**
*
* Implements the all interval problem.
* See http://www.hakank.org/google_or_tools/all_interval.py
*
*/
private static void Solve(int n=12)
{
Solver solver = new Solver("AllInterval");
//
// Decision variables
//
IntVar[] x = solver.MakeIntVarArray(n, 0, n-1, "x");
IntVar[] diffs = solver.MakeIntVarArray(n-1, 1, n-1, "diffs");
//
// Constraints
//
solver.Add(x.AllDifferent());
solver.Add(diffs.AllDifferent());
for(int k = 0; k < n - 1; k++) {
// solver.Add(diffs[k] == (x[k + 1] - x[k]).Abs());
solver.Add(diffs[k] == (x[k + 1] - x[k].Abs()));
}
// symmetry breaking
solver.Add(x[0] < x[n - 1]);
solver.Add(diffs[0] < diffs[1]);
//
// 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("{0} ", x[i].Value());
}
Console.Write(" diffs: ");
for(int i = 0; i < n-1; i++) {
Console.Write("{0} ", diffs[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();
}
示例3: Solve
/**
*
* Scheduling speakers problem
*
* From Rina Dechter, Constraint Processing, page 72
* Scheduling of 6 speakers in 6 slots.
*
* See http://www.hakank.org/google_or_tools/scheduling_speakers.py
*
*/
private static void Solve()
{
Solver solver = new Solver("SchedulingSpeakers");
// number of speakers
int n = 6;
// slots available to speak
int[][] available = {
// Reasoning:
new int[] {3,4,5,6}, // 2) the only one with 6 after speaker F -> 1
new int[] {3,4}, // 5) 3 or 4
new int[] {2,3,4,5}, // 3) only with 5 after F -> 1 and A -> 6
new int[] {2,3,4}, // 4) only with 2 after C -> 5 and F -> 1
new int[] {3,4}, // 5) 3 or 4
new int[] {1,2,3,4,5,6} // 1) the only with 1
};
//
// Decision variables
//
IntVar[] x = solver.MakeIntVarArray(n, 1, n, "x");
//
// Constraints
//
solver.Add(x.AllDifferent());
for(int i = 0; i < n; i++) {
solver.Add(x[i].Member(available[i]));
}
//
// Search
//
DecisionBuilder db = solver.MakePhase(x,
Solver.CHOOSE_FIRST_UNBOUND,
Solver.ASSIGN_MIN_VALUE);
solver.NewSearch(db);
while (solver.NextSolution()) {
Console.WriteLine(string.Join(",", (from i in x select 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();
}
示例4: Solve
private static long Solve(long num_buses_check = 0)
{
SolverParameters sPrm = new SolverParameters();
sPrm.compress_trail = 0;
sPrm.trace_level = 0;
sPrm.profile_level = 0;
Solver solver = new Solver("OrTools",sPrm);
//this works
// IntVar[,] x = solver.MakeIntVarMatrix(2,2, new int[] {-2,0,1,2}, "x");
//this doesn't work
IntVar[,] x = solver.MakeIntVarMatrix(2, 2, new int[] { 0, 1, 2 }, "x");
for (int w = 0; w < 2; w++)
{
IntVar[] b = new IntVar[2];
for (int i = 0; i < 2; i++)
{
b[i] = solver.MakeIsEqualCstVar(x[w, i], 0);
}
solver.Add(solver.MakeSumGreaterOrEqual(b, 2));
}
IntVar[] x_flat = x.Flatten();
DecisionBuilder db = solver.MakePhase(x_flat,
Solver.CHOOSE_FIRST_UNBOUND,
Solver.ASSIGN_MIN_VALUE);
solver.NewSearch(db);
while (solver.NextSolution())
{
Console.WriteLine("x: ");
for (int j = 0; j < 2; j++)
{
Console.Write("worker" + (j + 1).ToString() + ":");
for (int i = 0; i < 2; i++)
{
Console.Write(" {0,2} ", x[j, i].Value());
}
Console.Write("\n");
}
Console.WriteLine("End at---->" + DateTime.Now);
}
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();
return 1;
}
示例5: Solve
/**
*
* Magic sequence problem.
*
* This is a port of the Python model
* https://code.google.com/p/or-tools/source/browse/trunk/python/magic_sequence_distribute.py
* """
* This models aims at building a sequence of numbers such that the number of
* occurrences of i in this sequence is equal to the value of the ith number.
* It uses an aggregated formulation of the count expression called
* distribute().
* """
*
*/
private static void Solve(int size)
{
Solver solver = new Solver("MagicSequence");
Console.WriteLine("\nSize: {0}", size);
//
// data
//
int[] all_values = new int[size];
for (int i = 0; i < size; i++) {
all_values[i] = i;
}
//
// Decision variables
//
IntVar[] all_vars = solver.MakeIntVarArray(size, 0, size - 1, "vars");
//
// Constraints
//
solver.Add(all_vars.Distribute(all_values, all_vars));
solver.Add(all_vars.Sum() == size);
//
// Search
//
DecisionBuilder db = solver.MakePhase(all_vars,
Solver.CHOOSE_FIRST_UNBOUND,
Solver.ASSIGN_MIN_VALUE);
solver.NewSearch(db);
while (solver.NextSolution()) {
for(int i = 0; i < size; i++) {
Console.Write(all_vars[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();
}
示例6: Solve
/**
*
* Solve the xkcd problem
* See http://www.hakank.org/google_or_tools/xkcd.py
*
*/
private static void Solve()
{
Solver solver = new Solver("Xkcd");
//
// Constants, inits
//
int n = 6;
// for price and total: multiplied by 100 to be able to use integers
int[] price = {215, 275, 335, 355, 420, 580};
int total = 1505;
//
// Decision variables
//
IntVar[] x = solver.MakeIntVarArray(n, 0, 10, "x");
//
// Constraints
//
solver.Add(x.ScalProd(price) == total);
//
// 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 < 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();
}
示例7: Solve
/**
*
* Solves a set covering problem.
* See See http://www.hakank.org/or-tools/set_covering2.py
*
*/
private static void Solve()
{
Solver solver = new Solver("SetCovering2");
//
// data
//
// Example 9.1-2 from
// Taha "Operations Research - An Introduction",
// page 354ff.
// Minimize the number of security telephones in street
// corners on a campus.
int n = 8; // maximum number of corners
int num_streets = 11; // number of connected streets
// corners of each street
// Note: 1-based (handled below)
int[,] corner = {{1,2},
{2,3},
{4,5},
{7,8},
{6,7},
{2,6},
{1,6},
{4,7},
{2,4},
{5,8},
{3,5}};
//
// Decision variables
//
IntVar[] x = solver.MakeIntVarArray(n, 0, 1, "x");
// number of telephones, to be minimized
IntVar z = x.Sum().Var();
//
// Constraints
//
// ensure that all streets are covered
for(int i = 0; i < num_streets; i++) {
solver.Add(x[corner[i,0] - 1] + x[corner[i,1] - 1] >= 1);
}
//
// objective
//
OptimizeVar objective = z.Minimize(1);
//
// Search
//
DecisionBuilder db = solver.MakePhase(x,
Solver.INT_VAR_DEFAULT,
Solver.INT_VALUE_DEFAULT);
solver.NewSearch(db, objective);
while (solver.NextSolution()) {
Console.WriteLine("z: {0}", z.Value());
Console.Write("x: ");
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();
}
示例8: Solve
/**
*
* Implements a (decomposition) of the global constraint circuit.
* See http://www.hakank.org/google_or_tools/circuit.py
*
*/
private static void Solve(int n = 5)
{
Solver solver = new Solver("Circuit");
//
// Decision variables
//
IntVar[] x = solver.MakeIntVarArray(n, 0, n-1, "x");
//
// Constraints
//
circuit(solver, x);
//
// 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("{0} ", 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();
}
示例9: Solve
//.........这里部分代码省略.........
int P = 15;
int Q = 16;
int R = 17;
int S = 18;
int T = 19;
int U = 20;
int V = 21;
int W = 22;
int Y = 23;
String[] letters_str = {"A","B","C","D","E","F","G","H","I","J","K","L","M",
"N","O","P","Q","R","S","T","U","V","W","Y"};
int num_words = 13;
int[,] words =
{
{B,U,O,Y},
{C,A,V,E},
{C,E,L,T},
{F,L,U,B},
{F,O,R,K},
{H,E,M,P},
{J,U,D,Y},
{J,U,N,K},
{L,I,M,N},
{Q,U,I,P},
{S,W,A,G},
{V,I,S,A},
{W,I,S,H}
};
//
// Decision variables
//
IntVar[] dice = solver.MakeIntVarArray(m, 0, n-1, "dice");
IntVar[] gcc = solver.MakeIntVarArray(n, 6, 6, "gcc");
//
// Constraints
//
// the letters in a word must be on a different die
for(int i = 0; i < num_words; i++) {
solver.Add( (from j in Enumerable.Range(0, n)
select dice[words[i,j]]
).ToArray().AllDifferent());
}
// there must be exactly 6 letters of each die
/*
for(int i = 0; i < n; i++) {
solver.Add( ( from j in Enumerable.Range(0, m)
select (dice[j] == i)
).ToArray().Sum() == 6 );
}
*/
// Use Distribute (Global Cardinality Count) instead.
solver.Add(dice.Distribute(gcc));
//
// Search
//
DecisionBuilder db = solver.MakePhase(dice,
Solver.CHOOSE_FIRST_UNBOUND,
Solver.ASSIGN_MIN_VALUE);
solver.NewSearch(db);
while (solver.NextSolution()) {
for(int d = 0; d < n; d++) {
Console.Write("die {0}: ", d);
for(int i = 0; i < m; i++) {
if (dice[i].Value() == d) {
Console.Write(letters_str[i]);
}
}
Console.WriteLine();
}
Console.WriteLine("The words with the cube label:");
for(int i = 0; i < num_words; i++) {
for(int j = 0; j < n; j++) {
Console.Write("{0} ({1})", letters_str[words[i,j]], dice[words[i,j]].Value());
}
Console.WriteLine();
}
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();
}
示例10: Solve
//.........这里部分代码省略.........
IntVar[] all = new IntVar[2 * n * n]; // for branching
for(int i = 0; i < n*n; i++) {
all[i] = hates_flat[i];
all[(n*n)+i] = richer_flat[i];
}
//
// Constraints
//
// Agatha, the butler, and Charles live in Dreadsbury Mansion, and
// are the only ones to live there.
// A killer always hates, and is no richer than his victim.
// hates[the_killer, the_victim] == 1
// hates_flat[the_killer * n + the_victim] == 1
solver.Add(hates_flat.Element(the_killer * n + the_victim) == 1);
// richer[the_killer, the_victim] == 0
solver.Add(richer_flat.Element(the_killer * n + the_victim) == 0);
// define the concept of richer:
// no one is richer than him-/herself...
for(int i = 0; i < n; i++) {
solver.Add(richer[i,i] == 0);
}
// (contd...) if i is richer than j then j is not richer than i
// if (i != j) =>
// ((richer[i,j] = 1) <=> (richer[j,i] = 0))
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if (i != j) {
solver.Add((richer[i, j]==1) - (richer[j, i]==0) == 0);
}
}
}
// Charles hates no one that Agatha hates.
// forall i in 0..2:
// (hates[agatha, i] = 1) => (hates[charles, i] = 0)
for(int i = 0; i < n; i++) {
solver.Add((hates[agatha,i]==1) - (hates[charles,i]==0) <= 0);
}
// Agatha hates everybody except the butler.
solver.Add(hates[agatha,charles] == 1);
solver.Add(hates[agatha,agatha] == 1);
solver.Add(hates[agatha,butler] == 0);
// The butler hates everyone not richer than Aunt Agatha.
// forall i in 0..2:
// (richer[i, agatha] = 0) => (hates[butler, i] = 1)
for(int i = 0; i < n; i++) {
solver.Add((richer[i,agatha] == 0)-(hates[butler,i] == 1)<=0);
}
// The butler hates everyone whom Agatha hates.
// forall i : 0..2:
// (hates[agatha, i] = 1) => (hates[butler, i] = 1)
for(int i = 0; i < n; i++) {
solver.Add((hates[agatha,i] == 1)-(hates[butler,i] == 1)<=0);
}
// Noone hates everyone.
// forall i in 0..2:
// (sum j in 0..2: hates[i,j]) <= 2
for(int i = 0; i < n; i++) {
solver.Add((from j in Enumerable.Range(0, n)
select hates[i,j]
).ToArray().Sum() <= 2 );
}
// Who killed Agatha?
solver.Add(the_victim == agatha);
//
// Search
//
DecisionBuilder db = solver.MakePhase(all,
Solver.CHOOSE_FIRST_UNBOUND,
Solver.ASSIGN_MIN_VALUE);
solver.NewSearch(db);
while (solver.NextSolution()) {
Console.WriteLine("the_killer: " + the_killer.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();
}
示例11: Solve
/**
*
* Solves a set covering problem.
* See See http://www.hakank.org/or-tools/set_covering4.py
*
*/
private static void Solve(int set_partition)
{
Solver solver = new Solver("SetCovering4");
//
// data
//
// Set partition and set covering problem from
// Example from the Swedish book
// Lundgren, Roennqvist, Vaebrand
// 'Optimeringslaera' (translation: 'Optimization theory'),
// page 408.
int num_alternatives = 10;
int num_objects = 8;
// costs for the alternatives
int[] costs = {19, 16, 18, 13, 15, 19, 15, 17, 16, 15};
// the alternatives, and their objects
int[,] a = {
// 1 2 3 4 5 6 7 8 the objects
{1,0,0,0,0,1,0,0}, // alternative 1
{0,1,0,0,0,1,0,1}, // alternative 2
{1,0,0,1,0,0,1,0}, // alternative 3
{0,1,1,0,1,0,0,0}, // alternative 4
{0,1,0,0,1,0,0,0}, // alternative 5
{0,1,1,0,0,0,0,0}, // alternative 6
{0,1,1,1,0,0,0,0}, // alternative 7
{0,0,0,1,1,0,0,1}, // alternative 8
{0,0,1,0,0,1,0,1}, // alternative 9
{1,0,0,0,0,1,1,0}}; // alternative 10
//
// Decision variables
//
IntVar[] x = solver.MakeIntVarArray(num_alternatives, 0, 1, "x");
// number of assigned senators, to be minimized
IntVar z = x.ScalProd(costs).VarWithName("z");
//
// Constraints
//
for(int j = 0; j < num_objects; j++) {
IntVar[] b = new IntVar[num_alternatives];
for(int i = 0; i < num_alternatives; i++) {
b[i] = (x[i] * a[i,j]).Var();
}
if (set_partition == 1) {
solver.Add(b.Sum() >= 1);
} else {
solver.Add(b.Sum() == 1);
}
}
//
// objective
//
OptimizeVar objective = z.Minimize(1);
//
// Search
//
DecisionBuilder db = solver.MakePhase(x,
Solver.INT_VAR_DEFAULT,
Solver.INT_VALUE_DEFAULT);
solver.NewSearch(db, objective);
while (solver.NextSolution()) {
Console.WriteLine("z: " + z.Value());
Console.Write("Selected alternatives: ");
for(int i = 0; i < num_alternatives; i++) {
if (x[i].Value() == 1) {
Console.Write((i+1) + " ");
}
}
Console.WriteLine("\n");
}
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
//.........这里部分代码省略.........
// Another representation of Problem 1
int[,] problem1 = {
{8,8,0,9, 6},
{7,1,1,1, 0},
{2,1,7,2, 0},
{6,6,6,6, 4},
{1,1,1,1, 0},
{3,2,1,3, 0},
{7,6,6,2, 2},
{9,3,1,2, 1},
{0,0,0,0, 4},
{2,2,2,2, 0},
{3,3,3,3, 0},
{5,5,5,5, 0},
{8,1,9,3, 3},
{8,0,9,6, 5},
{7,7,7,7, 0},
{9,9,9,9, 4},
{7,7,5,6, 1},
{6,8,5,5, 3},
{9,8,8,1, 5},
{5,5,3,1, 0}
};
for(int i = 0; i < problem1.GetLength(0); i++) {
solver.Add( (from j in Enumerable.Range(0, 4)
select all[problem1[i,j]]
).ToArray().Sum() == problem1[i,4] );
}
solver.Add(all[2]+all[5]+all[8]+all[1] == x);
} else if (p == 3) {
// Problem 2
solver.Add(x8+x8+x0+x9 == 6);
solver.Add(x7+x6+x6+x2 == 2);
solver.Add(x9+x3+x1+x2 == 1);
solver.Add(x8+x1+x9+x3 == 3);
solver.Add(x8+x0+x9+x6 == 5);
solver.Add(x7+x7+x5+x6 == 1);
solver.Add(x6+x8+x5+x5 == 3);
solver.Add(x9+x8+x8+x1 == 5);
// The unknown
solver.Add(x2+x5+x8+x1 == x);
} else {
// Another representation of Problem 2
int[,] problem2 = {
{8,8,0,9, 6},
{7,6,6,2, 2},
{9,3,1,2, 1},
{8,1,9,3, 3},
{8,0,9,6, 5},
{7,7,5,6, 1},
{6,8,5,5, 3},
{9,8,8,1, 5}
};
for(int i = 0; i < problem2.GetLength(0); i++) {
solver.Add( (from j in Enumerable.Range(0, 4)
select all[problem2[i,j]]
).ToArray().Sum() == problem2[i,4] );
}
solver.Add(all[2]+all[5]+all[8]+all[1] == x);
}
//
// Search
//
DecisionBuilder db = solver.MakePhase(all,
Solver.INT_VAR_DEFAULT,
Solver.INT_VALUE_DEFAULT);
solver.NewSearch(db);
int c = 0;
while (solver.NextSolution()) {
Console.Write("x: {0} x0..x9: ", x.Value());
for(int i = 0; i < n; i++) {
Console.Write(all[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();
}
示例13: Solve
//.........这里部分代码省略.........
{22, 0, 15, 0, 0},
{23, 0, 1, 14, 11},
{0, 25, 0, 0, 12}};
puzzle = puzzle3;
} else if (model == 4) {
// problem 2 (Practice)
int[,] puzzle4 = {{0, 0, 0, 0, 14},
{0, 18, 12, 0, 0},
{0, 0, 17, 4, 5},
{0, 0, 7, 0, 0},
{9, 8, 25, 1, 0}};
puzzle = puzzle4;
} else if (model == 5) {
// problem 3 (Beginner)
int[,] puzzle5 = {{0, 26, 0, 0, 0, 18},
{0, 0, 27, 0, 0, 19},
{31, 23, 0, 0, 14, 0},
{0, 33, 8, 0, 15, 1},
{0, 0, 0, 5, 0, 0},
{35, 36, 0, 10, 0, 0}};
puzzle = puzzle5;
} else if (model == 6) {
// Problem 15 (Intermediate)
int[,] puzzle6 = {{64, 0, 0, 0, 0, 0, 0, 0},
{1, 63, 0, 59, 15, 57, 53, 0},
{0, 4, 0, 14, 0, 0, 0, 0},
{3, 0, 11, 0, 20, 19, 0, 50},
{0, 0, 0, 0, 22, 0, 48, 40},
{9, 0, 0, 32, 23, 0, 0, 41},
{27, 0, 0, 0, 36, 0, 46, 0},
{28, 30, 0, 35, 0, 0, 0, 0}};
puzzle = puzzle6;
}
int r = puzzle.GetLength(0);
int c = puzzle.GetLength(1);
Console.WriteLine();
Console.WriteLine("----- Solving problem {0} -----", model);
Console.WriteLine();
PrintMatrix(puzzle);
//
// Decision variables
//
IntVar[] positions = solver.MakeIntVarArray(r*c, 0, r * c - 1, "p");
//
// Constraints
//
solver.Add(positions.AllDifferent());
//
// Fill in the clues
//
for(int i = 0; i < r; i++) {
for(int j = 0; j < c; j++) {
if (puzzle[i,j] > 0) {
solver.Add(positions[puzzle[i,j] - 1] == i * c + j);
}
}
}
// Consecutive numbers much touch each other in the grid.
// We use an allowed assignment constraint to model it.
IntTupleSet close_tuples = BuildPairs(r, c);
for(int k = 1; k < r * c - 1; k++) {
IntVar[] tmp = new IntVar[] {positions[k], positions[k + 1]};
solver.Add(tmp.AllowedAssignments(close_tuples));
}
//
// Search
//
DecisionBuilder db = solver.MakePhase(positions,
Solver.CHOOSE_MIN_SIZE_LOWEST_MIN,
Solver.ASSIGN_MIN_VALUE);
solver.NewSearch(db);
int num_solution = 0;
while (solver.NextSolution()) {
num_solution++;
PrintOneSolution(positions, r, c, num_solution);
}
Console.WriteLine("\nSolutions: " + solver.Solutions());
Console.WriteLine("WallTime: " + solver.WallTime() + "ms ");
Console.WriteLine("Failures: " + solver.Failures());
Console.WriteLine("Branches: " + solver.Branches());
solver.EndSearch();
}
示例14: Solve
//.........这里部分代码省略.........
{
Solver solver = new Solver("KenKen2");
// size of matrix
int n = 6;
IEnumerable<int> RANGE = Enumerable.Range(0, n);
// For a better view of the problem, see
// http://en.wikipedia.org/wiki/File:KenKenProblem.svg
// hints
// sum, the hints
// Note: this is 1-based
int[][] problem =
{
new int[] { 11, 1,1, 2,1},
new int[] { 2, 1,2, 1,3},
new int[] { 20, 1,4, 2,4},
new int[] { 6, 1,5, 1,6, 2,6, 3,6},
new int[] { 3, 2,2, 2,3},
new int[] { 3, 2,5, 3,5},
new int[] {240, 3,1, 3,2, 4,1, 4,2},
new int[] { 6, 3,3, 3,4},
new int[] { 6, 4,3, 5,3},
new int[] { 7, 4,4, 5,4, 5,5},
new int[] { 30, 4,5, 4,6},
new int[] { 6, 5,1, 5,2},
new int[] { 9, 5,6, 6,6},
new int[] { 8, 6,1, 6,2, 6,3},
new int[] { 2, 6,4, 6,5}
};
int num_p = problem.GetLength(0); // Number of segments
//
// Decision variables
//
IntVar[,] x = solver.MakeIntVarMatrix(n, n, 1, n, "x");
IntVar[] x_flat = x.Flatten();
//
// Constraints
//
//
// alldifferent rows and columns
foreach(int i in RANGE) {
// rows
solver.Add( (from j in RANGE select x[i,j]).ToArray().AllDifferent());
// cols
solver.Add( (from j in RANGE select x[j,i]).ToArray().AllDifferent());
}
// Calculate the segments
for(int i = 0; i < num_p; i++) {
int[] segment = problem[i];
// Remove the sum from the segment
int len = segment.Length-1;
int[] s2 = new int[len];
Array.Copy(segment, 1, s2, 0, len);
// sum this segment
calc(solver, s2, x, segment[0]);
}
//
// Search
//
DecisionBuilder db = solver.MakePhase(x_flat,
Solver.INT_VAR_DEFAULT,
Solver.INT_VALUE_DEFAULT);
solver.NewSearch(db);
while (solver.NextSolution()) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
Console.Write(x[i,j].Value() + " ");
}
Console.WriteLine();
}
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
//.........这里部分代码省略.........
// size of crew
IntVar[] tmp = new IntVar[num_persons];
for(int p = 0; p < num_persons; p++) {
tmp[p] = crew[f,p];
}
solver.Add(tmp.Sum() == required_crew[f,0]);
// attributes and requirements
for(int a = 0; a < 5; a++) {
IntVar[] tmp2 = new IntVar[num_persons];
for(int p = 0; p < num_persons; p++) {
tmp2[p] = (crew[f,p]*attributes[p,a]).Var();
}
solver.Add(tmp2.Sum() >= required_crew[f,a+1]);
}
}
// after a flight, break for at least two flights
for(int f = 0; f < num_flights - 2; f++) {
for(int i = 0; i < num_persons; i++) {
solver.Add(crew[f,i] + crew[f+1,i] + crew[f+2,i] <= 1);
}
}
// extra contraint: all must work at least two of the flights
/*
for(int p = 0; p < num_persons; p++) {
IntVar[] tmp = new IntVar[num_flights];
for(int f = 0; f < num_flights; f++) {
tmp[f] = crew[f,p];
}
solver.Add(tmp.Sum() >= 2);
}
*/
//
// Search
//
DecisionBuilder db = solver.MakePhase(crew_flat,
Solver.CHOOSE_FIRST_UNBOUND,
Solver.ASSIGN_MIN_VALUE);
if (minimize > 0) {
OptimizeVar obj = num_working.Minimize(1);
solver.NewSearch(db, obj);
} else {
solver.NewSearch(db);
}
int num_solutions = 0;
while (solver.NextSolution()) {
num_solutions++;
Console.WriteLine("Solution #{0}", num_solutions);
Console.WriteLine("Number working: {0}", num_working.Value());
for(int f = 0; f < num_flights; f++) {
for(int p = 0; p < num_persons; p++) {
Console.Write(crew[f,p].Value() + " ");
}
Console.WriteLine();
}
Console.WriteLine("\nFlights: ");
for(int f = 0; f < num_flights; f++) {
Console.Write("Flight #{0}: ", f);
for(int p = 0; p < num_persons; p++) {
if (crew[f, p].Value() == 1) {
Console.Write(names[p] + " ");
}
}
Console.WriteLine();
}
Console.WriteLine("\nCrew:");
for(int p = 0; p < num_persons; p++) {
Console.Write("{0,-10}", names[p]);
for(int f = 0; f < num_flights; f++) {
if (crew[f,p].Value() == 1) {
Console.Write(f + " ");
}
}
Console.WriteLine();
}
Console.WriteLine();
if (num_solutions >= sols) {
break;
}
}
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();
}