本文整理汇总了C#中Solver.MakeMinimize方法的典型用法代码示例。如果您正苦于以下问题:C# Solver.MakeMinimize方法的具体用法?C# Solver.MakeMinimize怎么用?C# Solver.MakeMinimize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Solver
的用法示例。
在下文中一共展示了Solver.MakeMinimize方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Solve
//.........这里部分代码省略.........
}
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)
{
solver.Add(
solver.MakeSumLessOrEqual(contributions_per_slot[slot].ToArray(), 1));
}
// Add minimum start info.
for (int speaker = 0; speaker < number_of_speakers; ++speaker)
{
solver.Add(starts[speaker] >= first_slot);
}
// Creates makespan.
IntVar[] end_times = new IntVar[number_of_speakers];
for (int speaker = 0; speaker < number_of_speakers; speaker++)
{
end_times[speaker] = (starts[speaker] + (durations[speaker] - 1)).Var();
}
IntVar last_slot_var = end_times.Max().VarWithName("last_slot");
// Add trivial bound to objective.
last_slot_var.SetMin(first_slot + sum_of_durations - 1);
// Redundant scheduling constraint.
IntervalVar[] intervals =
solver.MakeFixedDurationIntervalVarArray(starts, durations, "intervals");
DisjunctiveConstraint disjunctive =
solver.MakeDisjunctiveConstraint(intervals, "disjunctive");
solver.Add(disjunctive);
//
// Search
//
List<IntVar> short_talks = new List<IntVar>();
List<IntVar> long_talks = new List<IntVar>();
for (int speaker = 0; speaker < number_of_speakers; ++speaker)
{
if (durations[speaker] == 1)
{
short_talks.Add(starts[speaker]);
}
else
{
long_talks.Add(starts[speaker]);
}
}
OptimizeVar objective_monitor = solver.MakeMinimize(last_slot_var, 1);
DecisionBuilder long_phase =
solver.MakePhase(long_talks.ToArray(),
Solver.CHOOSE_MIN_SIZE_LOWEST_MIN,
Solver.ASSIGN_MIN_VALUE);
DecisionBuilder short_phase =
new FlowAssign(short_talks.ToArray(), first_slot, last_slot_var);
DecisionBuilder obj_phase =
solver.MakePhase(last_slot_var,
Solver.CHOOSE_FIRST_UNBOUND,
Solver.ASSIGN_MIN_VALUE);
DecisionBuilder main_phase =
solver.Compose(long_phase, short_phase, obj_phase);
solver.NewSearch(main_phase, objective_monitor);
while (solver.NextSolution())
{
Console.WriteLine("\nLast used slot: " + (last_slot_var.Value()));
Console.WriteLine("Speakers (start..end):");
for (int s = 0; s < number_of_speakers; s++)
{
long sstart = starts[s].Value();
Console.WriteLine(" - speaker {0,2}: {1,2}..{2,2}", (s + 1),
sstart, (sstart + durations[s] - 1));
}
}
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();
}