예제 #1
0
        public static GRBLinExpr Sumx(this IEnumerable <GRBLinExpr> linExprs)
        {
            var expr = new GRBLinExpr();

            foreach (var lin in linExprs)
            {
                expr.Add(lin);
            }
            return(expr);
        }
예제 #2
0
        public static GRBLinExpr Sumx(this IEnumerable <GRBVar> vars)
        {
            var expr = new GRBLinExpr();

            foreach (var v in vars)
            {
                expr.Add(v);
            }
            return(expr);
        }
예제 #3
0
        public static GRBLinExpr SumL1(this GRBLinExpr[] _vars)
        {
            var res = new GRBLinExpr();

            foreach (var v in _vars)
            {
                res.Add(v);
            }
            return(res);
        }
예제 #4
0
        public static GRBLinExpr Quicksum(this IEnumerable <GRBLinExpr> exprsToAdd)
        {
            GRBLinExpr expr = new GRBLinExpr();

            foreach (var exprToAdd in exprsToAdd)
            {
                expr.Add(exprToAdd);
            }

            return(expr);
        }
예제 #5
0
        public static GRBLinExpr SumL1(this List <GRBLinExpr> _expressions)
        {
            var res = new GRBLinExpr();

            foreach (var v in _expressions)
            {
                if ((object)v != null)
                {
                    res.Add(v);
                }
            }
            return(res);
        }
예제 #6
0
        public static GRBLinExpr SumL1(this GRBVar[] _vars)
        {
            var res = new GRBLinExpr();

            foreach (var v in _vars)
            {
                if ((object)v != null)
                {
                    res.Add(v);
                }
            }
            return(res);
        }
예제 #7
0
 private void VehiclesMustLeaveTheArrivingCustomer()
 {
     for (int v = 0; v < _vehicles.Count; v++)
     {
         for (int s = 1; s <= _vertices.Count - 2; s++)
         {
             var flow = new GRBLinExpr();
             for (int e = 0; e < _vertices.Count; e++)
             {
                 flow.Add(_vehicleTraverse[v][e][s] - _vehicleTraverse[v][s][e]);
             }
             _model.AddConstr(flow, GRB.EQUAL, 0.0, "_VehiclesMustLeaveTheArrivingCustomer");
         }
     }
 }
