public static DisjunctiveConstraint Disjunctive(this IntervalVar[] vars,
                                                        String name)
        {
            Solver solver = GetSolver(vars);

            return(solver.MakeDisjunctiveConstraint(vars, name));
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            InitTaskList();
            int taskCount = GetTaskCount();

            Solver solver = new Solver("ResourceConstraintScheduling");

            IntervalVar[] tasks = new IntervalVar[taskCount];
            IntVar[] taskChoosed = new IntVar[taskCount];
            IntVar[] makeSpan = new IntVar[GetEndTaskCount()];

            int endJobCounter = 0;
            foreach (Job j in myJobList) {
              IntVar[] tmp = new IntVar[j.AlternativeTasks.Count];
              int i = 0;
              foreach (Task t in j.AlternativeTasks) {
            long ti = taskIndexes[t.Name];
            taskChoosed[ti] = solver.MakeIntVar(0, 1, t.Name + "_choose");
            tmp[i++] = taskChoosed[ti];
            tasks[ti] = solver.MakeFixedDurationIntervalVar(
            0, 100000, t.Duration, false, t.Name + "_interval");
            if (j.Successor == null)
              makeSpan[endJobCounter++] = tasks[ti].EndExpr().Var();
            if (!tasksToEquipment.ContainsKey(t.Equipment))
              tasksToEquipment[t.Equipment] = new List<IntervalVar>();
            tasksToEquipment[t.Equipment].Add(tasks[ti]);
              }
              solver.Add(IntVarArrayHelper.Sum(tmp) == 1);
            }

            List<SequenceVar> all_seq = new List<SequenceVar>();
            foreach (KeyValuePair<long, List<IntervalVar>> pair in tasksToEquipment) {
              DisjunctiveConstraint dc = solver.MakeDisjunctiveConstraint(
              pair.Value.ToArray(), pair.Key.ToString());
              solver.Add(dc);
              all_seq.Add(dc.SequenceVar());
            }

            IntVar objective_var = solver.MakeMax(makeSpan).Var();
            OptimizeVar objective_monitor = solver.MakeMinimize(objective_var, 1);

            DecisionBuilder sequence_phase =
            solver.MakePhase(all_seq.ToArray(), Solver.SEQUENCE_DEFAULT);
            DecisionBuilder objective_phase =
            solver.MakePhase(objective_var, Solver.CHOOSE_FIRST_UNBOUND,
                         Solver.ASSIGN_MIN_VALUE);
            DecisionBuilder main_phase = solver.Compose(sequence_phase, objective_phase);

            const int kLogFrequency = 1000000;
            SearchMonitor search_log =
            solver.MakeSearchLog(kLogFrequency, objective_monitor);

            SolutionCollector collector = solver.MakeLastSolutionCollector();
            collector.Add(all_seq.ToArray());
            collector.AddObjective(objective_var);

            if (solver.Solve(main_phase, search_log, objective_monitor, null, collector))
              Console.Out.WriteLine("Optimal solution = " + collector.ObjectiveValue(0));
            else
              Console.Out.WriteLine("No solution.");
        }