Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.DecisionList/3.3/Rule.cs @ 9605

Last change on this file since 9605 was 9605, checked in by sforsten, 11 years ago

#1980:

  • set plugin dependencies
  • added smart initialization
  • added hierarchical selection
  • fixed major and minor default rule
  • fixed several smaller bugs
  • some refactoring has been done
File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Optimization.Operators.LCS;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Encodings.DecisionList {
32  [StorableClass]
33  [Item("Rule", "")]
34  public class Rule : Item {
35    [Storable]
36    private IDictionary<string, IVariable> variables;
37    public IDictionary<string, IVariable> Variables {
38      get { return variables; }
39    }
40
41    [Storable]
42    private IAction action;
43    public IAction Action { get { return action; } }
44
45    public double Length {
46      get { return variables.Values.Sum(x => x.Length); }
47    }
48
49    [StorableConstructor]
50    protected Rule(bool deserializing) : base(deserializing) { }
51    protected Rule(Rule original, Cloner cloner)
52      : base(original, cloner) {
53      variables = new Dictionary<string, IVariable>(original.variables.Count);
54      foreach (var item in original.variables) {
55        variables.Add(item.Key, cloner.Clone(item.Value));
56      }
57      action = (IAction)original.action.Clone();
58    }
59    public Rule()
60      : base() {
61      variables = new Dictionary<string, IVariable>();
62    }
63    public Rule(IEnumerable<IVariable> condition, IAction action)
64      : this() {
65      foreach (var variable in condition) {
66        variables.Add(variable.VariableName, variable);
67      }
68      this.action = action;
69    }
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new Rule(this, cloner);
72    }
73
74    public override string ToString() {
75      StringBuilder sb = new StringBuilder();
76      foreach (var variable in Variables) {
77        sb.Append(variable.Value + "|");
78      }
79      sb.Append("|" + Action);
80      return sb.ToString();
81    }
82
83    public void Randomize(IRandom random, double oneProbability, IEnumerable<IDiscretizer> discretizers, IEnumerable<IAction> exceptActions = null) {
84      foreach (var variable in variables.Values) {
85        variable.Randomize(random, oneProbability, discretizers);
86      }
87      if (exceptActions == null) {
88        action.Randomize(random);
89      } else {
90        action.Randomize(random, exceptActions);
91      }
92    }
93
94    public void SetToMatchInput(IGAssistInput input) {
95      foreach (var variable in variables) {
96        if (!input.VariableNames.Contains(variable.Key)) {
97          throw new ArgumentException("input does not contain variable name of rule");
98        }
99        variable.Value.SetToMatch(input.GetVariableValue(variable.Key));
100      }
101    }
102
103    public double ComputeTheoryLength() {
104      return variables.Sum(x => x.Value.ComputeTheoryLength());
105    }
106
107    public bool MatchInput(IGAssistInput target) {
108      foreach (var variable in variables) {
109        if (!target.VariableNames.Contains(variable.Key)) { throw new ArgumentException("Input doesn't contain variable"); }
110        if (!variable.Value.Match(target.GetVariableValue(variable.Key))) { return false; }
111      }
112      return true;
113    }
114
115    public Rule Crossover(Rule rule, IRandom random) {
116      //variables and rule.Variables have to be the same
117      int cutpoint = random.Next(0, Variables.Count);
118      var crossedVariables = new List<IVariable>(variables.Count);
119      var variableNames = variables.Keys.ToList();
120      for (int i = 0; i < cutpoint; i++) {
121        crossedVariables.Add(this.Variables[variableNames[i]]);
122      }
123      for (int i = cutpoint; i < variables.Count; i++) {
124        crossedVariables.Add(rule.Variables[variableNames[i]]);
125      }
126      IAction action = random.Next(0, 2) == 0 ? this.action : rule.action;
127      return new Rule(crossedVariables, action);
128    }
129
130    public void ApplySplit(IRandom random, double probability) {
131      foreach (var variable in variables.Values) {
132        if (random.NextDouble() < probability)
133          variable.Split(random);
134      }
135    }
136
137    public void ApplyMerge(IRandom random, double probability) {
138      foreach (var variable in variables.Values) {
139        if (random.NextDouble() < probability)
140          variable.Merge(random);
141      }
142    }
143
144    public void ApplyReinitialize(IRandom random, double probability, double oneProbability, IEnumerable<IDiscretizer> discretizers) {
145      foreach (var variable in variables.Values) {
146        if (random.NextDouble() < probability)
147          variable.Reinitialize(random, oneProbability, discretizers);
148      }
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.