Example #1
0
        public static void Main(String[] args)
        {
            try {
                cp   = new CP();
                cost = cp.NumExpr();
                List <IIntervalVar> allTasks = new List <IIntervalVar>();
                List <IIntervalVar> joeTasks = new List <IIntervalVar>();
                List <IIntervalVar> jimTasks = new List <IIntervalVar>();

                List <Int32> joeLocations = new List <Int32>();
                List <Int32> jimLocations = new List <Int32>();

                MakeHouse(allTasks, joeTasks, jimTasks, joeLocations, jimLocations, 0, 0, 120, 100.0);
                MakeHouse(allTasks, joeTasks, jimTasks, joeLocations, jimLocations, 1, 0, 212, 100.0);
                MakeHouse(allTasks, joeTasks, jimTasks, joeLocations, jimLocations, 2, 151, 304, 100.0);
                MakeHouse(allTasks, joeTasks, jimTasks, joeLocations, jimLocations, 3, 59, 181, 200.0);
                MakeHouse(allTasks, joeTasks, jimTasks, joeLocations, jimLocations, 4, 243, 425, 100.0);

                ITransitionDistance tt = cp.TransitionDistance(5);
                for (int i = 0; i < 5; ++i)
                {
                    for (int j = 0; j < 5; ++j)
                    {
                        tt.SetValue(i, j, Math.Abs(i - j));
                    }
                }

                IIntervalSequenceVar joe = cp.IntervalSequenceVar(joeTasks.ToArray(), joeLocations.ToArray(), "Joe");
                IIntervalSequenceVar jim = cp.IntervalSequenceVar(jimTasks.ToArray(), jimLocations.ToArray(), "Jim");

                cp.Add(cp.NoOverlap(joe, tt));
                cp.Add(cp.NoOverlap(jim, tt));

                cp.Add(cp.Minimize(cost));


                cp.SetParameter(CP.IntParam.FailLimit, 50000);
                /* EXTRACTING THE MODEL AND SOLVING. */
                if (cp.Solve())
                {
                    for (int i = 0; i < allTasks.Count; ++i)
                    {
                        Console.WriteLine(cp.GetDomain(allTasks[i]));
                    }
                }
                else
                {
                    Console.WriteLine("No solution found.");
                }
            } catch (ILOG.Concert.Exception e) {
                Console.WriteLine(" ERROR: " + e);
            }
        }
Example #2
0
        public static void Main(String[] args)
        {
            int nbHouses = 5;
            int deadline = 318;

            skill = cp.IntExpr();
            List <IIntervalVar> allTasks = new List <IIntervalVar>();

            List <IIntervalVar>[] workerTasks = new List <IIntervalVar> [nbWorkers];
            for (int w = 0; w < nbWorkers; w++)
            {
                workerTasks[w] = new List <IIntervalVar>();
            }

            for (int h = 0; h < nbHouses; h++)
            {
                MakeHouse(allTasks, workerTasks, h, deadline);
            }

            for (int w = 0; w < nbWorkers; w++)
            {
                IIntervalSequenceVar seq = cp.IntervalSequenceVar(workerTasks[w].ToArray(), workerNames[w]);
                cp.Add(cp.NoOverlap(seq));
            }

            cp.Add(cp.Maximize(skill));

            /* EXTRACTING THE MODEL AND SOLVING. */
            cp.SetParameter(CP.IntParam.FailLimit, 10000);
            if (cp.Solve())
            {
                Console.WriteLine("Solution with objective " + cp.ObjValue + ":");
                for (int i = 0; i < allTasks.Count; i++)
                {
                    IIntervalVar var = (IIntervalVar)allTasks[i];
                    if (cp.IsPresent(allTasks[i]))
                    {
                        Console.WriteLine(cp.GetDomain((IIntervalVar)allTasks[i]));
                    }
                }
            }
            else
            {
                Console.WriteLine("No solution found. ");
            }
        }
