Method as an objective function in Microsoft Solver Foundation?

Here is a typical optimization wording with MSF:

using Microsoft.SolverFoundation.Services;

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

//decisions
Decision xs = new Decision(Domain.Real, "Number_of_small_chess_boards");
Decision xl = new Decision(Domain.Real, "Number_of_large_chess_boards");

model.AddDecisions(xs, xl);

//constraints
model.AddConstraints("limits", 0 <= xs, 0 <= xl);
model.AddConstraint("BoxWood", 1 * xs + 3 * xl <= 200);
model.AddConstraint("Lathe", 3 * xs + 2 * xl <= 160);

//Goals
model.AddGoal("Profit", GoalKind.Maximize, 5 * xs + 20 * xl);

// This doesn't work!
// model.AddGoal("Profit", GoalKind.Maximize, objfunc(xs, xl));


Solution sol = context.Solve(new SimplexDirective());
Report report = sol.GetReport();
Console.WriteLine(report);

Is it possible to use a separate method instead of an expression like "5 * xs + 20 * xl" as a function of the target? For example, a method like the following? How?

// this method doesn't work!
static double objfunc(Decision x, Decision y)
{
    return 5 * x.ToDouble() + 20 * y.ToDouble();
}
+4
source share
2 answers

It is so simple:

 static Term objfunc(Decision x, Decision y)
        {
            return 5 * x + 20 * y;
        }

Instead of returning a, the doublefunction should return a Term.

+4
source

Note. The function does not return a numerical response, but a method for evaluating the response. If you transcoded the term Test = 5 * x + 20 * y; String strTest = Test.ToString ();

Then strTest will be something like (Add (mult (5, x), mult (20, y));

-1

Source: https://habr.com/ru/post/1525326/


All Articles