gpt4 book ai didi

c# - LEM2算法在C#中的实现

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:43:40 24 4
gpt4 key购买 nike

我是 StackOverflow 的新手,这是我的第一个问题。我对 LEM2 算法的一部分有疑问。我有这样的东西:

public class Rule {
String atribute { get; set;}
String value { get; set;}

public Rule(){}

public Rule(String atribute, String value){
this.atribute = atribute;
this.value = value;
}
}

public class RuleObject {
Rule rule { get; set; }
List<int> indexList { get; set; }

public RuleObject(){}

public RuleObject (Rule rule, List<int> indexList){
this.rule = rule;
this.indexList = indexList;
}
}

public static void Main (string[] args){
List<int> G = new List<int> (){ 1, 2, 4, 5, 7 };
List<RuleObject> ruleObjectList = new List<RuleObject> ();

ruleObjectList.Add (new RuleObject (new Rule ("inflation", "decrease"), new List<int> (){1,2,7}));
ruleObjectList.Add (new RuleObject (new Rule ("inflation", "no_change"), new List<int> (){3,4,5,6,8}));
ruleObjectList.Add (new RuleObject (new Rule ("budget", "no_change"), new List<int> (){1,5,8}));
ruleObjectList.Add (new RuleObject (new Rule ("budget", "increase"), new List<int> (){2,3,4,6,7}));
ruleObjectList.Add (new RuleObject (new Rule ("reserve", "increase"), new List<int> (){1,3,7,8}));
ruleObjectList.Add (new RuleObject (new Rule ("reserve", "decrease"), new List<int> (){2,4,5}));
}

我必须做的:

  1. 从G中选择一个indexList覆盖最大值的规则。
  2. 如果出现平局,则选择最小值在 G 之外的规则。
  3. 如果出现另一对平局,则选择第一对。

例子:

  1. 通货膨胀减少 & 预算增加 & 储备减少涵盖 G 的 3 条规则。
  2. 从这 3 条规则中,我们选择通货膨胀减少和准备金减少。
  3. 在结果中我们选择规则 inflation-decrease。

有什么建议吗?

最佳答案

正如 jdweng 所说,您应该将属性设为public。然后你可以试试这个代码(对我来说它像你描述的那样工作):

public class Rule
{
public String atribute { get; set; }
public String value { get; set; }

public Rule() { }

public Rule(String atribute, String value)
{
this.atribute = atribute;
this.value = value;
}
}

public class RuleObject
{
public Rule rule { get; set; }
public List<int> indexList { get; set; }

public RuleObject() { }

public RuleObject(Rule rule, List<int> indexList)
{
this.rule = rule;
this.indexList = indexList;
}
}

public static class Program
{
public static void Main()
{
List<int> G = new List<int>() { 1, 2, 4, 5, 7 };
List<RuleObject> ruleObjectList = new List<RuleObject>();

ruleObjectList.Add(new RuleObject(new Rule("inflation", "decrease"), new List<int>() { 1, 2, 7 }));
ruleObjectList.Add(new RuleObject(new Rule("inflation", "no_change"), new List<int>() { 3, 4, 5, 6, 8 }));
ruleObjectList.Add(new RuleObject(new Rule("budget", "no_change"), new List<int>() { 1, 5, 8 }));
ruleObjectList.Add(new RuleObject(new Rule("budget", "increase"), new List<int>() { 2, 3, 4, 6, 7 }));
ruleObjectList.Add(new RuleObject(new Rule("reserve", "increase"), new List<int>() { 1, 3, 7, 8 }));
ruleObjectList.Add(new RuleObject(new Rule("reserve", "decrease"), new List<int>() { 2, 4, 5 }));

// See which rules are included and/or escluded form G.
// Later we'll take the result based on this.
var candidates = from r in ruleObjectList
select new
{
rule = r.rule,
contained = r.indexList.FindAll(num => G.Contains(num)),
excluded = r.indexList.FindAll(num => !G.Contains(num))
};

// Take the ruleObject which has most values inside G and least values outside G.
var result = candidates.OrderByDescending(c => c.contained.Count) // Order descending by contained rules in G
.ThenBy(e => e.excluded.Count) // Then order ascending by excluded rules from G
.FirstOrDefault(); // Take the element on top of the list, the result, or return null
// if there are no results.

// Print final result.
Console.WriteLine("Result is " + result?.rule.atribute + "-" + result?.rule.value);
}
}

关于c# - LEM2算法在C#中的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32288400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com