编程语言
首页 > 编程语言> > c# – 如何处理Microsoft Solver Foundation中已存在的活动模型?

c# – 如何处理Microsoft Solver Foundation中已存在的活动模型?

作者:互联网

我使用库来解决线方程,它适用于从这个库创建一个类对象,但是当我想重新创建对象时,它会抛弃我:

System.InvalidOperationException: There is already an active model in the context.
At Microsoft.SolverFoundation.Services.SolverContext.CreateModel ()

问题是,在计算第一个例子之后,我想要更改数据并单击按钮并获得另一个示例的结果.

class LinearResolve
{
    public string[] Zmienne = new string[3];

    public LinearResolve(string[] table)
    {
        Zmienne = table;
    }

    public string Solver()
    {
        SolverContext context = SolverContext.GetContext();
        Model model = context.CreateModel();
        Decision a = new Decision(Domain.Real, "a");
        Decision b = new Decision(Domain.Real, "b");
        Decision c = new Decision(Domain.Real, "c");
        model.AddDecisions(a, b, c);
        model.AddConstraint("eqA", Zmienne[0]);
        model.AddConstraint("eqB", Zmienne[1]);
        model.AddConstraint("eqC", Zmienne[2]);
        Solution solution = context.Solve();
        string results = solution.GetReport().ToString();
        return results;
    }
}}

解决方法:

先拨打ClearModel

SolverContext context = SolverContext.GetContext();
context.ClearModel();
Model model = context.CreateModel();

标签:c,ms-solver-foundation
来源: https://codeday.me/bug/20190627/1304342.html