Gurobi无法识别LP文件
本文关键字:LP 文件 识别 Gurobi | 更新日期: 2023-09-27 18:26:20
当试图用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();
}
}
}
使用
args = System.IO.File.ReadAllLines(@"C:'Users'Ben'Documents'Visual Studio 2015'Projects'ConsoleApplication5'ConsoleApplication5'mps.lp");
您正在用输入文件的所有行的数组覆盖main()
方法的args
参数。这就是为什么在
GRBModel model = new GRBModel(env, args[0]);
args[0]
包含一个字符串,其中包含LP文件的第一行,而不是文件名。