예제 #8
0
        public void ILP_Janssen()
        {
            try
            {
                // Number of plants and warehouses
                int nJobs     = 750;
                int nMachines = 12;

                double[,] processingTime =
                    new double[nMachines, nJobs];

                for (int i = 0; i < nMachines; ++i)
                {
                    for (int j = 0; j < nJobs; ++j)
                    {
                        processingTime[i, j] = new Random().Next(1000, 1500);
                    }
                }

                // Number of plants and warehouses
                //int nJobs = 20;
                //int nMachines = 3;

                //double[,] processingTime =
                //    new double[nMachines, nJobs];

                //Random Rand = new Random(1);

                //for (int i = 0; i < nMachines; ++i)
                //{
                //    if (i==0)
                //    {
                //        for (int j = 0; j < nJobs; ++j)
                //        {
                //            processingTime[i, j] = Rand.Next(100, 201);
                //        }
                //    }
                //    else if (i==1)
                //    {
                //        for (int j = 0; j < nJobs; ++j)
                //        {
                //            processingTime[i, j] = Rand.Next(100, 201);
                //        }
                //    }
                //    else if (i == 2)
                //    {
                //        for (int j = 0; j < nJobs; ++j)
                //        {
                //            processingTime[i, j] = Rand.Next(100, 201);
                //        }
                //    }
                //}

                // Create an empty environment, set options and start
                GRBEnv env = new GRBEnv(true);
                env.Set("LogFile", "mip1.log");
                env.Start();

                // Create empty model
                GRBModel model = new GRBModel(env);

                // Decision Variable
                GRBVar[,,] x = new GRBVar[nMachines, nJobs, nJobs];

                for (int i = 0; i < nMachines; ++i)
                {
                    for (int j = 0; j < nJobs; ++j)
                    {
                        for (int k = 0; k < nJobs; ++k)
                        {
                            x[i, j, k] = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "x");
                        }
                    }
                }

                //Set objective:
                GRBLinExpr exp1 = 0.0;

                for (int i = 0; i < nMachines; ++i)
                {
                    for (int j = 0; j < nJobs; ++j)
                    {
                        for (int k = 0; k < nJobs; ++k)
                        {
                            GRBLinExpr exp2 = (k + 1) * processingTime[i, j] * x[i, j, k];
                            exp1.Add(exp2);
                        }
                    }
                }

                model.SetObjective(exp1, GRB.MINIMIZE);

                // Constraints
                for (int j = 0; j < nJobs; ++j)
                {
                    GRBLinExpr exp3 = 0.0;

                    for (int i = 0; i < nMachines; ++i)
                    {
                        for (int k = 0; k < nJobs; ++k)
                        {
                            exp3.AddTerm(1.0, x[i, j, k]);
                        }
                    }
                    model.AddConstr(exp3 == 1.0, "c1");
                }

                for (int i = 0; i < nMachines; ++i)
                {
                    for (int k = 0; k < nJobs; ++k)
                    {
                        GRBLinExpr exp4 = 0.0;
                        for (int j = 0; j < nJobs; ++j)
                        {
                            exp4.AddTerm(1.0, x[i, j, k]);
                        }
                        model.AddConstr(exp4 <= 1.0, "c2");
                    }
                }

                // Solve
                model.Optimize();

                for (int i = 0; i < nMachines; ++i)
                {
                    double totalMachineTime = 0.0;
                    for (int j = 0; j < nJobs; ++j)
                    {
                        for (int k = 0; k < nJobs; ++k)
                        {
                            if (x[i, j, k].X == 1.0)
                            {
                                totalMachineTime += processingTime[i, j];
                                Console.WriteLine($"({i},{j},{k})" + "ProcessingTime: " + processingTime[i, j]);
                            }
                        }
                    }
                    Console.WriteLine(totalMachineTime);
                }

                Console.WriteLine("Obj: " + model.ObjVal);

                // Dispose of model and env
                model.Dispose();
                env.Dispose();
            }
            catch (GRBException e)
            {
                Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message);
            }
        }
        public void RescheduleLotMachineAllocation()
        {
            Console.WriteLine(GetTime);
            Console.WriteLine("QueueLength:");
            Console.WriteLine(Queue.Length);
            int limitILPSolver = 200; // maximum number of jobs put into the ILP solver


            // Clear scheduled lots
            foreach (Machine machine in LithographyArea.Machines)
            {
                ScheduledLotsPerMachine[machine.Name].Clear();
            }

            try
            {
                // Get lots which have to be scheduled

                List <Lot> lotsToBeScheduled = new List <Lot>();


                if (Queue.Length <= limitILPSolver) // Small enough for ILP solver
                {
                    for (int j = 0; j < Queue.Length; j++)
                    {
                        // Peek next lot in queue
                        Lot peekLot = Queue.PeekAt(j);
                        lotsToBeScheduled.Add(peekLot);
                    }
                }
                else //Filter lots to get a set of 750 lots for ILP Solver
                {
                    lotsToBeScheduled = DecreaseQueueLength(limitILPSolver);
                }

                double[,] processingTimes = GetProcessingTimes(lotsToBeScheduled);

                // Number of jobs and machines
                int nMachines = processingTimes.GetLength(0);
                int nJobs     = processingTimes.GetLength(1);

                Console.WriteLine($"Number of jobs Scheduled: {nJobs}, at time: {GetTime / 3600}");

                // Create an empty environment, set options and start
                GRBEnv env = new GRBEnv(true);
                //env.Set("LogFile", "mip1.log");
                env.OutputFlag = 0;
                env.Start();

                // Create empty model
                GRBModel model = new GRBModel(env);

                // Decision Variable
                GRBVar[,,] x = new GRBVar[nMachines, nJobs, nJobs];
                for (int i = 0; i < nMachines; ++i)
                {
                    for (int j = 0; j < nJobs; ++j)
                    {
                        for (int k = 0; k < nJobs; ++k)
                        {
                            x[i, j, k] = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "x");
                        }
                    }
                }

                // Set objective:
                GRBLinExpr exp1 = 0.0;

                for (int i = 0; i < nMachines; ++i)
                {
                    for (int j = 0; j < nJobs; ++j)
                    {
                        for (int k = 0; k < nJobs; ++k)
                        {
                            GRBLinExpr exp2 = (k + 1) * processingTimes[i, j] * x[i, j, k];
                            exp1.Add(exp2);
                        }
                    }
                }

                model.SetObjective(exp1, GRB.MINIMIZE);

                // Constraints
                for (int j = 0; j < nJobs; ++j)
                {
                    GRBLinExpr exp3 = 0.0;

                    for (int i = 0; i < nMachines; ++i)
                    {
                        for (int k = 0; k < nJobs; ++k)
                        {
                            exp3.AddTerm(1.0, x[i, j, k]);
                        }
                    }
                    model.AddConstr(exp3 == 1.0, "c1");
                }

                for (int i = 0; i < nMachines; ++i)
                {
                    for (int k = 0; k < nJobs; ++k)
                    {
                        GRBLinExpr exp4 = 0.0;
                        for (int j = 0; j < nJobs; ++j)
                        {
                            exp4.AddTerm(1.0, x[i, j, k]);
                        }
                        model.AddConstr(exp4 <= 1.0, "c2");
                    }
                }

                // Solve
                model.Optimize();

                for (int i = 0; i < nMachines; ++i)
                {
                    for (int j = 0; j < nJobs; j++)
                    {
                        Lot peekLot = lotsToBeScheduled[j];
                        for (int k = 0; k < nJobs; ++k)
                        {
                            if (x[i, j, k].X == 1.0)
                            {
                                //Console.WriteLine($"({i},{j},{k})" + "ProcessingTime: " + processingTimes[i, j]);

                                //TODO: Schedule on machine
                                ScheduledLotsPerMachine[LithographyArea.Machines[i].Name].Add(peekLot);
                            }
                        }
                    }
                }

                Console.WriteLine("Obj: " + model.ObjVal);

                // Dispose of model and env
                model.Dispose();
                env.Dispose();
            }
            catch (GRBException e)
            {
                Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message);
            }
        }
