本文整理汇总了C#中Solver.Branches方法的典型用法代码示例。如果您正苦于以下问题:C# Solver.Branches方法的具体用法?C# Solver.Branches怎么用?C# Solver.Branches使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Solver
的用法示例。
在下文中一共展示了Solver.Branches方法的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
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;
}
示例4: 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();
}
示例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 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();
}
示例8: 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();
}
示例9: 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();
}
示例10: 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();
//.........这里部分代码省略.........
示例11: Solve
/**
*
* Coin application.
*
* From "Constraint Logic Programming using ECLiPSe"
* pages 99f and 234 ff.
* The solution in ECLiPSe is at page 236.
*
* """
* What is the minimum number of coins that allows one to pay _exactly_
* any amount smaller than one Euro? Recall that there are six different
* euro cents, of denomination 1, 2, 5, 10, 20, 50
* """
* Also see http://www.hakank.org/or-tools/coins3.py
*
*/
private static void Solve()
{
Solver solver = new Solver("Coins3");
//
// Data
//
int n = 6; // number of different coins
int[] variables = {1, 2, 5, 10, 25, 50};
IEnumerable<int> RANGE = Enumerable.Range(0, n);
//
// Decision variables
//
IntVar[] x = solver.MakeIntVarArray(n, 0, 99, "x");
IntVar num_coins = x.Sum().VarWithName("num_coins");
//
// Constraints
//
// Check that all changes from 1 to 99 can be made.
for(int j = 1; j < 100; j++) {
IntVar[] tmp = solver.MakeIntVarArray(n, 0, 99, "tmp");
solver.Add(tmp.ScalProd(variables) == j);
foreach(int i in RANGE) {
solver.Add(tmp[i] <= x[i]);
}
}
//
// Objective
//
OptimizeVar obj = num_coins.Minimize(1);
//
// Search
//
DecisionBuilder db = solver.MakePhase(x,
Solver.CHOOSE_MIN_SIZE_LOWEST_MAX,
Solver.ASSIGN_MIN_VALUE);
solver.NewSearch(db, obj);
while (solver.NextSolution()) {
Console.WriteLine("num_coins: {0}", num_coins.Value());
Console.Write("x: ");
foreach(int i in RANGE) {
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
//.........这里部分代码省略.........
* Problem 26: Shidoku from
* "Taking Sudoku Seriously", page 61
* 4 _ _ _
* 3 1 _ _
*
* _ _ 4 1
* _ _ _ 2
*
*/
private static void Solve()
{
Solver solver = new Solver("Sudoku");
//
// data
//
int block_size = 2;
IEnumerable<int> BLOCK = Enumerable.Range(0, block_size);
int n = block_size * block_size;
IEnumerable<int> RANGE = Enumerable.Range(0, n);
// 0 marks an unknown value
int[,] initial_grid = {{4, 0, 0, 0},
{3, 1, 0, 0},
{0, 0, 4, 1},
{0, 0, 0, 2}};
//
// Decision variables
//
IntVar[,] grid = solver.MakeIntVarMatrix(n, n, 1, n, "grid");
IntVar[] grid_flat = grid.Flatten();
//
// Constraints
//
// init
foreach(int i in RANGE) {
foreach(int j in RANGE) {
if (initial_grid[i,j] > 0) {
solver.Add(grid[i,j] == initial_grid[i,j]);
}
}
}
foreach(int i in RANGE) {
// rows
solver.Add( (from j in RANGE
select grid[i,j]).ToArray().AllDifferent());
// cols
solver.Add( (from j in RANGE
select grid[j,i]).ToArray().AllDifferent());
}
// blocks
foreach(int i in BLOCK) {
foreach(int j in BLOCK) {
solver.Add( (from di in BLOCK
from dj in BLOCK
select grid[i*block_size+di, j*block_size+dj]
).ToArray().AllDifferent());
}
}
//
// Search
//
DecisionBuilder db = solver.MakePhase(grid_flat,
Solver.INT_VAR_SIMPLE,
Solver.INT_VALUE_SIMPLE);
solver.NewSearch(db);
while (solver.NextSolution()) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++){
Console.Write("{0} ", grid[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();
}
示例13: Solve
//.........这里部分代码省略.........
* - 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)
select v[i].Value()).ToArray()));
for(int i = 0; i < n; i++) {
Console.WriteLine("Lecture {0} at {1}h", i, v[i].Value());
}
Console.WriteLine("\n");
}
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
/**
*
*
* Organizing a day.
*
* Simple scheduling problem.
*
* Problem formulation from ECLiPSe:
* Slides on (Finite Domain) Constraint Logic Programming, page 38f
* http://eclipse-clp.org/reports/eclipse.ppt
*
*
* Also see http://www.hakank.org/google_or_tools/organize_day.py
*
*/
private static void Solve()
{
Solver solver = new Solver("OrganizeDay");
int n = 4;
int work = 0;
int mail = 1;
int shop = 2;
int bank = 3;
int[] tasks = {work, mail, shop, bank};
int[] durations = {4,1,2,1};
// task [i,0] must be finished before task [i,1]
int[,] before_tasks = {
{bank, shop},
{mail, work}
};
// the valid times of the day
int begin = 9;
int end = 17;
//
// Decision variables
//
IntVar[] begins = solver.MakeIntVarArray(n, begin, end, "begins");
IntVar[] ends = solver.MakeIntVarArray(n, begin, end, "ends");
//
// Constraints
//
foreach(int t in tasks) {
solver.Add(ends[t] == begins[t] + durations[t]);
}
foreach(int i in tasks) {
foreach(int j in tasks) {
if (i < j) {
NoOverlap(solver,
begins[i], durations[i],
begins[j], durations[j]);
}
}
}
// specific constraints
for(int t = 0; t < before_tasks.GetLength(0); t++) {
solver.Add(ends[before_tasks[t,0]] <= begins[before_tasks[t,1]]);
}
solver.Add(begins[work] >= 11);
//
// Search
//
DecisionBuilder db = solver.MakePhase(begins,
Solver.INT_VAR_DEFAULT,
Solver.INT_VALUE_DEFAULT);
solver.NewSearch(db);
while (solver.NextSolution()) {
foreach(int t in tasks) {
Console.WriteLine("Task {0}: {1,2} .. ({2}) .. {3,2}",
t,
begins[t].Value(),
durations[t],
ends[t].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
/**
*
* Olympic puzzle.
*
* Benchmark for Prolog (BProlog)
* """
* File : olympic.pl
* Author : Neng-Fa ZHOU
* Date : 1993
*
* Purpose: solve a puzzle taken from Olympic Arithmetic Contest
*
* Given ten variables with the following configuration:
*
* X7 X8 X9 X10
*
* X4 X5 X6
*
* X2 X3
*
* X1
*
* We already know that X1 is equal to 3 and want to assign each variable
* with a different integer from {1,2,...,10} such that for any three
* variables
* Xi Xj
*
* Xk
*
* the following constraint is satisfied:
*
* |Xi-Xj| = Xk
* """
*
* Also see http://www.hakank.org/or-tools/olympic.py
*
*/
private static void Solve()
{
Solver solver = new Solver("Olympic");
//
// Data
//
int n = 10;
//
// Decision variables
//
IntVar[] x = solver.MakeIntVarArray(n, 1, n, "x");
IntVar X1 = x[0];
IntVar X2 = x[1];
IntVar X3 = x[2];
IntVar X4 = x[3];
IntVar X5 = x[4];
IntVar X6 = x[5];
IntVar X7 = x[6];
IntVar X8 = x[7];
IntVar X9 = x[8];
IntVar X10 = x[9];
//
// Constraints
//
solver.Add(x.AllDifferent());
solver.Add(X1 == 3);
minus(solver, X2, X3, X1);
minus(solver, X4, X5, X2);
minus(solver, X5, X6, X3);
minus(solver, X7, X8, X4);
minus(solver, X8, X9, X5);
minus(solver, X9, X10, X6);
//
// Search
//
DecisionBuilder db = solver.MakePhase(x,
Solver.INT_VAR_SIMPLE,
Solver.INT_VALUE_DEFAULT);
solver.NewSearch(db);
while (solver.NextSolution()) {
for(int i = 0; i < n; i++) {
Console.Write("{0,2} ", x[i].Value());
}
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();
}