本文整理汇总了C#中IntVar.Max方法的典型用法代码示例。如果您正苦于以下问题:C# IntVar.Max方法的具体用法?C# IntVar.Max怎么用?C# IntVar.Max使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IntVar
的用法示例。
在下文中一共展示了IntVar.Max方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
示例2: CheckIntRange
static void CheckIntRange(IntVar var, long vmin, long vmax) {
if (var.Min() != vmin) {
Console.WriteLine("Error: " + var.ToString() +
" does not have the expected min : " + vmin);
error_count_++;
}
if (var.Max() != vmax) {
Console.WriteLine("Error: " + var.ToString() +
" does not have the expected max : " + vmax);
error_count_++;
}
}
示例3: Solve
private static void Solve(int first_slot)
{
Console.WriteLine("----------------------------------------------------");
Solver solver = new Solver("SpeakerScheduling");
// the slots each speaker is available
int[][] speaker_availability = {
new int[] {1,3,4,6,7,10,12,13,14,15,16,18,19,20,21,22,23,24,25,33,34,35,36,37,38,39,40,41,43,44,45,46,47,48,49,50,51,52,54,55,56,57,58,59},
new int[] {1,2,7,8,10,11,16,17,18,21,22,23,24,25,33,34,35,36,37,38,39,40,42,43,44,45,46,47,48,49,52,53,54,55,56,57,58,59,60},
new int[] {5,6,7,10,12,14,16,17,21,22,23,24,33,35,37,38,39,40,41,42,43,44,45,46,51,53,55,56,57,58,59},
new int[] {1,2,3,4,5,6,7,11,13,14,15,16,20,24,25,33,34,35,37,38,39,40,41,43,44,45,46,47,48,49,50,51,52,53,54,55,56,58,59,60},
new int[] {4,7,8,9,16,17,19,20,21,22,23,24,25,33,34,35,36,37,38,39,40,41,42,43,49,50,51,53,55,56,57,58,59,60},
new int[] {4,7,9,11,12,13,14,15,16,17,18,22,23,24,33,34,35,36,38,39,42,44,46,48,49,51,53,54,55,56,57},
new int[] {1,2,3,4,5,6,7,33,34,35,36,37,38,39,40,41,42,54,55,56,57,58,59,60},
new int[] {1,3,11,14,15,16,17,21,22,23,24,25,33,35,36,37,39,40,41,42,43,44,45,47,48,49,51,52,53,54,55,56,57,58,59,60},
new int[] {1,2,3,4,5,7,8,9,10,11,13,18,19,20,21,22,23,24,25,33,34,35,36,37,38,39,40,41,42,43,44,45,46,50,51,52,53,54,55,56,57,59,60},
new int[] {24,33,34,35,36,37,38,39,40,41,42,43,45,49,50,51,52,53,54,55,56,57,58,59,60},
new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,16,17,18,19,20,22,23,24,25,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,50,51,52,53,55,56,57,58,59,60},
new int[] {3,4,5,6,13,15,16,17,18,19,21,22,24,25,33,34,35,36,37,39,40,41,42,43,44,45,47,52,53,55,57,58,59,60},
new int[] {4,5,6,8,11,12,13,14,17,19,20,22,23,24,25,33,34,35,36,37,39,40,41,42,43,47,48,49,50,51,52,55,56,57},
new int[] {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60},
new int[] {12,25,33,35,36,37,39,41,42,43,48,51,52,53,54,57,59,60},
new int[] {4,8,16,17,19,23,25,33,34,35,37,41,44,45,47,48,49,50},
new int[] {3,23,24,25,33,35,36,37,38,39,40,42,43,44,49,50,53,54,55,56,57,58,60},
new int[] {7,13,19,20,22,23,24,25,33,34,35,38,40,41,42,44,45,46,47,48,49,52,53,54,58,59,60}
};
// how long each talk lasts for each speaker
int[] durations = { 1, 2, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
int sum_of_durations = durations.Sum();
int number_of_speakers = durations.Length;
// calculate the total number of slots (maximum in the availability array)
// (and add the max durations)
int last_slot = (from s in Enumerable.Range(0, number_of_speakers)
select speaker_availability[s].Max()).Max();
Console.WriteLine(
"Scheduling {0} speakers, for a total of {1} slots, during [{2}..{3}]",
number_of_speakers, sum_of_durations, first_slot, last_slot);
// Start variable for all talks.
IntVar[] starts = new IntVar[number_of_speakers];
// We store the possible starts for all talks filtered from the
// duration and the speaker availability.
int[][] possible_starts = new int[number_of_speakers][];
for (int speaker = 0; speaker < number_of_speakers; ++speaker)
{
int duration = durations[speaker];
// Let's filter the possible starts.
List<int> filtered_starts = new List<int>();
int availability = speaker_availability[speaker].Length;
for (int index = 0; index < availability; ++index)
{
bool ok = true;
int slot = speaker_availability[speaker][index];
if (slot < first_slot)
{
continue;
}
for (int offset = 1; offset < duration; ++offset)
{
if (index + offset >= availability ||
speaker_availability[speaker][index + offset] != slot + offset)
{
// discontinuity.
ok = false;
break;
}
}
if (ok)
{
filtered_starts.Add(slot);
}
possible_starts[speaker] = filtered_starts.ToArray();
}
starts[speaker] =
solver.MakeIntVar(possible_starts[speaker], "start[" + speaker + "]");
}
List<IntVar>[] contributions_per_slot =
new List<IntVar>[last_slot + 1];
for (int slot = first_slot; slot <= last_slot; ++slot)
{
contributions_per_slot[slot] = new List<IntVar>();
}
for (int speaker = 0; speaker < number_of_speakers; ++speaker)
{
int duration = durations[speaker];
IntVar start_var = starts[speaker];
foreach (int start in possible_starts[speaker])
{
for (int offset = 0; offset < duration; ++offset)
{
contributions_per_slot[start + offset].Add(start_var.IsEqual(start));
}
}
}
// Force the schedule to be consistent.
for (int slot = first_slot; slot <= last_slot; ++slot)
//.........这里部分代码省略.........
示例4: Solve
/**
*
* Moving furnitures (scheduling) problem in Google CP Solver.
*
* Marriott & Stukey: 'Programming with constraints', page 112f
*
* Also see http://www.hakank.org/or-tools/furniture_moving.py
*
*/
private static void Solve()
{
Solver solver = new Solver("FurnitureMovingIntervals");
const int n = 4;
int[] durations = {30,10,15,15};
int[] demand = {3, 1, 3, 2};
const int upper_limit = 160;
const int max_num_workers = 5;
//
// Decision variables
//
IntervalVar[] tasks = new IntervalVar[n];
for (int i = 0; i < n; ++i)
{
tasks[i] = solver.MakeFixedDurationIntervalVar(0,
upper_limit - durations[i],
durations[i],
false,
"task_" + i);
}
// Fillers that span the whole resource and limit the available
// number of workers.
IntervalVar[] fillers = new IntervalVar[max_num_workers];
for (int i = 0; i < max_num_workers; ++i)
{
fillers[i] = solver.MakeFixedDurationIntervalVar(0,
0,
upper_limit,
true,
"filler_" + i);
}
// Number of needed resources, to be minimized or constrained.
IntVar num_workers = solver.MakeIntVar(0, max_num_workers, "num_workers");
// Links fillers and num_workers.
for (int i = 0; i < max_num_workers; ++i)
{
solver.Add((num_workers > i) + fillers[i].PerformedExpr() == 1);
}
// Creates makespan.
IntVar[] ends = new IntVar[n];
for (int i = 0; i < n; ++i)
{
ends[i] = tasks[i].EndExpr().Var();
}
IntVar end_time = ends.Max().VarWithName("end_time");
//
// Constraints
//
IntervalVar[] all_tasks = new IntervalVar[n + max_num_workers];
int[] all_demands = new int[n + max_num_workers];
for (int i = 0; i < n; ++i)
{
all_tasks[i] = tasks[i];
all_demands[i] = demand[i];
}
for (int i = 0; i < max_num_workers; ++i)
{
all_tasks[i + n] = fillers[i];
all_demands[i + n] = 1;
}
solver.Add(all_tasks.Cumulative(all_demands, max_num_workers, "workers"));
//
// Some extra constraints to play with
//
// all tasks must end within an hour
// solver.Add(end_time <= 60);
// All tasks should start at time 0
// for(int i = 0; i < n; i++) {
// solver.Add(tasks[i].StartAt(0));
// }
// limitation of the number of people
// solver.Add(num_workers <= 3);
solver.Add(num_workers <= 4);
//
// Objective
//
// OptimizeVar obj = num_workers.Minimize(1);
OptimizeVar obj = end_time.Minimize(1);
//.........这里部分代码省略.........