Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • added be project Optimization.Operators.LCS
  • added default rule strategies for GAssist
File size: 4.7 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    [StorableConstructor]
46    protected Rule(bool deserializing) : base(deserializing) { }
47    protected Rule(Rule original, Cloner cloner)
48      : base(original, cloner) {
49      variables = new Dictionary<string, IVariable>(original.variables.Count);
50      foreach (var item in original.variables) {
51        variables.Add(item.Key, cloner.Clone(item.Value));
52      }
53      action = (IAction)original.action.Clone();
54    }
55    public Rule()
56      : base() {
57      variables = new Dictionary<string, IVariable>();
58    }
59    public Rule(IEnumerable<IVariable> condition, IAction action)
60      : this() {
61      foreach (var variable in condition) {
62        variables.Add(variable.VariableName, variable);
63      }
64      this.action = action;
65    }
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new Rule(this, cloner);
68    }
69
70    public override string ToString() {
71      StringBuilder sb = new StringBuilder();
72      foreach (var variable in Variables) {
73        sb.Append(variable.Value + "|");
74      }
75      sb.Append(Action);
76      return sb.ToString();
77    }
78
79    public void Randomize(IRandom random, double onePercentage, IEnumerable<IDiscretizer> discretizer) {
80      foreach (var variable in variables.Values) {
81        variable.Randomize(random, onePercentage, discretizer);
82      }
83      action.Randomize(random);
84    }
85
86    public double ComputeTheoryLength() {
87      return variables.Sum(x => x.Value.ComputeTheoryLength());
88    }
89
90    public bool MatchInput(DecisionListInput target) {
91      foreach (var variable in variables) {
92        if (!target.InputDictionary.ContainsKey(variable.Key)) { throw new ArgumentException("Input doesn't contain variable"); }
93        if (!variable.Value.Match(target.InputDictionary[variable.Key])) { return false; }
94      }
95      return true;
96    }
97
98    public Rule Crossover(Rule rule, IRandom random) {
99      //variables and rule.Variables have to be the same
100      int cutpoint = random.Next(0, Variables.Count);
101      var crossedVariables = new List<IVariable>(variables.Count);
102      var variableNames = variables.Keys.ToList();
103      for (int i = 0; i < cutpoint; i++) {
104        crossedVariables.Add(this.Variables[variableNames[i]]);
105      }
106      for (int i = cutpoint; i < variables.Count; i++) {
107        crossedVariables.Add(rule.Variables[variableNames[i]]);
108      }
109      IAction action = random.Next(0, 2) == 0 ? this.action : rule.action;
110      return new Rule(crossedVariables, action);
111    }
112
113    public void ApplySplit(IRandom random, double probability) {
114      foreach (var variable in variables.Values) {
115        if (random.NextDouble() < probability)
116          variable.Split(random);
117      }
118    }
119
120    public void ApplyMerge(IRandom random, double probability) {
121      foreach (var variable in variables.Values) {
122        if (random.NextDouble() < probability)
123          variable.Merge(random);
124      }
125    }
126
127    public void ApplyReinitialize(IRandom random, double probability, double oneProbability, IEnumerable<IDiscretizer> discretizers) {
128      foreach (var variable in variables.Values) {
129        if (random.NextDouble() < probability)
130          variable.Reinitialize(random, oneProbability, discretizers);
131      }
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.