本文整理汇总了C#中Solver.NextSolution方法的典型用法代码示例。如果您正苦于以下问题:C# Solver.NextSolution方法的具体用法?C# Solver.NextSolution怎么用?C# Solver.NextSolution使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Solver
的用法示例。
在下文中一共展示了Solver.NextSolution方法的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
/**
*
* Decomposition of alldifferent_except_0
*
* See http://www.hakank.org/google_or_tools/map.py
*
*
*/
private static void Solve()
{
Solver solver = new Solver("AllDifferentExcept0");
//
// data
//
int n = 6;
//
// Decision variables
//
IntVar[] x = solver.MakeIntVarArray(n, 0, n - 1 , "x");
//
// Constraints
//
AllDifferentExcept0(solver, x);
// we also require at least 2 0's
IntVar[] z_tmp = new IntVar[n];
for(int i = 0; i < n; i++) {
z_tmp[i] = x[i] == 0;
}
IntVar z = z_tmp.Sum().VarWithName("z");
solver.Add(z == 2);
//
// Search
//
DecisionBuilder db = solver.MakePhase(x,
Solver.CHOOSE_FIRST_UNBOUND,
Solver.INT_VALUE_DEFAULT);
solver.NewSearch(db);
while (solver.NextSolution()) {
Console.Write("z: {0} x: ", z.Value());
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();
}
示例5: 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;
}
示例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
/**
*
* 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();
}
示例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: 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();
}
示例10: 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();
}
示例11: 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();
}
示例12: 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();
}
示例13: 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();
}
示例14: 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();
//.........这里部分代码省略.........
示例15: Solve
/**
*
* Lectures problem in Google CP Solver.
*
* Biggs: Discrete Mathematics (2nd ed), page 187.
* """
* Suppose we wish to schedule six one-hour lectures, v1, v2, v3, v4, v5, v6.
* Among the the potential audience there are people who wish to hear both
*
* - v1 and v2
* - v1 and v4
* - v3 and v5
* - v2 and v6
* - v4 and v5
* - v5 and v6
* - v1 and v6
*
* How many hours are necessary in order that the lectures can be given
* without clashes?
* """
*
* Note: This can be seen as a coloring problem.
*
* Also see http://www.hakank.org/or-tools/lectures.py
*
*/
private static void Solve()
{
Solver solver = new Solver("Lectures");
//
// The schedule requirements:
// lecture a cannot be held at the same time as b
// Note: 1-based (compensated in the constraints).
int[,] g =
{
{1, 2},
{1, 4},
{3, 5},
{2, 6},
{4, 5},
{5, 6},
{1, 6}
};
// number of nodes
int n = 6;
// number of edges
int edges = g.GetLength(0);
//
// Decision variables
//
//
// declare variables
//
IntVar[] v = solver.MakeIntVarArray(n, 0, n-1,"v");
// Maximum color (hour) to minimize.
// Note: since C# is 0-based, the
// number of colors is max_c+1.
IntVar max_c = v.Max().VarWithName("max_c");
//
// Constraints
//
// Ensure that there are no clashes
// also, adjust to 0-base.
for(int i = 0; i < edges; i++) {
solver.Add(v[g[i,0]-1] != v[g[i,1]-1]);
}
// Symmetry breaking:
// - v0 has the color 0,
// - v1 has either color 0 or 1
solver.Add(v[0] == 0);
solver.Add(v[1] <= 1);
//
// Objective
//
OptimizeVar obj = max_c.Minimize(1);
//
// Search
//
DecisionBuilder db = solver.MakePhase(v,
Solver.CHOOSE_MIN_SIZE_LOWEST_MIN,
Solver.ASSIGN_MIN_VALUE);
solver.NewSearch(db, obj);
while (solver.NextSolution()) {
Console.WriteLine("\nmax hours: {0}", max_c.Value()+1);
Console.WriteLine("v: " +
String.Join(" ", (from i in Enumerable.Range(0, n)
//.........这里部分代码省略.........