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

标签 c# optimization ms-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;
        }

函数必须返回一个 Term,而不是返回一个 double

关于c# - 方法作为 Microsoft Solver Foundation 中的目标函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21564722/

相关文章:

c# - 在 C# 中使用 Microsoft Solver Foundation

c# - 集成测试和 TDD

python - 计算连接到优化器中同一神经元的张量值子集的值

optimization - 我可以将 "null pointer optimization"用于我自己的非指针类型吗?

c# - 取决于 MSF 中决策的参数

c# - 将 MS Solver Foundation 与矩阵乘法结合使用

c# - 检查 session 中是否已存在数组

c# - 如何在 Xamarin 表单中的内容页面中添加选项卡式页面

c# - 尝试通过 Office 365 发送电子邮件时出现 SMTP 5.7.57 错误

performance - 有效地检查FP位模式是否为整数。根据条件组合,分支速度更快?