Example #3
0
        public static void Main(String[] args)
        {
            String filename = "../../../../examples/data/flowshop_default.data";
            int failLimit = 10000;
            int nbJobs, nbMachines;

            if (args.Length > 0)
            filename = args[0];
            if (args.Length > 1)
            failLimit = Convert.ToInt32(args[1]);

            CP cp = new CP();

            DataReader data = new DataReader(filename);

            nbJobs = data.Next();
            nbMachines = data.Next();

            List<IIntExpr> ends = new List<IIntExpr>();
            List<IIntervalVar>[] machines = new List<IIntervalVar>[nbMachines];
            for (int j = 0; j < nbMachines; j++)
            machines[j] = new List<IIntervalVar>();
            for (int i = 0; i < nbJobs; i++)
            {
            IIntervalVar prec = cp.IntervalVar();
            for (int j = 0; j < nbMachines; j++)
            {
                int d = data.Next();
                IIntervalVar ti = cp.IntervalVar(d);
                machines[j].Add(ti);
                if (j > 0)
                {
                    cp.Add(cp.EndBeforeStart(prec, ti));
                }
                prec = ti;
            }
            ends.Add(cp.EndOf(prec));
            }

            IIntervalSequenceVar[] seqs = new IIntervalSequenceVar[nbMachines];
            for (int j = 0; j < nbMachines; j++)
            {
            seqs[j] = cp.IntervalSequenceVar(machines[j].ToArray());
            cp.Add(cp.NoOverlap(seqs[j]));
            if (0 < j)
            {
                cp.Add(cp.SameSequence(seqs[0], seqs[j]));
            }
            }

            IObjective objective = cp.Minimize(cp.Max(ends.ToArray()));
            cp.Add(objective);

            cp.SetParameter(CP.IntParam.FailLimit, failLimit);
            Console.WriteLine("Instance \t: " + filename);
            if (cp.Solve())
            {
            Console.WriteLine("Makespan \t: " + cp.ObjValue);
            }
            else
            {
            Console.WriteLine("No solution found.");
            }
        }
Example #4
0
        public static void Main(String[] args)
        {
            String filename = "../../../../examples/data/flowshop_default.data";
            int    failLimit = 10000;
            int    nbJobs, nbMachines;

            if (args.Length > 0)
            {
                filename = args[0];
            }
            if (args.Length > 1)
            {
                failLimit = Convert.ToInt32(args[1]);
            }

            CP cp = new CP();

            DataReader data = new DataReader(filename);

            nbJobs     = data.Next();
            nbMachines = data.Next();

            List <IIntExpr> ends = new List <IIntExpr>();

            List <IIntervalVar>[] machines = new List <IIntervalVar> [nbMachines];
            for (int j = 0; j < nbMachines; j++)
            {
                machines[j] = new List <IIntervalVar>();
            }
            for (int i = 0; i < nbJobs; i++)
            {
                IIntervalVar prec = cp.IntervalVar();
                for (int j = 0; j < nbMachines; j++)
                {
                    int          d  = data.Next();
                    IIntervalVar ti = cp.IntervalVar(d);
                    machines[j].Add(ti);
                    if (j > 0)
                    {
                        cp.Add(cp.EndBeforeStart(prec, ti));
                    }
                    prec = ti;
                }
                ends.Add(cp.EndOf(prec));
            }

            IIntervalSequenceVar[] seqs = new IIntervalSequenceVar[nbMachines];
            for (int j = 0; j < nbMachines; j++)
            {
                seqs[j] = cp.IntervalSequenceVar(machines[j].ToArray());
                cp.Add(cp.NoOverlap(seqs[j]));
                if (0 < j)
                {
                    cp.Add(cp.SameSequence(seqs[0], seqs[j]));
                }
            }

            IObjective objective = cp.Minimize(cp.Max(ends.ToArray()));

            cp.Add(objective);

            cp.SetParameter(CP.IntParam.FailLimit, failLimit);
            Console.WriteLine("Instance \t: " + filename);
            if (cp.Solve())
            {
                Console.WriteLine("Makespan \t: " + cp.ObjValue);
            }
            else
            {
                Console.WriteLine("No solution found.");
            }
        }
        public static void Main(String[] args)
        {
            String filename  = "../../../../examples/data/stochastic_jobshop_default.data";
            int    failLimit = 250000;

            if (args.Length > 0)
            {
                filename = args[0];
            }
            if (args.Length > 1)
            {
                failLimit = Convert.ToInt32(args[1]);
            }

            // Data reading
            DataReader data = new DataReader(filename);
            int        nbJobs, nbMachines, nbScenarios;

            nbJobs      = data.Next();
            nbMachines  = data.Next();
            nbScenarios = data.Next();
            int i, j, k;

            // machines[i][j]: machine used by jth operation of job i
            int[][] machines = new int[nbJobs][];
            for (i = 0; i < nbJobs; i++)
            {
                machines[i] = new int[nbMachines];
                for (j = 0; j < nbMachines; j++)
                {
                    machines[i][j] = data.Next();
                }
            }
            // durations[k][i][j]: duration of jth operation of job i in scenario k
            int[][][] durations = new int[nbScenarios][][];
            for (k = 0; k < nbScenarios; k++)
            {
                durations[k] = new int[nbJobs][];
                for (i = 0; i < nbJobs; i++)
                {
                    durations[k][i] = new int[nbMachines];
                    for (j = 0; j < nbMachines; j++)
                    {
                        durations[k][i][j] = data.Next();
                    }
                }
            }

            CP cp = new CP();

            IIntervalSequenceVar[] refSequences = new IIntervalSequenceVar[nbMachines];
            IIntExpr sumMakespan = cp.IntExpr();

            for (k = 0; k < nbScenarios; k++)
            {
                IIntervalSequenceVar[] scenarioSequences = new IIntervalSequenceVar[nbMachines];
                IIntExpr scenarioMakespan =
                    MakeScenarioSubmodel(cp, nbJobs, nbMachines,
                                         machines, durations[k],
                                         scenarioSequences);
                // Objective function is aggregated
                sumMakespan = cp.Sum(sumMakespan, scenarioMakespan);
                // For each machine, a sameSequence constraint is posted across all scenarios
                if (0 == k)
                {
                    refSequences = scenarioSequences;
                }
                else
                {
                    for (j = 0; j < nbMachines; j++)
                    {
                        cp.Add(cp.SameSequence(refSequences[j], scenarioSequences[j]));
                    }
                }
            }
            // Objective function is expected makespan
            INumExpr   expectedMakespan = cp.Quot(sumMakespan, nbScenarios);
            IObjective objective        = cp.Minimize(expectedMakespan);

            cp.Add(objective);
            cp.SetParameter(CP.IntParam.FailLimit, failLimit);
            cp.SetParameter(CP.IntParam.LogPeriod, 1000000);
            Console.WriteLine("Instance \t: " + filename);
            if (cp.Solve())
            {
                Console.WriteLine("Expected makespan \t: " + cp.ObjValue);
                for (j = 0; j < nbMachines; ++j)
                {
                    IIntervalSequenceVar s = refSequences[j];
                    Console.Write(s.Name + ":\t");
                    IIntervalVar op = cp.GetFirst(s);
                    for (; !op.Equals(cp.GetLast(s)); op = cp.GetNext(s, op))
                    {
                        Console.Write(op.Name + "\t");
                    }
                    Console.WriteLine(op.Name + "\t");
                }
            }
            else
            {
                Console.WriteLine("No solution found.");
            }
        }
