2

我正在做一个简单的求解器练习,我正在努力使用Math.Pow.

为什么我不能Math.Pow(error1, 2)在以下几行中使用?

error1 = error1 * error1;
error2 = error2 * error2;

它给出了一个错误:

错误 3 参数 1:无法从“Microsoft.SolverFoundation.Services.Term”转换为“double”

相关代码:

using Microsoft.SolverFoundation.Common;
using Microsoft.SolverFoundation.Services;

... 

private void Form1_Load(object sender, EventArgs e)
{    
    var solver = SolverContext.GetContext();
    var model = solver.CreateModel();
    Decision R = new Decision(Domain.Real, "R");
    Decision T = new Decision(Domain.Real, "T");
    model.AddDecisions(R);
    model.AddDecisions(T);

    var xr1=5;
    var xr2=4;
    var xp1 = 6;
    var xp2=8;
    var error1 = xr1 * R + T - xp1;
    var error2 = xr2 * R + T - xp2;
    error1 = error1 * error1;
    error2 = error2 * error2;
    model.AddGoal("error",GoalKind.Minimize,error1+error2);

    var solution = solver.Solve();
    var valordeR = R.GetDouble();
    var valordeT = T.GetDouble();
}  
4

1 回答 1

6

你可以使用Model.Power方法。Math.Pow仅适用于双打。

error1 = Model.Power(error1, 2);
error2 = Model.Power(error2, 2);
于 2013-11-23T12:16:06.653 回答