Esempio n. 1
0
        int Run()
        {
            int        status = 127;
            OplFactory oplF   = new OplFactory();

            if (_cl.CompileName != null)
            {
                OplCompiler    compiler    = oplF.CreateOplCompiler();
                StreamWriter   ofs         = new StreamWriter(_cl.CompileName, false);
                OplModelSource modelSource = oplF.CreateOplModelSource(_cl.ModelFileName);
                compiler.Compile(modelSource, ofs);
                ofs.Close();
                Trace("Compile");
                return(0);
            }
            if (_cl.ModelFileName == null && !_cl.IsProject)
            {
                return(0);
            }

            Trace("initial");
            OplRunConfiguration rc;
            OplErrorHandler     errHandler = oplF.CreateOplErrorHandler();

            if (_cl.IsProject)
            {
                OplProject prj = oplF.CreateOplProject(_cl.getProjectPath());
                rc = prj.MakeRunConfiguration(_cl.getRunConfigurationName());
            }
            else
            {
                if (_cl.DataFileNames.Length == 0)
                {
                    rc = oplF.CreateOplRunConfiguration(_cl.ModelFileName);
                }
                else
                {
                    rc = oplF.CreateOplRunConfiguration(_cl.ModelFileName, _cl.DataFileNames);
                }
            }
            rc.ErrorHandler = errHandler;
            OplModel    opl      = rc.OplModel;
            OplSettings settings = opl.Settings;

            settings.IsWithLocations     = true;
            settings.IsWithNames         = true;
            settings.IsForceElementUsage = _cl.IsForceElementUsage;

            status = 9;
            if (opl.ModelDefinition.hasMain())
            {
                status = opl.Main();
                Console.Out.WriteLine("main returns " + status);
                Trace("main");
            }
            else if (errHandler.Ok)
            {
                opl.Generate();
                Trace("generate model");
                if (opl.HasCplex)
                {
                    if (_cl.ExportName != null)
                    {
                        opl.Cplex.ExportModel(_cl.ExportName);
                        Trace("export model " + _cl.ExportName);
                    }
                    if (_cl.IsRelaxation)
                    {
                        Console.Out.WriteLine("RELAXATIONS to obtain a feasible problem: ");
                        opl.PrintRelaxation(Console.Out);
                        Console.Out.WriteLine("RELAXATIONS done.");
                    }
                    if (_cl.IsConflict)
                    {
                        Console.Out.WriteLine("CONFLICT in the infeasible problem: ");
                        opl.PrintConflict(Console.Out);
                        Console.Out.WriteLine("CONFLICT done.");
                    }
                    if (!_cl.IsRelaxation && !_cl.IsConflict)
                    {
                        bool result = false;
                        try
                        {
                            result = opl.Cplex.Solve();
                        }
                        catch (IloException ex)
                        {
                            Console.Out.WriteLine("### ENGINE exception: " + ex.Message);
                        }
                        if (result)
                        {
                            Trace("solve");
                            Console.Out.WriteLine();
                            Console.Out.WriteLine();
                            Console.Out.WriteLine("OBJECTIVE: " + opl.Cplex.ObjValue.ToString("F"));
                            opl.PostProcess();
                            Trace("post process");
                            if (_cl.IsVerbose)
                            {
                                opl.PrintSolution(Console.Out);
                            }
                            status = 0;
                        }
                        else
                        {
                            Trace("no solution");
                            status = 1;
                        }
                    }
                }
                else
                {//opl.hasCP()
                    bool result = false;
                    try
                    {
                        result = opl.CP.Solve();
                    }
                    catch (IloException ex)
                    {
                        Console.Out.WriteLine("### Engine exception: " + ex.Message);
                    }
                    if (result)
                    {
                        Trace("solve");
                        if (opl.CP.HasObjective())
                        {
                            Console.Out.WriteLine();
                            Console.Out.WriteLine();
                            Console.Out.WriteLine("OBJECTIVE: " + opl.CP.ObjValue.ToString("F"));
                        }
                        else
                        {
                            Console.Out.WriteLine();
                            Console.Out.WriteLine();
                            Console.Out.WriteLine("OBJECTIVE: no objective");
                        }
                        opl.PostProcess();
                        Trace("post process");
                        if (_cl.IsVerbose)
                        {
                            opl.PrintSolution(Console.Out);
                        }
                        status = 0;
                    }
                    else
                    {
                        Trace("no solution");
                        status = 1;
                    }
                }
            }
            if (_cl.ExternalDataName != null)
            {
                StreamWriter ofs = new StreamWriter(_cl.ExternalDataName, false);
                opl.PrintExternalData(ofs);
                ofs.Close();
                Trace("write external data " + _cl.ExternalDataName);
            }
            if (_cl.InternalDataName != null)
            {
                StreamWriter ofs = new StreamWriter(_cl.InternalDataName, false);
                opl.PrintInternalData(ofs);
                ofs.Close();
                Trace("write internal data " + _cl.InternalDataName);
            }

            Trace("done");
            return(status);
        }