Example #6
0
        public static void Main(String[] args)
        {
            try
            {
                CP cp = new CP();

                ITransitionDistance setup1 = cp.TransitionDistance(NbTypes);
                ITransitionDistance setup2 = cp.TransitionDistance(NbTypes);
                int i, j;
                for (i = 0; i < NbTypes; ++i)
                {
                    for (j = 0; j < NbTypes; ++j)
                    {
                        setup1.SetValue(i, j, SetupM1[NbTypes * i + j]);
                        setup2.SetValue(i, j, SetupM2[NbTypes * i + j]);
                    }
                }
                int[]          tp = new int[NbTasks];
                IIntervalVar[] a  = new IIntervalVar[NbTasks];
                IIntervalVar[] a1 = new IIntervalVar[NbTasks];
                IIntervalVar[] a2 = new IIntervalVar[NbTasks];

                String name;
                for (i = 0; i < NbTasks; ++i)
                {
                    int type = TaskType[i];
                    int d    = TaskDur[i];
                    tp[i] = type;
                    name  = "A" + i + "_TP" + type;
                    a[i]  = cp.IntervalVar(d, name);
                    IIntervalVar[] alt = new IIntervalVar[2];
                    name  = "A" + i + "_M1_TP" + type;
                    a1[i] = cp.IntervalVar(name);
                    a1[i].SetOptional();
                    alt[0] = a1[i];
                    name   = "A" + i + "_M2_TP" + type;
                    a2[i]  = cp.IntervalVar(name);
                    a2[i].SetOptional();
                    alt[1] = a2[i];
                    cp.Add(cp.Alternative(a[i], alt));
                }

                IIntervalSequenceVar s1 = cp.IntervalSequenceVar(a1, tp);
                IIntervalSequenceVar s2 = cp.IntervalSequenceVar(a2, tp);
                cp.Add(cp.NoOverlap(s1, setup1, true));
                cp.Add(cp.NoOverlap(s2, setup2, true));

                IIntExpr nbLongSetups = cp.IntExpr();
                for (i = 0; i < NbTasks; ++i)
                {
                    int   tpi          = TaskType[i];
                    int[] isLongSetup1 = new int[NbTypes + 1];
                    int[] isLongSetup2 = new int[NbTypes + 1];
                    for (j = 0; j < NbTypes; ++j)
                    {
                        isLongSetup1[j] = (30 <= SetupM1[NbTypes * tpi + j]) ? 1 : 0;
                        isLongSetup2[j] = (30 <= SetupM2[NbTypes * tpi + j]) ? 1 : 0;
                    }
                    isLongSetup1[NbTypes] = 0; // Last on resource or resource not selected
                    isLongSetup2[NbTypes] = 0; // Last on resource or resource not selected
                    nbLongSetups          = cp.Sum(nbLongSetups,
                                                   cp.Element(isLongSetup1, cp.TypeOfNext(s1, a1[i], NbTypes, NbTypes)));
                    nbLongSetups = cp.Sum(nbLongSetups,
                                          cp.Element(isLongSetup2, cp.TypeOfNext(s2, a2[i], NbTypes, NbTypes)));
                }
                cp.Add(cp.Minimize(nbLongSetups));

                cp.SetParameter(CP.IntParam.FailLimit, 100000);
                cp.SetParameter(CP.IntParam.LogPeriod, 10000);
                if (cp.Solve())
                {
                    Console.WriteLine("Machine 1: ");
                    IIntervalVar x;
                    for (x = cp.GetFirst(s1); !x.Equals(cp.GetLast(s1)); x = cp.GetNext(s1, x))
                    {
                        Console.WriteLine(cp.GetDomain(x));
                    }
                    Console.WriteLine(cp.GetDomain(x));
                    Console.WriteLine("Machine 2: ");
                    for (x = cp.GetFirst(s2); !x.Equals(cp.GetLast(s2)); x = cp.GetNext(s2, x))
                    {
                        Console.WriteLine(cp.GetDomain(x));
                    }
                    Console.WriteLine(cp.GetDomain(x));
                    Console.WriteLine("Number of long transition times \t: " + cp.ObjValue);
                }
                else
                {
                    Console.WriteLine("No solution found.");
                }
            }
            catch (IloException e)
            {
                Console.WriteLine("Error: " + e);
            }
        }
Example #7
0
        public static void Main(String[] args)
        {
            try
            {
                CP cp = new CP();
                ITransitionDistance setup1 = cp.TransitionDistance(NbTypes);
                ITransitionDistance setup2 = cp.TransitionDistance(NbTypes);
                int i, j;
                for (i = 0; i < NbTypes; ++i)
                {
                    for (j = 0; j < NbTypes; ++j)
                    {
                        int d1 = SetupM1[NbTypes * i + j];
                        if (d1 < 0)
                        {
                            d1 = CP.IntervalMax; // Forbidden transition
                        }
                        setup1.SetValue(i, j, d1);
                        int d2 = SetupM2[NbTypes * i + j];
                        if (d2 < 0)
                        {
                            d2 = CP.IntervalMax; // Forbidden transition
                        }
                        setup2.SetValue(i, j, d2);
                    }
                }
                int[]          tp   = new int[NbTasks];
                IIntervalVar[] a    = new IIntervalVar[NbTasks];
                IIntervalVar[] a1   = new IIntervalVar[NbTasks];
                IIntervalVar[] a2   = new IIntervalVar[NbTasks];
                IIntExpr[]     ends = new IIntExpr[NbTasks];

                String name;
                for (i = 0; i < NbTasks; ++i)
                {
                    int type = TaskType[i];
                    int d1   = TaskDurM1[i];
                    int d2   = TaskDurM2[i];
                    tp[i] = type;
                    name  = "A" + i + "_TP" + type;
                    a[i]  = cp.IntervalVar(name);
                    IIntervalVar[] alt = new IIntervalVar[2];
                    name  = "A" + i + "_M1_TP" + type;
                    a1[i] = cp.IntervalVar(d1, name);
                    a1[i].SetOptional();
                    alt[0] = a1[i];
                    name   = "A" + i + "_M2_TP" + type;
                    a2[i]  = cp.IntervalVar(d2, name);
                    a2[i].SetOptional();
                    alt[1] = a2[i];
                    cp.Add(cp.Alternative(a[i], alt));
                    ends[i] = cp.EndOf(a[i]);
                }

                IIntervalSequenceVar s1 = cp.IntervalSequenceVar(a1, tp);
                IIntervalSequenceVar s2 = cp.IntervalSequenceVar(a2, tp);
                cp.Add(cp.NoOverlap(s1, setup1, true));
                cp.Add(cp.NoOverlap(s2, setup2, true));
                cp.Add(cp.Minimize(cp.Max(ends)));

                cp.SetParameter(CP.IntParam.FailLimit, 100000);
                cp.SetParameter(CP.IntParam.LogPeriod, 10000);
                if (cp.Solve())
                {
                    Console.WriteLine("Machine 1: ");
                    IIntervalVar x;
                    for (x = cp.GetFirst(s1); !x.Equals(cp.GetLast(s1)); x = cp.GetNext(s1, x))
                    {
                        Console.WriteLine(cp.GetDomain(x));
                    }
                    Console.WriteLine(cp.GetDomain(x));
                    Console.WriteLine("Machine 2: ");
                    for (x = cp.GetFirst(s2); !x.Equals(cp.GetLast(s2)); x = cp.GetNext(s2, x))
                    {
                        Console.WriteLine(cp.GetDomain(x));
                    }
                    Console.WriteLine(cp.GetDomain(x));
                    Console.WriteLine("Makespan \t: " + cp.ObjValue);
                }
                else
                {
                    Console.WriteLine("No solution found.");
                }
            }
            catch (IloException e)
            {
                Console.WriteLine("Error: " + e);
            }
        }
