简体   繁体   English

方法作为Microsoft Solver Foundation中的目标函数?

[英]method as goal function in Microsoft Solver Foundation?

Here is a typical optimization formulation with MSF: 以下是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 a statement like "5 * xs + 20 * xl" as goal function? 是否可以使用单独的方法而不是像“5 * xs + 20 * xl”这样的语句作为目标函数? 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();
}

It is as simple as that: 它是如此简单:

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

Rather than returning a double , the function has to return a Term . 该函数不必返回double ,而是返回Term

Note The function does not return a numerical answer, but a method to evaluate the answer. 注意该函数不返回数字答案,而是一种评估答案的方法。 If you were to recode as Term Test = 5 * x + 20 * y; 如果你要重新编码为Term Test = 5 * x + 20 * y; String strTest = Test.ToString(); String strTest = Test.ToString();

Then strTest would by something like (Add(mult(5,x), mult(20,y)); 然后strTest会像(Add(mult(5,x),mult(20,y));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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