LEM2算法的c#实现

本文关键字:实现 算法 LEM2 | 更新日期: 2023-09-27 18:10:18

我是StackOverflow的新手,这是我的第一个问题。我对部分le2算法有问题。像这样:

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}));
        }

And i have to do:

  1. 选择一条规则,使indexList覆盖g的最大值。
  2. 如果出现平局,则选择g之外的最小值规则。
  3. 如果出现平局,选择第一对。

的例子:

  1. Inflation-decrease,增加预算,储备减少涵盖了G.
  2. 的3条规则。
  3. 从这三个规则中我们选择了通货膨胀-减少&reserve-decrease。
  4. 结果选择规则膨胀-减小。

有什么建议吗?

LEM2算法的c#实现

正如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);
    }
}

如果您将更多属性设置为公共属性,将会有所帮助。参见下面的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        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 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 }));
            var results = ruleObjectList.Where(x => x.indexList.Where(y => G.Contains(y)).Count() == 3).ToList();
        }
    }
}
​