Example #8
0
        static void Main(string[] args)
        {
            int D = 5;
            int W = 5;
            int H = 3;
            int G = 2;
            int T = 12;

            int[] k_g  = new int[] { 2, 2 };
            int   ALLK = 4;

            int[] cc_d     = new int[] { 2, 1, 2, 1, 2 };
            int[] ave      = new int[] { 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1 };
            int[] dur      = new int[] { 1, 2, 1, 1, 2 };
            int[] prf_D    = new int[] { 2, 1, 1, 1, 100 };
            int[] indexg_d = new int[] { 0, 1, 1, 1, 0 };

            CP roster = new CP();

            // intern availbility
            INumToNumStepFunction resource_AveIntern = roster.NumToNumStepFunction(0, T, 100, "AvailibilityOfIntern");

            for (int t = 0; t < T; t++)
            {
                if (ave[t] == 0)
                {
                    resource_AveIntern.SetValue(t, t + 1, 0);
                }
            }



            // discipline
            IIntervalVar[]     discipline_d  = new IIntervalVar[D];
            ICumulFunctionExpr hospitalNotRR = roster.CumulFunctionExpr();

            for (int d = 0; d < D; d++)
            {
                discipline_d[d]           = roster.IntervalVar();
                discipline_d[d].EndMax    = T;
                discipline_d[d].EndMin    = dur[d];
                discipline_d[d].LengthMax = dur[d];
                discipline_d[d].LengthMin = dur[d];
                discipline_d[d].SizeMax   = dur[d];
                discipline_d[d].SizeMin   = dur[d];
                discipline_d[d].StartMax  = T;
                discipline_d[d].StartMin  = 0;
                discipline_d[d].SetIntensity(resource_AveIntern, 100);
                hospitalNotRR.Add(roster.Pulse(discipline_d[d], 1));
                discipline_d[d].SetOptional();
            }
            IIntervalSequenceVar dis = roster.IntervalSequenceVar(discipline_d);

            roster.Add(roster.Ge(roster.PresenceOf(discipline_d[1]), roster.PresenceOf(discipline_d[4])));
            roster.Add(roster.Before(dis, discipline_d[1], discipline_d[4]));

            roster.Add(roster.NoOverlap(discipline_d));
            // desciplien  for not renewable resources
            IIntVar[] height_t = new IIntVar[T];
            for (int t = 0; t < T; t++)
            {
                height_t[t] = roster.IntVar(0, 1);
            }

            INumToNumSegmentFunction piecewise = roster.NumToNumSegmentFunction(new double[] { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11 },
                                                                                new double[] { 10, 0, 10, 1, 10, 2, 10, 3, 10, 4, 10, 5, 10, 6, 10, 7, 10, 8, 10, 9, 10, 10, 10, 11 });



            INumExpr rc = roster.NumExpr();

            for (int d = 0; d < D; d++)
            {
                rc = roster.Sum(rc, roster.StartEval(discipline_d[d], piecewise, 0));
            }


            for (int t = 0; t < T; t++)
            {
            }

            IIntervalVar[] disciplineNR_d = new IIntervalVar[D];

            for (int d = 0; d < D; d++)
            {
                disciplineNR_d[d]           = roster.IntervalVar();
                disciplineNR_d[d].EndMax    = T;
                disciplineNR_d[d].EndMin    = T;
                disciplineNR_d[d].LengthMax = T;
                disciplineNR_d[d].LengthMin = T;
                disciplineNR_d[d].SizeMax   = T;
                disciplineNR_d[d].SizeMin   = T;
                disciplineNR_d[d].StartMax  = T;
                disciplineNR_d[d].StartMin  = 0;
                disciplineNR_d[d].SetOptional();
                roster.IfThen(roster.PresenceOf(discipline_d[d]), roster.PresenceOf(disciplineNR_d[d]));
            }
            //roster.Add(roster.IfThen(roster.PresenceOf(discipline_d[4]), roster.And(roster.Le(roster.EndOf(discipline_d[4]), roster.StartOf(discipline_d[0])),roster.PresenceOf(discipline_d[0]))));
            //roster.Add(roster.StartBeforeEnd(discipline_d[4],discipline_d[0]));
            // hospital changes
            //ICumulFunctionExpr[] hospital

            //for (int d = 0; d < D; d++)
            //{
            //    roster.IfThen(roster.PresenceOf(disciplineNR_d[d]),);
            //}



            // hospital assignment
            IIntervalVar[][] Hospital_dh = new IIntervalVar[D][];
            for (int d = 0; d < D; d++)
            {
                Hospital_dh[d] = new IIntervalVar[H];
                for (int h = 0; h < H; h++)
                {
                    Hospital_dh[d][h]           = roster.IntervalVar();
                    Hospital_dh[d][h].EndMax    = T;
                    Hospital_dh[d][h].EndMin    = dur[d];
                    Hospital_dh[d][h].LengthMax = dur[d];
                    Hospital_dh[d][h].LengthMin = dur[d];
                    Hospital_dh[d][h].SizeMax   = dur[d];
                    Hospital_dh[d][h].SizeMin   = dur[d];
                    Hospital_dh[d][h].StartMax  = T;
                    Hospital_dh[d][h].StartMin  = 0;

                    Hospital_dh[d][h].SetOptional();
                    if (h == 0 && (d != 4))
                    {
                        Hospital_dh[d][h].SetAbsent();
                    }
                    if (h == 1 && (d == 4))
                    {
                        Hospital_dh[d][h].SetAbsent();
                    }
                    if (h == 2 && (d == 4))
                    {
                        Hospital_dh[d][h].SetAbsent();
                    }
                }
                roster.Add(roster.Alternative(discipline_d[d], Hospital_dh[d]));
            }

            IIntervalVar[] disHospSetUp_dh = new IIntervalVar[D * H];
            int[]          type            = new int[D * H];
            for (int dh = 0; dh < D * H; dh++)
            {
                int dIndex = dh % D;
                int hIndex = dh / D;
                disHospSetUp_dh[dh] = roster.IntervalVar("DsiHosp" + "[" + dIndex + "][" + hIndex + "]");
                type[dh]            = hIndex;
                disHospSetUp_dh[dh].SetOptional();
                disHospSetUp_dh[dh] = Hospital_dh[dIndex][hIndex];
            }
            // changes
            IIntervalSequenceVar cc = roster.IntervalSequenceVar(disHospSetUp_dh, type);

            roster.NoOverlap(cc);
            IIntVar[][] change_dD = new IIntVar[D][];
            for (int d = 0; d < D; d++)
            {
                change_dD[d] = new IIntVar[D];
                for (int dd = 0; dd < D; dd++)
                {
                    change_dD[d][dd] = roster.IntVar(0, 1, "change_dD[" + d + "][" + dd + "]");
                }
            }
            IIntVar[] change_d = new IIntVar[D];
            for (int d = 0; d < D; d++)
            {
                change_d[d] = roster.IntVar(0, 1, "change_d[" + d + "]");
            }
            for (int dh = 0; dh < D * H; dh++)
            {
                int      dIndex = dh % D;
                int      hIndex = dh / D;
                IIntExpr chngD  = roster.IntExpr();
                chngD = roster.Sum(chngD, change_d[dIndex]);
                roster.Add(roster.IfThen(roster.And(roster.PresenceOf(disHospSetUp_dh[dh]), roster.Neq(roster.TypeOfNext(cc, disHospSetUp_dh[dh], hIndex, hIndex), hIndex)), roster.Eq(chngD, 1)));
                for (int ddh = 0; ddh < D * H; ddh++)
                {
                    int ddIndex = ddh % D;
                    int hhIndex = ddh / D;
                    if (hhIndex == hIndex || dIndex == ddIndex)
                    {
                        continue;
                    }
                }
            }



            //IIntVar[][] y_dD = new IIntVar[D][];
            //for (int d = 0; d < D; d++)
            //{
            //    y_dD[d] = new IIntVar[D];
            //    for (int dd= 0; dd < D; dd++)
            //    {
            //        y_dD[d][dd] = roster.IntVar(0,1);
            //        if (d == dd)
            //        {
            //            y_dD[d][dd] = roster.IntVar(0, 0);
            //        }
            //    }
            //}
            //for (int d = 0; d < D; d++)
            //{
            //    for (int dd = 0; dd < D; dd++)
            //    {
            //        if (d != dd)
            //        {
            //            for (int h = 0; h < H; h++)
            //            {
            //                for (int hh = 0; hh < H; hh++)
            //                {
            //                    if (d != dd && h != hh && true)
            //                    {
            //                        IIntExpr yyy = roster.IntExpr();
            //                        yyy = roster.Sum(yyy,roster.Prod(T,y_dD[d][dd]));
            //                        yyy = roster.Sum(yyy, roster.Prod(1, roster.EndOf(Hospital_dh[dd][hh])));
            //                        yyy = roster.Sum(yyy, roster.Prod(-1, roster.StartOf(Hospital_dh[d][h])));
            //                        roster.Add( roster.IfThen(roster.And(roster.PresenceOf(Hospital_dh[d][h]), roster.PresenceOf(Hospital_dh[dd][hh])), roster.AddGe(yyy, 0)));
            //                    }
            //                }
            //            }
            //        }
            //    }
            //}
            //for (int d = 0; d < D; d++)
            //{
            //    for (int dd = 0; dd < D; dd++)
            //    {
            //        if (d == dd)
            //        {
            //            continue;
            //        }
            //        IIntExpr change = roster.IntExpr();
            //        change = roster.Sum(change, change_dD[dd][d]);
            //        change = roster.Sum(change, roster.Prod(-1, y_dD[dd][d]));
            //        for (int ddd = 0; ddd < D; ddd++)
            //        {
            //            if (ddd == d || ddd == dd)
            //            {
            //                continue;
            //            }
            //            change = roster.Sum(change, change_dD[dd][ddd]);
            //        }
            //        roster.Add(roster.IfThen(roster.And(roster.PresenceOf(discipline_d[d]), roster.PresenceOf(discipline_d[dd])), roster.AddEq(change, 0)));


            //    }

            //}

            // all group assignment
            IIntExpr allPossibleCourses = roster.IntExpr();

            for (int d = 0; d < D; d++)
            {
                allPossibleCourses = roster.Sum(allPossibleCourses, roster.Prod(cc_d[d], roster.PresenceOf(discipline_d[d])));
            }
            roster.AddEq(allPossibleCourses, ALLK);

            // group assignment
            for (int g = 0; g < G; g++)
            {
                IIntExpr groupedCours_g = roster.IntExpr();
                for (int d = 0; d < D; d++)
                {
                    if (indexg_d[d] == g)
                    {
                        groupedCours_g = roster.Sum(groupedCours_g, roster.Prod(cc_d[d], roster.PresenceOf(discipline_d[d])));
                    }
                }
                roster.AddGe(groupedCours_g, k_g[g]);
            }


            // stay in one hospital



            // objective function
            INumExpr objExp = roster.NumExpr();

            // discipline desire
            for (int d = 0; d < D; d++)
            {
                objExp = roster.Sum(objExp, roster.Prod(prf_D[d], roster.PresenceOf(discipline_d[d])));
                for (int dd = 0; dd < D; dd++)
                {
                    objExp = roster.Sum(objExp, roster.Prod(-1, change_d[d]));
                }
            }
            objExp = roster.Sum(objExp, rc);
            IIntExpr makespan = roster.IntExpr();

            for (int d = 0; d < D; d++)
            {
                makespan = roster.Max(makespan, roster.EndOf(discipline_d[d]));
            }

            IIntVar  wait      = roster.IntVar(0, T);
            IIntExpr waitConst = roster.IntExpr();

            waitConst = roster.Sum(waitConst, wait);
            waitConst = roster.Sum(waitConst, roster.Prod(-1, makespan));
            for (int d = 0; d < D; d++)
            {
                waitConst = roster.Sum(waitConst, roster.Prod(dur[d], roster.PresenceOf(discipline_d[d])));
            }
            roster.AddEq(waitConst, 0);
            roster.AddMaximize(objExp);

            roster.ExportModel("Roster.cpo");
            roster.SetParameter(CP.IntParam.TimeMode, CP.ParameterValues.ElapsedTime);
            roster.SetParameter(CP.IntParam.LogVerbosity, CP.ParameterValues.Quiet);
            roster.SetParameter(CP.IntParam.SolutionLimit, 10);


            // solve it now
            if (roster.Solve())
            {
                Console.WriteLine("this is the cost of the CP column {0}", roster.ObjValue);
                for (int d = 0; d < D; d++)
                {
                    if (roster.IsPresent(discipline_d[d]))
                    {
                        Console.WriteLine("Discipline {0} with CC {1} and Dur {2} and Prf {3} started at time {4} and finished at time {5}", d, cc_d[d], dur[d], prf_D[d], roster.GetStart(discipline_d[d]), roster.GetEnd(discipline_d[d]));
                    }
                }

                for (int d = 0; d < D; d++)
                {
                    for (int h = 0; h < H; h++)
                    {
                        if (roster.IsPresent(Hospital_dh[d][h]))
                        {
                            Console.WriteLine("Discipline {0} with CC {1} and Dur {2} and Prf {3} started at time {4} and finished at time {5} at Hospitail {6}", d, cc_d[d], dur[d], prf_D[d], roster.GetStart(Hospital_dh[d][h]), roster.GetEnd(Hospital_dh[d][h]), h);
                        }
                    }
                }
                for (int d = 0; d < D * H; d++)
                {
                    int dIndex = d % D;
                    int hIndex = d / D;
                    if (roster.IsPresent(disHospSetUp_dh[d]))
                    {
                        Console.WriteLine("discpline " + dIndex + " in hospital " + hIndex);
                    }
                }

                for (int d = 0; d < D; d++)
                {
                    if (roster.GetValue(change_d[d]) > 0.5)
                    {
                        Console.WriteLine("We have change for discipline {0}", d);
                    }
                    for (int dd = 0; dd < D; dd++)
                    {
                        if (d == dd)
                        {
                            continue;
                        }
                    }
                }

                Console.WriteLine("=========================================");
                Console.WriteLine("Wainting time {0}", roster.GetValue(wait));
            }
        }
 public static IIntExpr MakeScenarioSubmodel(CP                     cp,
                                         int                    nbJobs,
                                         int                    nbMachines,
                                         int[][]                machines,
                                         int[][]                durations,
                                         IIntervalSequenceVar[] sequences)
 {
     List<IIntervalVar>[] machinesOps = new List<IIntervalVar>[nbMachines];
       String name;
       int i,j;
       for (j = 0; j < nbMachines; j++)
     machinesOps[j] = new List<IIntervalVar>();
       IIntExpr[] ends = new IIntExpr[nbJobs];
       for (i = 0; i < nbJobs; i++) {
     IIntervalVar prec = cp.IntervalVar();
     for (j = 0; j < nbMachines; j++) {
       name = "J" + i + "_O" + j;
       IIntervalVar ti = cp.IntervalVar(durations[i][j], name);
       machinesOps[machines[i][j]].Add(ti);
       if (j > 0)
     cp.Add(cp.EndBeforeStart(prec, ti));
       prec = ti;
     }
     ends[i] = cp.EndOf(prec);
       }
       for (j = 0; j < nbMachines; j++) {
     name = "M" + j;
     sequences[j] = cp.IntervalSequenceVar(machinesOps[j].ToArray(), name);
     cp.Add(cp.NoOverlap(sequences[j]));
       }
       return cp.Max(ends);
 }
        public static void Main(String[] args)
        {
            String filename = "../../../../examples/data/stochastic_jobshop_default.data";
              int failLimit = 250000;

              if (args.Length > 0)
            filename = args[0];
              if (args.Length > 1)
            failLimit = Convert.ToInt32(args[1]);

              // Data reading
              DataReader data = new DataReader(filename);
              int nbJobs, nbMachines, nbScenarios;
              nbJobs      = data.Next();
              nbMachines  = data.Next();
              nbScenarios = data.Next();
              int i,j,k;
              // machines[i][j]: machine used by jth operation of job i
              int[][] machines = new int[nbJobs][];
              for (i = 0; i < nbJobs; i++) {
            machines[i] = new int[nbMachines];
            for (j = 0; j < nbMachines; j++) {
              machines[i][j] = data.Next();
            }
              }
              // durations[k][i][j]: duration of jth operation of job i in scenario k
              int[][][] durations = new int[nbScenarios][][];
              for (k = 0; k < nbScenarios; k++) {
            durations[k] = new int[nbJobs][];
            for (i = 0; i < nbJobs; i++) {
              durations[k][i] = new int[nbMachines];
              for (j = 0; j < nbMachines; j++) {
            durations[k][i][j] = data.Next();
              }
            }
              }

              CP cp = new CP();
              IIntervalSequenceVar[] refSequences = new IIntervalSequenceVar[nbMachines];
              IIntExpr sumMakespan = cp.IntExpr();
              for (k = 0; k < nbScenarios; k++) {
            IIntervalSequenceVar[] scenarioSequences = new IIntervalSequenceVar[nbMachines];
            IIntExpr scenarioMakespan =
              MakeScenarioSubmodel(cp, nbJobs, nbMachines,
                               machines, durations[k],
                               scenarioSequences);
            // Objective function is aggregated
            sumMakespan = cp.Sum(sumMakespan, scenarioMakespan);
            // For each machine, a sameSequence constraint is posted across all scenarios
            if (0==k) {
              refSequences = scenarioSequences;
            } else {
              for (j = 0; j < nbMachines; j++) {
            cp.Add(cp.SameSequence(refSequences[j], scenarioSequences[j]));
              }
            }
              }
              // Objective function is expected makespan
              INumExpr expectedMakespan = cp.Quot(sumMakespan, nbScenarios);
              IObjective objective = cp.Minimize(expectedMakespan);
              cp.Add(objective);
              cp.SetParameter(CP.IntParam.FailLimit, failLimit);
              cp.SetParameter(CP.IntParam.LogPeriod, 1000000);
              Console.WriteLine("Instance \t: " + filename);
              if (cp.Solve()) {
            Console.WriteLine("Expected makespan \t: " + cp.ObjValue);
            for (j=0; j<nbMachines; ++j) {
              IIntervalSequenceVar s = refSequences[j];
              Console.Write(s.Name + ":\t");
              IIntervalVar op = cp.GetFirst(s);
              for (; !op.Equals(cp.GetLast(s)); op = cp.GetNext(s, op))
            Console.Write(op.Name+ "\t");
              Console.WriteLine(op.Name+ "\t");
            }
              } else {
            Console.WriteLine("No solution found.");
              }
        }