开发者

How to use Solver Foundation to simplify a decision matrix

开发者 https://www.devze.com 2023-03-21 01:12 出处:网络
Have been struggling a while, seek for some suggestion here hopefully. First the decision matrix is something like this:

Have been struggling a while, seek for some suggestion here hopefully. First the decision matrix is something like this:

Condition 1 -> Condition 2 -> Decision

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

Assumption: 1. We only consider in above sequence ("->"). 2. All possible options from condition 1 & 2 are merely "Yes" and "No".

The criteria of simplification is that if the same decision is made under all possible options from the last condition, then the last condition can be omitted under the same last-1 condition. i.e.

For decision "Go ahead" *,

Condition 1 -> Condition 2 -> Decision

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

My initial idea is to apply solver foundation in this part(*), whereas other parts are handle by traditional programming, like for loop and recursive. In this case the final answer should be

Condition 1 -> Condition 2 -> Decision

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

What I am stuck in is the implementation of the following logic:

Decision & Goal: From the set *, pick the smallest index i (i.e. pick a case which represent the key to simplify).

Constraint: Pick cases whose condition 1 is equal to the decided case's condition 1 and then check if all possible options appear in condition 2 in all the cases picked.

Once we get the decision, which is 0, we knows the cases where condition 1 is "Yes" can have the condition 2 omitted.

See anyone can help. Thanks.

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 = n开发者_StackOverflowew 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; }
 }
}


is it possible to use if command like this

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

if you can provide a sample code which will help others to answer...

thanks

0

精彩评论

暂无评论...
验证码 换一张
取 消