mercredi 2 novembre 2016

C# + Microsoft Solver Foundation: How to use if operator with string variables

I have this simple test code, slightly modified from here to include a string variable taken from an enumeration:

        SolverContext sc = SolverContext.GetContext();
        Model m = sc.CreateModel();

        Decision dec1 = new Decision(Domain.Enum("a","b","c"),"dec1");
        m.AddDecision(dec1);

        Decision dec2 = new Decision(Domain.RealNonnegative, "dec2");
        m.AddDecision(dec2);

        m.AddConstraint(null, "dec1 == If[dec2 == 3.0, \"a\" , \"b\"]");
        m.AddConstraint(null, "dec2 > 2.0");

        var sol = sc.Solve();
        Console.WriteLine(sol.GetReport());
        Console.ReadKey();

When I run the code, I get an argument exception error (" Input "a" is not numeric").

I also tried the following, but without success:

        SolverContext sc = SolverContext.GetContext();
        Model m = sc.CreateModel();

        Decision dec1 = new Decision(Domain.Enum("a","b","c"),"dec1");
        m.AddDecision(dec1);

        Decision dec2 = new Decision(Domain.RealNonnegative, "dec2");
        m.AddDecision(dec2);

        m.AddConstraint(null, "dec1 == If[dec2 == 3.0, a , b]"); // changed here
        m.AddConstraint(null, "dec2 > 2.0");

        var sol = sc.Solve();
        Console.WriteLine(sol.GetReport());
        Console.ReadKey();

This time I get an unhandled OnLParseException ("Failed to parse OML model. Expression: a").

It seems as if the IF operator in MSF can handle numbers only, and not strings... can someone confirm it? What's wrong with this code??

Aucun commentaire:

Enregistrer un commentaire