解决方案基金会无法解决?!

时间:2015-10-15 13:21:27

标签: c# .net solver

我在玩微软的Solver Foundation。我想解决4个等于711的变量的问题。对于我的问题,每个人都应该至少看到一个可能的解决方案。但我的代码不想工作。这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SolverFoundation.Services;
using Microsoft.SolverFoundation.Common;
namespace CSP711Problem
{
    class Program
    {
        static void Main(string[] args)
        {
            var solver = SolverContext.GetContext();
            var model = solver.CreateModel();

            Domain values = Domain.IntegerRange(1, 711);
            Decision p1 = new Decision(values, "price1");
            Decision p2 = new Decision(values, "price2");
            Decision p3 = new Decision(values, "price3");
            Decision p4 = new Decision(values, "price4");

            model.AddDecisions(p1, p2, p3, p4);
            var target = 711;
            model.AddConstraint("Add", Model.Equals(target , p1 + p2 + p3 + p4) );
            //model.AddConstraint("Mul", 711 == p1 * p2 * p3 * p4);

            DecisionBinding bindP1 = p1.CreateBinding();
            DecisionBinding bindP2 = p2.CreateBinding();
            DecisionBinding bindP3 = p3.CreateBinding();
            DecisionBinding bindP4 = p4.CreateBinding();

            DecisionBinding[] bindings = new DecisionBinding[] { bindP1, bindP2, bindP3, bindP4 };

            solver.FindAllowedValues(bindings);

            string[] valuesp1 = bindP1.StringFeasibleValues.ToArray();
            string[] valuesp2 = bindP2.StringFeasibleValues.ToArray();
            string[] valuesp3 = bindP3.StringFeasibleValues.ToArray();
            string[] valuesp4 = bindP4.StringFeasibleValues.ToArray();

            Console.WriteLine("Price1: \t{0}", string.Join(", ", valuesp1));
            Console.WriteLine("Price2: \t{0}", string.Join(", ", valuesp2));
            Console.WriteLine("Price3: \t{0}", string.Join(", ", valuesp3));
            Console.WriteLine("Price4: \t{0}", string.Join(", ", valuesp4));
            //var solution = solver.Solve();


            Console.ReadLine();
        }
    }
}

但是当我运行Code时,我得到一个: Microsoft.Solver.Foundation.dll中出现'Microsoft.SolverFoundation.Common.UnsolvableModelException'

异常。我是新手,不知道如何解决这个问题。有人可以帮帮我吗。我认为问题依赖于问题定义。

谢谢!

0 个答案:

没有答案