如何使用Solver Foundation来简化决策矩阵



已经挣扎了一段时间,希望在这里寻求一些建议。首先,决策矩阵是这样的:

条件1 ->条件2 ->决策

 Yes           Yes          Go ahead
 Yes            No          Go ahead
  No           Yes          Go ahead
  No            No          Give up

假设:1. 我们只考虑上面的序列("->")。2. 条件1 &只有"是"one_answers"不是"。

简化的标准是,如果在最后一个条件的所有可能选项下做出相同的决策,那么在相同的last-1条件下可以省略最后一个条件。例如

For decision "Go ahead" *,

条件1 ->条件2 ->决策

 Yes        Yes =>  X       Go ahead
 Yes        Yes =>  X       Go ahead
  No           Yes          Go ahead

我最初的想法是在这部分(*)中应用求解器基础,而其他部分则由传统编程处理,如for循环和递归。在这种情况下,最终答案应该是

条件1 ->条件2 ->决策

 Yes             X          Go ahead
  No           Yes          Go ahead
  No            No          Give up

我陷入的是以下逻辑的实现:

的决定,目标:从集合*中选择最小的索引i(即选择一个表示要简化的键的大小写)。

约束:选择条件1等于条件1的情况,然后检查在所有选择的情况下是否在条件2中出现所有可能的选项。

一旦我们得到决定,即0,我们知道条件1为"是"的情况下可以省略条件2。

看谁能帮上忙。谢谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SolverFoundation.Common;
using Microsoft.SolverFoundation.Services;
using Microsoft.SolverFoundation.Solvers;
namespace ConsoleApplication5
{
 class Program
 {
  static void Main(string[] args)
  {
   SolverContext context = SolverContext.GetContext();
   Model model = context.CreateModel();
   Segment[] segmentData = new Segment[] { 
    new Segment { id = 0, col1 = 0, col2 = 0 },
    new Segment { id = 1, col1 = 0, col2 = 1 },//answer = 0, since this row's col1 and col2 = row 0's, thus can be omitted (simplified).
    new Segment { id = 2, col1 = 1, col2 = 0 } //answer = 0, since this row's col1 not = row 0's, thus remain unchanged.
   };
   //set
   Set items = new Set(Domain.Integer, "items");
   //decision
   Decision i = new Decision(Domain.IntegerRange(0,1), "index");
   model.AddDecisions(i);
   //parameter
   Parameter col1 = new Parameter(Domain.Integer, "col1", items);
   col1.SetBinding(segmentData, "col1", "id");
   Parameter col2 = new Parameter(Domain.Integer, "col2", items);
   col2.SetBinding(segmentData, "col2", "id");
   model.AddParameters(col1, col2);
   //constraint
   //sum of all possible col2 should be 0 + 1 = 1
   model.AddConstraint("cases", 
    1 == Model.Sum(
     Model.ForEachWhere(
      items,
      s => col2[s],
      s => col1[s] == i
     )
    )
   );
   model.AddGoal("goal", GoalKind.Minimize, i);
   //problem: no suitable directive found.
   HybridLocalSearchDirective directive = new HybridLocalSearchDirective();
   directive.TimeLimit = 10000;
   Solution solution = context.Solve(directive);
   Report report = solution.GetReport();
   Console.WriteLine("{0}", i);
   Console.Write("{0}", report);
   Console.ReadLine();
  }
 }
 class Segment
 {
  public int id { get; set; }
  public int col1 { get; set; }
  public int col2 { get; set; }
 }
}

是否可以像这样使用if命令

if (Condition_One || Condition_Two)
{
 //Go ahead command
}
else
{
//Give up code
}

如果你能提供一个示例代码,这将有助于别人回答…

谢谢

最新更新