簡體   English   中英

方法作為Microsoft Solver Foundation中的目標函數?

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

以下是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);

是否可以使用單獨的方法而不是像“5 * xs + 20 * xl”這樣的語句作為目標函數? 例如,如下所示的方法? 怎么樣?

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

它是如此簡單:

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

該函數不必返回double ,而是返回Term

注意該函數不返回數字答案,而是一種評估答案的方法。 如果你要重新編碼為Term Test = 5 * x + 20 * y; String strTest = Test.ToString();

然后strTest會像(Add(mult(5,x),mult(20,y));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM