Gurobi无法识别LP文件

时间:2015-12-10 06:13:18

标签: c# linear-programming gurobi

当尝试使用Gurobi + C#从文本中打开线性编程问题时,它会抛出错误:10012无法打开文件“Maximize”进行输入。 最大化是文本的第一个单词,当使用

foreach (string s in args)
{
    Console.WriteLine(s);
}

我从文本文件中获得正确的输出。请帮忙!

using System;
using Gurobi;

class lp_cs
{
static void Main(string[] args)
{
    args = System.IO.File.ReadAllLines(@"C:\Users\Ben\Documents\Visual     Studio 2015\Projects\ConsoleApplication5\ConsoleApplication5\mps.lp");
    foreach (string s in args)
    {
        Console.WriteLine(s);
    }
    if (args.Length < 1)
    {
        Console.Out.WriteLine("Please Wait..");
        return;
    }

    try
    {
        GRBEnv env = new GRBEnv();
        GRBModel model = new GRBModel(env, args[0]);

        model.Optimize();

        int optimstatus = model.Get(GRB.IntAttr.Status);

        if (optimstatus == GRB.Status.INF_OR_UNBD)
        {
            model.GetEnv().Set(GRB.IntParam.Presolve, 0);
            model.Optimize();
            optimstatus = model.Get(GRB.IntAttr.Status);
        }

        if (optimstatus == GRB.Status.OPTIMAL)
        {
            double objval = model.Get(GRB.DoubleAttr.ObjVal);
            Console.WriteLine("Optimal objective: " + objval);
        }
        else if (optimstatus == GRB.Status.INFEASIBLE)
        {
            Console.WriteLine("Model is infeasible");

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

        model.Dispose();
        env.Dispose();

    }
    catch (GRBException e)
    {
        Console.WriteLine("Hibakód: " + e.ErrorCode + ". " + e.Message);
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

}

1 个答案:

答案 0 :(得分:1)

使用

args = System.IO.File.ReadAllLines(@"C:\Users\Ben\Documents\Visual Studio 2015\Projects\ConsoleApplication5\ConsoleApplication5\mps.lp");

您正在使用输入文件的所有行数组覆盖args方法的main()参数。这就是

的原因
GRBModel model = new GRBModel(env, args[0]);

args[0]包含一个字符串,其中包含LP文件的第一行而不是文件名。