苦苦挣扎了一段时间,希望在这里寻求一些建议。首先,决策矩阵是这样的:
条件 1 -> 条件 2 -> 决定
Yes Yes Go ahead
Yes No Go ahead
No Yes Go ahead
No No Give up
假设: 1. 我们只考虑上述序列(“->”)。2. 条件 1 和 2 中的所有可能选项都只是“是”和“否”。
简化的标准是,如果在最后一个条件的所有可能选项下都做出相同的决定,那么在相同的 last-1 条件下可以省略最后一个条件。IE
对于决定“继续”*,
条件 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; }
}
}