C# 如何利用求解器基础简化决策矩阵

C# 如何利用求解器基础简化决策矩阵,c#,constraints,solver,C#,Constraints,Solver,已经挣扎了一段时间,希望在这里寻求一些建议。 首先,决策矩阵是这样的: 条件1->条件2->决策 假设: 1.我们只考虑上面的序列(“->”)。 2.条件1和条件2的所有可能选项仅为“是”和“否” 简化的标准是,如果在最后一个条件的所有可能选项下做出相同的决定,那么在相同的最后一个条件下,可以省略最后一个条件。i、 e 关于决定“继续”* 条件1->条件2->决策 我最初的想法是在这部分(*)中应用求解器基础,而其他部分则由传统编程来处理,比如循环和递归。在这种情况下,最终答案应该是 条件1->

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

条件1->条件2->决策 假设: 1.我们只考虑上面的序列(“->”)。 2.条件1和条件2的所有可能选项仅为“是”和“否”

简化的标准是,如果在最后一个条件的所有可能选项下做出相同的决定,那么在相同的最后一个条件下,可以省略最后一个条件。i、 e

关于决定“继续”*

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

条件1->条件2->决策 我陷入的是以下逻辑的实现:

决策和目标: 从集合*中,选择最小的索引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
}
如果你能提供一个样本代码,这将有助于其他人回答


感谢

directive.rununtimeout应在directive.TimeLimit=10000之前设置为true;随便说说!
 Yes             X          Go ahead
  No           Yes          Go ahead
  No            No          Give up
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 (Condition_One || Condition_Two)
{
 //Go ahead command
}
else
{
//Give up code
}