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 HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Encodings.ConditionActionEncoding {
|
---|
31 | [Item("ActionSetSubsumptionoperator", "Description missing")]
|
---|
32 | [StorableClass]
|
---|
33 | public class ActionSetSubsumptionOperator : SingleSuccessorOperator, IActionSetSubsumption {
|
---|
34 |
|
---|
35 | #region Parameter Properties
|
---|
36 | public ILookupParameter<ItemArray<IClassifier>> ClassifiersParameter {
|
---|
37 | get { return (ILookupParameter<ItemArray<IClassifier>>)Parameters["Classifiers"]; }
|
---|
38 | }
|
---|
39 | public ILookupParameter<ItemArray<IntValue>> NumerositiesParameter {
|
---|
40 | get { return (ILookupParameter<ItemArray<IntValue>>)Parameters["Numerosities"]; }
|
---|
41 | }
|
---|
42 | public ILookupParameter<ItemArray<IntValue>> ExperiencesParameter {
|
---|
43 | get { return (ILookupParameter<ItemArray<IntValue>>)Parameters["Experiences"]; }
|
---|
44 | }
|
---|
45 | public ILookupParameter<ItemArray<DoubleValue>> ErrorsParameter {
|
---|
46 | get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["Errors"]; }
|
---|
47 | }
|
---|
48 | public ILookupParameter<ItemArray<BoolValue>> HasBeenSubsumedParameter {
|
---|
49 | get { return (ILookupParameter<ItemArray<BoolValue>>)Parameters["HasBeenSubsumed"]; }
|
---|
50 | }
|
---|
51 | public ILookupParameter<DoubleValue> ErrorZeroParameter {
|
---|
52 | get { return (ILookupParameter<DoubleValue>)Parameters["ErrorZero"]; }
|
---|
53 | }
|
---|
54 | public ILookupParameter<IntValue> ThetaSubsumptionParameter {
|
---|
55 | get { return (ILookupParameter<IntValue>)Parameters["ThetaSubsumption"]; }
|
---|
56 | }
|
---|
57 | #endregion
|
---|
58 |
|
---|
59 | [StorableConstructor]
|
---|
60 | protected ActionSetSubsumptionOperator(bool deserializing) : base(deserializing) { }
|
---|
61 | protected ActionSetSubsumptionOperator(ActionSetSubsumptionOperator original, Cloner cloner)
|
---|
62 | : base(original, cloner) {
|
---|
63 | }
|
---|
64 | public ActionSetSubsumptionOperator()
|
---|
65 | : base() {
|
---|
66 | Parameters.Add(new ScopeTreeLookupParameter<IClassifier>("Classifiers"));
|
---|
67 | Parameters.Add(new ScopeTreeLookupParameter<IntValue>("Numerosities"));
|
---|
68 | Parameters.Add(new ScopeTreeLookupParameter<IntValue>("Experiences"));
|
---|
69 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Errors"));
|
---|
70 | Parameters.Add(new ScopeTreeLookupParameter<BoolValue>("HasBeenSubsumed"));
|
---|
71 | Parameters.Add(new LookupParameter<DoubleValue>("ErrorZero"));
|
---|
72 | Parameters.Add(new LookupParameter<IntValue>("ThetaSubsumption"));
|
---|
73 | }
|
---|
74 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
75 | return new ActionSetSubsumptionOperator(this, cloner);
|
---|
76 | }
|
---|
77 |
|
---|
78 | public override IOperation Apply() {
|
---|
79 | var classifiers = ClassifiersParameter.ActualValue;
|
---|
80 | var numerosities = NumerositiesParameter.ActualValue;
|
---|
81 | var experiences = ExperiencesParameter.ActualValue;
|
---|
82 | var hasBeenSubsumed = HasBeenSubsumedParameter.ActualValue;
|
---|
83 | var errors = ErrorsParameter.ActualValue;
|
---|
84 | if (errors.Length != classifiers.Length || errors.Length != numerosities.Length
|
---|
85 | || errors.Length != experiences.Length || errors.Length != hasBeenSubsumed.Length) {
|
---|
86 | throw new ArgumentException("The number of classifiers, error, numerosity, hasBeenSubsumed and experience values is not equal.");
|
---|
87 | }
|
---|
88 |
|
---|
89 | int subsumptionClassifier = -1;
|
---|
90 |
|
---|
91 | for (int i = 0; i < classifiers.Length; i++) {
|
---|
92 | if (CouldSubsume(experiences[i], errors[i])) {
|
---|
93 | if (subsumptionClassifier == -1 || classifiers[i].IsMoreGeneral(classifiers[subsumptionClassifier])) {
|
---|
94 | subsumptionClassifier = i;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (subsumptionClassifier != -1) {
|
---|
100 | for (int i = 0; i < classifiers.Length; i++) {
|
---|
101 | if (classifiers[subsumptionClassifier].IsMoreGeneral(classifiers[i])) {
|
---|
102 | numerosities[subsumptionClassifier].Value += numerosities[i].Value;
|
---|
103 | hasBeenSubsumed[i].Value = true;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | return base.Apply();
|
---|
109 | }
|
---|
110 |
|
---|
111 | private bool CouldSubsume(IntValue exp, DoubleValue error) {
|
---|
112 | return exp.Value > ThetaSubsumptionParameter.ActualValue.Value && error.Value < ErrorZeroParameter.ActualValue.Value;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|