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.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Encodings.DecisionList {
|
---|
30 | [StorableClass]
|
---|
31 | [Item("DecisionList", "")]
|
---|
32 | public class DecisionList : Item {
|
---|
33 | [Storable]
|
---|
34 | private IList<Rule> rules;
|
---|
35 | public IList<Rule> Rules {
|
---|
36 | get { return rules; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | [Storable]
|
---|
40 | private IAction defaultAction;
|
---|
41 | public IAction DefaultAction {
|
---|
42 | get { return defaultAction; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | [StorableConstructor]
|
---|
46 | protected DecisionList(bool deserializing) : base(deserializing) { }
|
---|
47 | protected DecisionList(DecisionList original, Cloner cloner)
|
---|
48 | : base(original, cloner) {
|
---|
49 | }
|
---|
50 | public DecisionList()
|
---|
51 | : base() {
|
---|
52 | rules = new List<Rule>();
|
---|
53 | defaultAction = null;
|
---|
54 | }
|
---|
55 | public DecisionList(List<Rule> rules)
|
---|
56 | : base() {
|
---|
57 | this.rules = rules;
|
---|
58 | defaultAction = null;
|
---|
59 | }
|
---|
60 | public DecisionList(List<Rule> rules, IAction defaultAction)
|
---|
61 | : base() {
|
---|
62 | 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."); }
|
---|
63 | this.rules = rules;
|
---|
64 | this.defaultAction = defaultAction;
|
---|
65 | }
|
---|
66 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
67 | return new DecisionList(this, cloner);
|
---|
68 | }
|
---|
69 |
|
---|
70 | // for convenience
|
---|
71 | public IEnumerable<IAction> Evaluate(IEnumerable<DecisionListInput> input) {
|
---|
72 | int numberOfAliveRules;
|
---|
73 | return Evaluate(input, out numberOfAliveRules);
|
---|
74 | }
|
---|
75 | // for convenience
|
---|
76 | public IEnumerable<IAction> Evaluate(IEnumerable<DecisionListInput> input, out int numberOfAliveRules) {
|
---|
77 | double theoryLengtgh;
|
---|
78 | return Evaluate(input, out numberOfAliveRules, out theoryLengtgh);
|
---|
79 | }
|
---|
80 | public IEnumerable<IAction> Evaluate(IEnumerable<DecisionListInput> input, out int numberOfAliveRules, out double theoryLengtgh) {
|
---|
81 | var estimated = new List<IAction>();
|
---|
82 | var activatedRules = new HashSet<Rule>();
|
---|
83 | foreach (var dli in input) {
|
---|
84 | foreach (var rule in rules) {
|
---|
85 | if (rule.MatchInput(dli)) {
|
---|
86 | estimated.Add(rule.Action);
|
---|
87 | activatedRules.Add(rule);
|
---|
88 | break;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 | numberOfAliveRules = activatedRules.Count;
|
---|
93 | theoryLengtgh = activatedRules.Sum(x => x.ComputeTheoryLength());
|
---|
94 |
|
---|
95 | return estimated;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|