Exemplo n.º 1
0
        static void Main(string[] args)
        {
            int nbWarehouses = -1;
            int nbStores = -1;
            int fixedP = -1;
            int disaggregate = -1;
            for (int i = 0; i < args.Length; i++)
            {
                if ("-h".Equals(args[i]))
                {
                    usage();
                }
                else if ("fixed".Equals(args[i]))
                {
                    if (i == args.Length)
                    {
                        usage();
                    }
                    fixedP = System.Int32.Parse(args[++i]);
                }
                else if ("nbWarehouses".Equals(args[i]))
                {
                    if (i == args.Length)
                    {
                        usage();
                    }
                    nbWarehouses = System.Int32.Parse(args[++i]);
                }
                else if ("nbStores".Equals(args[i]))
                {
                    if (i == args.Length)
                    {
                        usage();
                    }
                    nbStores = System.Int32.Parse(args[++i]);
                }
                else if ("disaggregate".Equals(args[i]))
                {
                    if (i == args.Length)
                    {
                        usage();
                    }
                    disaggregate = System.Int32.Parse(args[++i]);
                }
                else
                {
                    break;
                }
            }

            if (fixedP == -1 || nbWarehouses == -1 || nbStores == -1 || disaggregate == -1)
            {
                usage();
            }

            int status = 127;
            try
            {
                OplFactory.DebugMode = true;
                OplFactory oplF = new OplFactory();
                OplErrorHandler errHandler = oplF.CreateOplErrorHandler(Console.Out);
                OplModelSource modelSource = oplF.CreateOplModelSourceFromString(GetModelText(), "warehouse");
                OplSettings settings = oplF.CreateOplSettings(errHandler);
                OplModelDefinition def = oplF.CreateOplModelDefinition(modelSource, settings);
                Cplex cplex = oplF.CreateCplex();
                OplModel opl = oplF.CreateOplModel(def, cplex);
                OplDataSource dataSource = new MyParams(oplF, nbWarehouses, nbStores, fixedP, disaggregate);
                opl.AddDataSource(dataSource);
                opl.Generate();
                if (cplex.Solve())
                {
                    Console.Out.WriteLine("OBJECTIVE: " + opl.Cplex.ObjValue);
                    opl.PostProcess();
                    opl.PrintSolution(Console.Out);
                    status = 0;
                }
                else
                {
                    Console.Out.WriteLine("No solution!");
                    status = 1;
                }

                oplF.End();
            }
            catch (ILOG.OPL.OplException ex)
            {
                Console.WriteLine(ex.Message);
                status = 2;
            }
            catch (ILOG.Concert.Exception ex)
            {
                Console.WriteLine(ex.Message);
                status = 3;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
                status = 4;
            }

            Environment.ExitCode = status;

            Console.WriteLine("--Press <Enter> to exit--");
            Console.ReadLine();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            int nbWarehouses = -1;
            int nbStores     = -1;
            int fixedP       = -1;
            int disaggregate = -1;

            for (int i = 0; i < args.Length; i++)
            {
                if ("-h".Equals(args[i]))
                {
                    usage();
                }
                else if ("fixed".Equals(args[i]))
                {
                    if (i == args.Length)
                    {
                        usage();
                    }
                    fixedP = System.Int32.Parse(args[++i]);
                }
                else if ("nbWarehouses".Equals(args[i]))
                {
                    if (i == args.Length)
                    {
                        usage();
                    }
                    nbWarehouses = System.Int32.Parse(args[++i]);
                }
                else if ("nbStores".Equals(args[i]))
                {
                    if (i == args.Length)
                    {
                        usage();
                    }
                    nbStores = System.Int32.Parse(args[++i]);
                }
                else if ("disaggregate".Equals(args[i]))
                {
                    if (i == args.Length)
                    {
                        usage();
                    }
                    disaggregate = System.Int32.Parse(args[++i]);
                }
                else
                {
                    break;
                }
            }

            if (fixedP == -1 || nbWarehouses == -1 || nbStores == -1 || disaggregate == -1)
            {
                usage();
            }

            int status = 127;

            try
            {
                OplFactory.DebugMode = true;
                OplFactory         oplF        = new OplFactory();
                OplErrorHandler    errHandler  = oplF.CreateOplErrorHandler(Console.Out);
                OplModelSource     modelSource = oplF.CreateOplModelSourceFromString(GetModelText(), "warehouse");
                OplSettings        settings    = oplF.CreateOplSettings(errHandler);
                OplModelDefinition def         = oplF.CreateOplModelDefinition(modelSource, settings);
                Cplex         cplex            = oplF.CreateCplex();
                OplModel      opl        = oplF.CreateOplModel(def, cplex);
                OplDataSource dataSource = new MyParams(oplF, nbWarehouses, nbStores, fixedP, disaggregate);
                opl.AddDataSource(dataSource);
                opl.Generate();
                if (cplex.Solve())
                {
                    Console.Out.WriteLine("OBJECTIVE: " + opl.Cplex.ObjValue);
                    opl.PostProcess();
                    opl.PrintSolution(Console.Out);
                    status = 0;
                }
                else
                {
                    Console.Out.WriteLine("No solution!");
                    status = 1;
                }

                oplF.End();
            }
            catch (ILOG.OPL.OplException ex)
            {
                Console.WriteLine(ex.Message);
                status = 2;
            }
            catch (ILOG.Concert.Exception ex)
            {
                Console.WriteLine(ex.Message);
                status = 3;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
                status = 4;
            }

            Environment.ExitCode = status;

            Console.WriteLine("--Press <Enter> to exit--");
            Console.ReadLine();
        }