Ejemplo n.º 1
0
    static void Main(string[] args)
    {
        if (args.Length < 1)
        {
            Console.Out.WriteLine("Usage: tune_cs filename");
            return;
        }

        try {
            GRBEnv env = new GRBEnv();

            // Read model from file
            GRBModel model = new GRBModel(env, args[0]);

            // Set the TuneResults parameter to 1
            model.GetEnv().Set(GRB.IntParam.TuneResults, 1);

            // Tune the model
            model.Tune();

            // Get the number of tuning results
            int resultcount = model.Get(GRB.IntAttr.TuneResultCount);

            if (resultcount > 0)
            {
                // Load the tuned parameters into the model's environment
                model.GetTuneResult(0);

                // Write the tuned parameters to a file
                model.Write("tune.prm");

                // Solve the model using the tuned parameters
                model.Optimize();
            }

            // Dispose of model and environment
            model.Dispose();
            env.Dispose();
        } catch (GRBException e) {
            Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message);
        }
    }
Ejemplo n.º 2
0
        public Dictionary <string, string> Solve(bool tune, bool debug, string tuneOutputFile = null, string paramFile = null)
        {
            var times = new Dictionary <string, string>();

            var totalTime = Utils.TimeAction(() =>
            {
                try
                {
                    // Create an empty environment, set options and start
                    GRBEnv env = new GRBEnv(true);

                    // Use gurobi's parameter reader to load the best file
                    if (paramFile != null)
                    {
                        env.ReadParams(paramFile);
                    }

                    if (!debug)
                    {
                        env.Set(GRB.IntParam.OutputFlag, 0);
                    }

                    env.Start();

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

                    (var grbSeated, var addDecisionVariableTime) = Utils.TimeFunction(() => AddSeatedBinaryVariables(model), "Add Decision Variables");

                    var addConstraintsTime = Utils.TimeAction(() => AddContraints(model, grbSeated), "Add Constraints");

                    var addObjectiveTime = Utils.TimeAction(() => AddObjective(model, grbSeated), "Add Objective");

                    var optimizeTime = Utils.TimeAction(() => model.Optimize(), "Optimizing");

                    if (tune)
                    {
                        model.Tune();

                        model.GetTuneResult(0);
                        model.Write(tuneOutputFile);
                    }

                    SeatGroups(grbSeated);

                    // Dispose of model and env
                    model.Dispose();
                    env.Dispose();

                    times.Add("Add Decision Variables", addDecisionVariableTime);
                    times.Add("Add Constraints", addConstraintsTime);
                    times.Add("Add Objective", addObjectiveTime);
                    times.Add("Optimizing", optimizeTime);
                }
                catch (GRBException e)
                {
                    Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message);
                    throw e;
                }
            }, "Total");

            times.Add("Total", totalTime);

            return(times);
        }