[9089] | 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.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Encodings.ConditionActionEncoding {
|
---|
| 32 | [Item("CoveringOperator", "")]
|
---|
| 33 | [StorableClass]
|
---|
| 34 | public class CoveringOperator : SingleSuccessorOperator, ICovering {
|
---|
| 35 |
|
---|
| 36 | #region Parameter Properties
|
---|
| 37 | public ILookupParameter<IClassifier> CoverClassifierParameter {
|
---|
| 38 | get { return (ILookupParameter<IClassifier>)Parameters["CoverClassifier"]; }
|
---|
| 39 | }
|
---|
| 40 | public ILookupParameter<IItemSet<IClassifier>> ActionsInMatchSetParameter {
|
---|
| 41 | get { return (ILookupParameter<IItemSet<IClassifier>>)Parameters["ActionsInMatchSet"]; }
|
---|
| 42 | }
|
---|
| 43 | public ILookupParameter<IItemSet<IClassifier>> PossibleActionsParameter {
|
---|
| 44 | get { return (ILookupParameter<IItemSet<IClassifier>>)Parameters["PossibleActions"]; }
|
---|
| 45 | }
|
---|
| 46 | public ILookupParameter<IntValue> MinimalNumberOfUniqueActionsParameter {
|
---|
| 47 | get { return (ILookupParameter<IntValue>)Parameters["MinimalNumberOfUniqueActions"]; }
|
---|
| 48 | }
|
---|
| 49 | public IValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
| 50 | get { return (IValueLookupParameter<IOperator>)Parameters[" Evaluator"]; }
|
---|
| 51 | }
|
---|
| 52 | public IValueLookupParameter<ICoveringSolutionCreator> SolutionCreatorParameter {
|
---|
| 53 | get { return (IValueLookupParameter<ICoveringSolutionCreator>)Parameters["SolutionCreator"]; }
|
---|
| 54 | }
|
---|
| 55 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 56 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 57 | }
|
---|
| 58 | public IValueLookupParameter<BoolValue> ParallelParameter {
|
---|
| 59 | get { return (IValueLookupParameter<BoolValue>)Parameters["Parallel"]; }
|
---|
| 60 | }
|
---|
| 61 | private ScopeParameter CurrentScopeParameter {
|
---|
| 62 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
| 63 | }
|
---|
| 64 | public IScope CurrentScope {
|
---|
| 65 | get { return CurrentScopeParameter.ActualValue; }
|
---|
| 66 | }
|
---|
| 67 | #endregion
|
---|
| 68 |
|
---|
| 69 | [StorableConstructor]
|
---|
| 70 | protected CoveringOperator(bool deserializing) : base(deserializing) { }
|
---|
| 71 | protected CoveringOperator(CoveringOperator original, Cloner cloner)
|
---|
| 72 | : base(original, cloner) {
|
---|
| 73 | }
|
---|
| 74 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 75 | return new CoveringOperator(this, cloner);
|
---|
| 76 | }
|
---|
| 77 | public CoveringOperator()
|
---|
| 78 | : base() {
|
---|
| 79 | Parameters.Add(new LookupParameter<IClassifier>("CoverClassifier"));
|
---|
| 80 | Parameters.Add(new ScopeTreeLookupParameter<IClassifier>("ActionsInMatchSet"));
|
---|
| 81 | Parameters.Add(new ScopeTreeLookupParameter<IClassifier>("PossibleActions"));
|
---|
| 82 | Parameters.Add(new LookupParameter<IntValue>("MinimalNumberOfUniqueActions"));
|
---|
| 83 | Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator"));
|
---|
| 84 | Parameters.Add(new ValueLookupParameter<ICoveringSolutionCreator>("SolutionCreator"));
|
---|
| 85 | Parameters.Add(new LookupParameter<IRandom>("Random"));
|
---|
| 86 | Parameters.Add(new ValueLookupParameter<BoolValue>("Parallel", "True if the operator should be applied in parallel on all sub-scopes, otherwise false.", new BoolValue(true)));
|
---|
| 87 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope to which the new solutions are added as sub-scopes."));
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | public override IOperation Apply() {
|
---|
| 91 | int count = MinimalNumberOfUniqueActionsParameter.ActualValue.Value - ActionsInMatchSetParameter.ActualValue.Count;
|
---|
| 92 | ICoveringSolutionCreator creator = SolutionCreatorParameter.ActualValue;
|
---|
| 93 | IOperator evaluator = EvaluatorParameter.ActualValue;
|
---|
| 94 | bool parallel = ParallelParameter.ActualValue.Value;
|
---|
| 95 |
|
---|
| 96 | IItemSet<IClassifier> clone = (IItemSet<IClassifier>)PossibleActionsParameter.ActualValue.Clone();
|
---|
| 97 | clone.ExceptWith(ActionsInMatchSetParameter.ActualValue);
|
---|
| 98 |
|
---|
| 99 | if (count > clone.Count) {
|
---|
| 100 | throw new ArgumentException("More classifiers with unique actions shall be created than unique actions are available.");
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | int current = CurrentScope.SubScopes.Count;
|
---|
| 104 | for (int i = 0; i < count; i++) {
|
---|
| 105 | CurrentScope.SubScopes.Add(new Scope((current + i).ToString()));
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | creator.ActionParameter.ActualName = "Action";
|
---|
| 109 | OperationCollection variableCreation = new OperationCollection() { Parallel = parallel };
|
---|
| 110 | OperationCollection creation = new OperationCollection();
|
---|
| 111 | OperationCollection evaluation = new OperationCollection() { Parallel = parallel };
|
---|
| 112 | for (int i = 0; i < count; i++) {
|
---|
| 113 | VariableCreator variableCreator = new VariableCreator();
|
---|
| 114 | int pos = RandomParameter.ActualValue.Next(clone.Count);
|
---|
| 115 | IClassifier action = clone.ElementAt(pos);
|
---|
| 116 | clone.Remove(action);
|
---|
| 117 | variableCreator.CollectedValues.Add(new ValueParameter<IClassifier>("Action", action));
|
---|
| 118 | variableCreation.Add(ExecutionContext.CreateOperation(variableCreator, CurrentScope.SubScopes[current + i]));
|
---|
| 119 | creation.Add(ExecutionContext.CreateOperation(creator, CurrentScope.SubScopes[current + i]));
|
---|
| 120 | evaluation.Add(ExecutionContext.CreateOperation(evaluator, CurrentScope.SubScopes[current + i]));
|
---|
| 121 | }
|
---|
| 122 | OperationCollection next = new OperationCollection();
|
---|
| 123 | next.Add(variableCreation);
|
---|
| 124 | next.Add(creation);
|
---|
| 125 | next.Add(evaluation);
|
---|
| 126 | next.Add(base.Apply());
|
---|
| 127 | return next;
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 | }
|
---|