本文整理汇总了C#中Solver.MakeIntConst方法的典型用法代码示例。如果您正苦于以下问题:C# Solver.MakeIntConst方法的具体用法?C# Solver.MakeIntConst怎么用?C# Solver.MakeIntConst使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Solver
的用法示例。
在下文中一共展示了Solver.MakeIntConst方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Solve
/**
*
* Secret Santa problem in Google CP Solver.
*
* From Ruby Quiz Secret Santa
* http://www.rubyquiz.com/quiz2.html
* """
* Honoring a long standing tradition started by my wife's dad, my friends
* all play a Secret Santa game around Christmas time. We draw names and
* spend a week sneaking that person gifts and clues to our identity. On the
* last night of the game, we get together, have dinner, share stories, and,
* most importantly, try to guess who our Secret Santa was. It's a crazily
* fun way to enjoy each other's company during the holidays.
*
* To choose Santas, we use to draw names out of a hat. This system was
* tedious, prone to many 'Wait, I got myself...' problems. This year, we
* made a change to the rules that further complicated picking and we knew
* the hat draw would not stand up to the challenge. Naturally, to solve
* this problem, I scripted the process. Since that turned out to be more
* interesting than I had expected, I decided to share.
*
* This weeks Ruby Quiz is to implement a Secret Santa selection script.
* * Your script will be fed a list of names on STDIN.
* ...
* Your script should then choose a Secret Santa for every name in the list.
* Obviously, a person cannot be their own Secret Santa. In addition, my friends
* no longer allow people in the same family to be Santas for each other and your
* script should take this into account.
* """
*
* Comment: This model skips the file input and mail parts. We
* assume that the friends are identified with a number from 1..n,
* and the families is identified with a number 1..num_families.
*
* Also see http://www.hakank.org/or-tools/secret_santa.py
* Also see http://www.hakank.org/or-tools/secret_santa2.cs
*
*/
private static void Solve()
{
Solver solver = new Solver("SecretSanta");
int[] family = {1,1,1,1, 2, 3,3,3,3,3, 4,4};
int n = family.Length;
Console.WriteLine("n = {0}", n);
IEnumerable<int> RANGE = Enumerable.Range(0, n);
//
// Decision variables
//
IntVar[] x = solver.MakeIntVarArray(n, 0, n-1, "x");
//
// Constraints
//
solver.Add(x.AllDifferent());
// Can't be one own"s Secret Santa
// (i.e. ensure that there are no fix-point in the array.)
foreach(int i in RANGE) {
solver.Add(x[i] != i);
}
// No Secret Santa to a person in the same family
foreach(int i in RANGE) {
solver.Add(solver.MakeIntConst(family[i]) != family.Element(x[i]));
}
//
// Search
//
DecisionBuilder db = solver.MakePhase(x,
Solver.INT_VAR_SIMPLE,
Solver.INT_VALUE_SIMPLE);
solver.NewSearch(db);
while (solver.NextSolution()) {
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();
}
示例2: Solve
//.........这里部分代码省略.........
//
// New: Number of members in each team
//
// Note that we restrict the possible values to +/- team_diff.
int n2 = (int)n / num_teams;
Console.WriteLine("n2: " + n2);
IntVar[] team_num = solver.MakeIntVarArray(num_teams, n2-team_diff, n2+team_diff, "team_num");
for(int team = 0; team < num_teams; team++) {
team_num[team] = (from k in NRange select (x[k] == team) ).ToArray().Sum().Var();
}
// New: We send all the IntVar arrays to the solver
IntVar[] all = x.Concat(team_sum).Concat(team_num).ToArray();
//
// Constraints
//
// New: Ensure that there are the (about) same number of people in each team.
for(int t = 0; t < num_teams; t++) {
solver.Add((((from k in NRange select (x[k]==t)).ToArray().Sum()-n2)).Abs() <= team_diff);
}
// The teams_sums should add up to the total sum
solver.Add(team_sum.Sum() == the_sum);
// symmetry breaking: assign first member to team 0
solver.Add(x[0] == 0);
// Odd sum must yield odd diff, even sum yield even diff
IntVar even_odd = solver.MakeIntConst(the_sum % 2);
solver.Add(solver.MakeModuloConstraint(diff, 2, even_odd));
//
// Search
//
DecisionBuilder db = solver.MakePhase(all,
// Solver.INT_VAR_DEFAULT,
// Solver.INT_VAR_SIMPLE,
// Solver.CHOOSE_FIRST_UNBOUND,
// Solver.CHOOSE_RANDOM,
Solver.CHOOSE_MIN_SIZE_LOWEST_MIN,
// Solver.CHOOSE_MIN_SIZE_HIGHEST_MIN,
// Solver.CHOOSE_MIN_SIZE_LOWEST_MAX,
// Solver.CHOOSE_MIN_SIZE_HIGHEST_MAX,
// Solver.CHOOSE_PATH,
// Solver.INT_VALUE_DEFAULT
// Solver.INT_VALUE_SIMPLE
// Solver.ASSIGN_MIN_VALUE
// Solver.ASSIGN_MAX_VALUE
Solver.ASSIGN_RANDOM_VALUE
// Solver.ASSIGN_CENTER_VALUE
);
/*
DefaultPhaseParameters parameters = new DefaultPhaseParameters();
parameters.heuristic_period = 20;
示例3: Solve
/**
*
* This model was inspired by David Curran's
* blog post "The Fairest Way to Pick a Team "
* http://liveatthewitchtrials.blogspot.se/2012/06/fairest-way-to-pick-team.html
* """
* What is the best way to pick a team? As kids we would always strictly alternate
* between teams so team 1 had first team 2 the second pick and then team 1 again etc.
*
* Most things you can measure about people are on a bell curve. A small number of
* people are bad, most are in the middle and a few are good. There are a few good
* known metrics of ability. None are perfect, there is no one number that can sum up
* ability. The simpler the sport the more one metric can tell you, in cycling VO2 max is
* a very good indicator. Whereas in soccer VO2 max, kicking speed, vertical leap, number
* of keep me ups you can do etc could all measure some part of football ability.
*
* So say there was one good metric for a task and teams were picked based on this.
* Is the standard strict alteration, where Team 1 picks then Team 2 alternating, fair?
* Fair here meaning both teams end up with a similar quality.
* """
*
* Model by Hakan Kjellerstrand ([email protected])
* See other or-tools/C# models at http://www.hakank.org/or-tools/#csharp
*
*/
private static void Solve(int n = 10, int max = 10, int sols_to_show = 0)
{
Solver solver = new Solver("PickingTeams");
Console.WriteLine("n: " + n);
Console.WriteLine("max: " + max);
Console.WriteLine("sols_to_show: " + sols_to_show);
if (n % 2 != 0) {
Console.WriteLine("The number of people (n) must be divisible by 2.");
System.Environment.Exit(1);
}
//
// Data
//
int num_teams = 2;
// Randomize data:
int seed = (int)DateTime.Now.Ticks;
Random generator = new Random(seed);
int[] s = new int[n];
for(int i = 0; i < n; i++) {
s[i] = 1 + generator.Next(max);
if (n <= 100) {
Console.Write(s[i] + " ");
}
}
Console.WriteLine();
Console.WriteLine("\n" + n + " numbers generated\n");
// int[] s = {35, 52, 17, 26, 90, 55, 57, 54, 41, 9, 75, 24, 17, 23, 62, 74, 100, 67, 40, 48, 7, 6, 44, 19, 16, 14, 2, 66, 70, 2, 43, 45, 76, 53, 90, 12, 88, 96, 30, 30, 36, 93, 74, 1, 52, 45, 38, 7, 24, 96, 17, 21, 12, 12, 23, 90, 77, 64, 37, 79, 67, 62, 24, 11, 74, 82, 51, 17, 72, 18, 37, 94, 43, 44, 32, 86, 94, 33, 97, 27, 38, 38, 29, 92, 35, 82, 22, 66, 80, 8, 62, 72, 25, 13, 94, 42, 51, 31, 69, 66};
// n = s.Length;
int the_sum = s.Sum();
int half_sum = (int)the_sum / 2;
Console.WriteLine("sum: " + the_sum + " half_sum: " + half_sum);
IEnumerable<int> NRange = Enumerable.Range(0, n);
IEnumerable<int> Sets = Enumerable.Range(0, num_teams);
//
// Decision variables
//
// To which team (s) do x[i] belong?
IntVar[] x = solver.MakeIntVarArray(n, 0, 1, "x");
IntVar diff = (
(from k in NRange select (s[k]*(x[k] == 0)) ).ToArray().Sum() -
(from k in NRange select (s[k]*(x[k] == 1)) ).ToArray().Sum()
).Abs().Var();
Console.WriteLine("diff.Max(): " + diff.Max());
//
// Constraints
//
// Ensure that there are the same number of people in each team
int n2 = (int)n / 2;
solver.Add((from k in NRange select (x[k]==0)).ToArray().Sum() == n2 );
// solver.Add((from k in NRange select (x[k]==1)).ToArray().Sum() == n2 );
// symmetry breaking: assign first number to team 0
solver.Add(x[0] == 0);
// Odd sum must yield odd diff, even sum yield even diff
IntVar even_odd = solver.MakeIntConst(the_sum % 2);
solver.Add(solver.MakeModuloConstraint(diff, 2, even_odd));
//.........这里部分代码省略.........
示例4: Solve
/**
*
* Solves the divisible by 9 through 1 problem.
* See http://www.hakank.org/google_or_tools/divisible_by_9_through_1.py
*
*/
private static void Solve(int bbase)
{
Solver solver = new Solver("DivisibleBy9Through1");
int m = (int)Math.Pow(bbase,(bbase-1)) - 1;
int n = bbase - 1;
String[] digits_str = {"_","0","1","2","3","4","5","6","7","8","9"};
Console.WriteLine("base: " + bbase);
//
// Decision variables
//
// digits
IntVar[] x = solver.MakeIntVarArray(n, 1, bbase - 1, "x");
// the numbers. t[0] contains the answe
IntVar[] t = solver.MakeIntVarArray(n, 0, m, "t");
//
// Constraints
//
solver.Add(x.AllDifferent());
// Ensure the divisibility of base .. 1
IntVar zero = solver.MakeIntConst(0);
for(int i = 0; i < n; i++) {
int mm = bbase - i - 1;
IntVar[] tt = new IntVar[mm];
for(int j = 0; j < mm; j++) {
tt[j] = x[j];
}
solver.Add(ToNum(tt, t[i], bbase));
MyMod(solver, t[i], solver.MakeIntConst(mm), zero);
}
//
// Search
//
DecisionBuilder db = solver.MakePhase(x,
Solver.INT_VAR_DEFAULT,
Solver.INT_VALUE_DEFAULT);
solver.NewSearch(db);
while (solver.NextSolution()) {
Console.Write("x: ");
for(int i = 0; i < n; i++) {
Console.Write(x[i].Value() + " ");
}
Console.WriteLine("\nt: ");
for(int i = 0; i < n; i++) {
Console.Write(t[i].Value() + " ");
}
Console.WriteLine("\n");
if (bbase != 10) {
Console.Write("Number base 10: " + t[0].Value());
Console.Write(" Base " + bbase + ": ");
for(int i = 0; i < n; i++) {
Console.Write(digits_str[(int)x[i].Value() + 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();
}