Esempio n. 1
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.");
        }
        static void Main(string[] args)
        {
            Program obj = new Program();

            obj.Readfile(@"C:\binpackdata.txt");

            obj.nbCourses = obj.credits.Length;
            Solver solver = new Solver("BinPacking");

            IntVar[] x = new IntVar[obj.nbCourses];
            IntVar[] loadVars = new IntVar[obj.nbPeriods];

            for (int i = 0; i < obj.nbCourses; i++)
                x[i] = solver.MakeIntVar(0, obj.nbPeriods - 1, "x" + i);

            for (int i = 0; i < obj.nbPeriods; i++)
                loadVars[i] = solver.MakeIntVar(0, obj.credits.Sum(), "loadVars" + i);

            //-------------------post of the constraints--------------
            obj.Pack(solver, x, obj.credits, loadVars);

            foreach (Tuple<int, int> t in obj.prereqTupleArr)
                solver.Add(x[t.Item1] < x[t.Item2]);

            //-------------------------Objective---------------------------
            IntVar objectiveVar = solver.MakeMax(loadVars).Var();
            OptimizeVar objective = solver.MakeMinimize(objectiveVar, 1);

            //------------start the search and optimization-----------
            DecisionBuilder db = solver.MakePhase(x, Solver.CHOOSE_MIN_SIZE_LOWEST_MIN, Solver.INT_VALUE_DEFAULT);
            SearchMonitor searchLog = solver.MakeSearchLog(100000, objectiveVar);
            solver.NewSearch(db, objective, searchLog);

            while (solver.NextSolution())
            {
                Console.WriteLine(">> Objective: " + objectiveVar.Value());
            }

            solver.EndSearch();
        }