using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SolverFoundation.Services;
namespace Test
{
class Program
{
static void Main(string[] args)
{
var context= SolverContext.GetContext();
var model = context.CreateModel();
var index = new Set(Domain.IntegerRange(0, 6), "index");
var x = new Decision(Domain.IntegerRange(0, 5), "x", index);
model.AddDecision(x);
// When I uncomment the following line, values ends
// up with only 3 elements instead of 7 as expected
// model.AddConstraint("constraint", x[0] + x[1] + x[2] == 2);
model.AddGoal("objective", GoalKind.Minimize, Model.Sum(Model.ForEach(index, i => Model.Power(x[i] - 2, 2))));
context.Solve();
var values = x.GetValues().ToArray();
}
}
}
如果我按原样运行此代码,Solver Foundation 会正确计算七个值,每个值都等于 2。
当我取消注释model.AddConstraint("constraint", x[0] + x[1] + x[2] == 2)
时,最后的值只包含三个值:0、1 和 1。为什么它不包含其余值?
这是怎么回事?