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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Optimization.Operators.LCS;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Encodings.DecisionList {
|
---|
31 | [StorableClass]
|
---|
32 | [Item("DecisionList", "")]
|
---|
33 | public class DecisionList : Item, IGAssistIndividual {
|
---|
34 | [Storable]
|
---|
35 | private IList<Rule> rules;
|
---|
36 | public IList<Rule> Rules {
|
---|
37 | get { return rules; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | [Storable]
|
---|
41 | private IAction defaultAction;
|
---|
42 | public IAction DefaultAction {
|
---|
43 | get { return defaultAction; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | // default rule (action) is part of the rule set size
|
---|
47 | public int RuleSetSize {
|
---|
48 | get { return defaultAction == null ? rules.Count : rules.Count + 1; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | [StorableConstructor]
|
---|
52 | protected DecisionList(bool deserializing) : base(deserializing) { }
|
---|
53 | protected DecisionList(DecisionList original, Cloner cloner)
|
---|
54 | : base(original, cloner) {
|
---|
55 | defaultAction = original.DefaultAction;
|
---|
56 | rules = new List<Rule>(original.rules.Count);
|
---|
57 | foreach (var rule in original.Rules) {
|
---|
58 | rules.Add(cloner.Clone(rule));
|
---|
59 | }
|
---|
60 | }
|
---|
61 | public DecisionList()
|
---|
62 | : base() {
|
---|
63 | rules = new List<Rule>();
|
---|
64 | defaultAction = null;
|
---|
65 | }
|
---|
66 | public DecisionList(List<Rule> rules)
|
---|
67 | : base() {
|
---|
68 | this.rules = rules;
|
---|
69 | this.defaultAction = null;
|
---|
70 | }
|
---|
71 | public DecisionList(List<Rule> rules, IAction defaultAction)
|
---|
72 | : base() {
|
---|
73 | if (rules.Any(x => x.Action.Match(defaultAction))) { throw new ArgumentException("If a default action is used. The default action is not allowed be used in any rule."); }
|
---|
74 | this.rules = rules;
|
---|
75 | this.defaultAction = defaultAction;
|
---|
76 | }
|
---|
77 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
78 | return new DecisionList(this, cloner);
|
---|
79 | }
|
---|
80 |
|
---|
81 | // for convenience
|
---|
82 | public IEnumerable<IAction> Evaluate(IEnumerable<IGAssistInput> input) {
|
---|
83 | ItemSet<Rule> aliveRules;
|
---|
84 | return Evaluate(input, out aliveRules);
|
---|
85 | }
|
---|
86 | // for convenience
|
---|
87 | public IEnumerable<IAction> Evaluate(IEnumerable<IGAssistInput> input, out ItemSet<Rule> aliveRules) {
|
---|
88 | double theoryLengtgh;
|
---|
89 | return Evaluate(input, out aliveRules, out theoryLengtgh);
|
---|
90 | }
|
---|
91 | public IEnumerable<IAction> Evaluate(IEnumerable<IGAssistInput> input, out ItemSet<Rule> aliveRules, out double theoryLength) {
|
---|
92 | var estimated = new List<IAction>();
|
---|
93 | var activatedRules = new ItemSet<Rule>();
|
---|
94 | int count = 0;
|
---|
95 | foreach (var dli in input) {
|
---|
96 | count++;
|
---|
97 | foreach (var rule in rules) {
|
---|
98 | if (rule.MatchInput(dli)) {
|
---|
99 | estimated.Add(rule.Action);
|
---|
100 | activatedRules.Add(rule);
|
---|
101 | break;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | if (count > estimated.Count) {
|
---|
105 | estimated.Add(defaultAction);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | aliveRules = activatedRules;
|
---|
109 | theoryLength = activatedRules.Sum(x => x.ComputeTheoryLength());
|
---|
110 |
|
---|
111 | return estimated;
|
---|
112 | }
|
---|
113 |
|
---|
114 | public void RemoveRules(IEnumerable<Rule> deadRules) {
|
---|
115 | foreach (var deadRule in deadRules) {
|
---|
116 | Rules.Remove(deadRule);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | #region IGAssistIndividual Members
|
---|
121 |
|
---|
122 | public IGAssistNiche Niche {
|
---|
123 | get { return defaultAction; }
|
---|
124 | }
|
---|
125 |
|
---|
126 | public void SetNiche(IRandom random, IGAssistNiche niche) {
|
---|
127 | var action = niche as IAction;
|
---|
128 | if (action == null) {
|
---|
129 | throw new ArgumentException("Niche has to be an action");
|
---|
130 | }
|
---|
131 | if (Niche != null) {
|
---|
132 | throw new ArgumentException("Niche has already been set. It cannot be set again.");
|
---|
133 | }
|
---|
134 | defaultAction = action;
|
---|
135 | var except = new List<IAction>() { action };
|
---|
136 | foreach (var rule in rules.Where(x => x.Action.SameNiche(niche))) {
|
---|
137 | rule.Action.Randomize(random, except);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | public void ApplySplit(IRandom random, double probability) {
|
---|
142 | foreach (var rule in rules) {
|
---|
143 | rule.ApplySplit(random, probability);
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | public void ApplyMerge(IRandom random, double probability) {
|
---|
148 | foreach (var rule in rules) {
|
---|
149 | rule.ApplyMerge(random, probability);
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | public void ApplyReinitialize(IRandom random, double probability, double oneProbability, IEnumerable<IDiscretizer> discretizers) {
|
---|
154 | foreach (var rule in rules) {
|
---|
155 | rule.ApplyReinitialize(random, probability, oneProbability, discretizers);
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | public IGAssistSolution CreateGAssistSolution(IGAssistProblemData problemData) {
|
---|
160 | return new DecisionListSolution(this, problemData);
|
---|
161 | }
|
---|
162 | #endregion
|
---|
163 |
|
---|
164 | #region IGAssistModel Members
|
---|
165 | IEnumerable<IGAssistNiche> IGAssistModel.Evaluate(IEnumerable<IGAssistInput> input) {
|
---|
166 | return Evaluate(input);
|
---|
167 | }
|
---|
168 | public IEnumerable<IGAssistNiche> Evaluate(IEnumerable<IGAssistInput> input, out ItemSet<IItem> aliveRules, out double theoryLength) {
|
---|
169 | return Evaluate(input, out aliveRules, out theoryLength);
|
---|
170 | }
|
---|
171 | #endregion
|
---|
172 | }
|
---|
173 | }
|
---|