예제 #10
0
        public void OptimalPlacement(ref List <Station> stationList, ref List <OHCAEvent> eventList)
        {
            int[,] a = new int[J, I];
            for (int j = 0; j < J; j++)
            {
                for (int i = 0; i < I; i++)
                {
                    a[j, i] = (Utils.GetDistance(stationList[i].lat, stationList[i].lon, eventList[j].lat, eventList[j].lon) < Utils.GOLDEN_TIME - 1.0 / 6.0) ? 1 : 0;
                }
            }

            try
            {
                GRBEnv   env   = new GRBEnv("Boutilier.log");
                GRBModel model = new GRBModel(env);

                GRBVar[] y = new GRBVar[I];
                for (int i = 0; i < I; i++)
                {
                    y[i] = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "y_" + i);
                }

                GRBVar[, ] z = new GRBVar[J, I];
                for (int j = 0; j < J; j++)
                {
                    for (int i = 0; i < I; i++)
                    {
                        z[j, i] = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, "z_" + i + "," + j);
                    }
                }

                GRBLinExpr obj_expr = 0.0;
                for (int i = 0; i < I; i++)
                {
                    obj_expr.AddTerm(1.0, y[i]);
                }

                model.SetObjective(obj_expr, GRB.MINIMIZE);
                GRBLinExpr bigExpr = 0.0;
                for (int j = 0; j < J; j++)
                {
                    GRBLinExpr expr = 0.0;
                    for (int i = 0; i < I; i++)
                    {
                        expr.AddTerm(1, z[j, i]);
                    }
                    bigExpr.Add(expr);
                    model.AddConstr(expr, GRB.LESS_EQUAL, 1.0, "c0_" + j);
                }
                model.AddConstr(bigExpr, GRB.GREATER_EQUAL, f / 100.0 * J, "c1");

                for (int j = 0; j < J; j++)
                {
                    for (int i = 0; i < I; i++)
                    {
                        model.AddConstr(z[j, i], GRB.LESS_EQUAL, a[j, i] * y[i], "c2_" + i + "," + j);
                    }
                }

                model.Write("model_b.lp");

                model.Optimize();

                int optimstatus = model.Status;

                if (optimstatus == GRB.Status.INF_OR_UNBD)
                {
                    model.Parameters.Presolve = 0;
                    model.Optimize();
                    optimstatus = model.Status;
                }

                if (optimstatus == GRB.Status.OPTIMAL)
                {
                }

                else if (optimstatus == GRB.Status.INFEASIBLE)
                {
                    Console.WriteLine("Model is unbounded");
                }
                else
                {
                    Console.WriteLine("Optimization was stopped with status = " + optimstatus);
                }

                List <Station> tempList = new List <Station>();
                CloneList(stationList, tempList);
                stationList.Clear();
                for (int i = 0; i < I; i++)
                {
                    if (y[i].Get(GRB.DoubleAttr.X) > 0)
                    {
                        stationList.Add(tempList[i]);
                    }
                }

                I = stationList.Count;

                coverList = new List <List <int> >();
                for (int i = 0; i < I; i++)
                {
                    coverList.Add(new List <int>());
                }

                for (int j = 0; j < J; j++)
                {
                    for (int i = 0; i < I; i++)
                    {
                        if (z[j, i].Get(GRB.DoubleAttr.X) > 0)
                        {
                            coverList[i].Add(j);
                        }
                    }
                }

                model.Dispose();
                env.Dispose();
            }
            catch (GRBException e)
            {
                Console.WriteLine("Error code : " + e.ErrorCode + ", " + e.Message);
            }
        }