简体   繁体   中英

How to deal with an active model already present in Microsoft Solver Foundation?

I used the library to solve line equation, it works for one creation of a class object from this library, but when I want to re-create the object, it throws me away:

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

The problem is that after counting the first example, I want to change data and click on the button and get results for another example.

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;
    }
}}

Call ClearModel first:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM