[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
|
---|
[9194] | 37 | public ILookupParameter<IItemSet<IAction>> ActionsInMatchSetParameter {
|
---|
| 38 | get { return (ILookupParameter<IItemSet<IAction>>)Parameters["ActionsInMatchSet"]; }
|
---|
[9089] | 39 | }
|
---|
[9194] | 40 | public ILookupParameter<IItemSet<IAction>> PossibleActionsParameter {
|
---|
| 41 | get { return (ILookupParameter<IItemSet<IAction>>)Parameters["PossibleActions"]; }
|
---|
[9089] | 42 | }
|
---|
| 43 | public ILookupParameter<IntValue> MinimalNumberOfUniqueActionsParameter {
|
---|
| 44 | get { return (ILookupParameter<IntValue>)Parameters["MinimalNumberOfUniqueActions"]; }
|
---|
| 45 | }
|
---|
| 46 | public IValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
[9090] | 47 | get { return (IValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
[9089] | 48 | }
|
---|
| 49 | public IValueLookupParameter<ICoveringSolutionCreator> SolutionCreatorParameter {
|
---|
| 50 | get { return (IValueLookupParameter<ICoveringSolutionCreator>)Parameters["SolutionCreator"]; }
|
---|
| 51 | }
|
---|
| 52 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 53 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 54 | }
|
---|
[9105] | 55 | public ILookupParameter<IntValue> CurrentPopulationSizeParameter {
|
---|
| 56 | get { return (ILookupParameter<IntValue>)Parameters["CurrentPopulationSize"]; }
|
---|
| 57 | }
|
---|
[9089] | 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() {
|
---|
[9194] | 79 | Parameters.Add(new LookupParameter<IItemSet<IAction>>("ActionsInMatchSet"));
|
---|
| 80 | Parameters.Add(new LookupParameter<IItemSet<IAction>>("PossibleActions"));
|
---|
[9089] | 81 | Parameters.Add(new LookupParameter<IntValue>("MinimalNumberOfUniqueActions"));
|
---|
| 82 | Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator"));
|
---|
| 83 | Parameters.Add(new ValueLookupParameter<ICoveringSolutionCreator>("SolutionCreator"));
|
---|
| 84 | Parameters.Add(new LookupParameter<IRandom>("Random"));
|
---|
| 85 | Parameters.Add(new ValueLookupParameter<BoolValue>("Parallel", "True if the operator should be applied in parallel on all sub-scopes, otherwise false.", new BoolValue(true)));
|
---|
| 86 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope to which the new solutions are added as sub-scopes."));
|
---|
[9105] | 87 | Parameters.Add(new LookupParameter<IntValue>("CurrentPopulationSize"));
|
---|
[9089] | 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 |
|
---|
[9194] | 96 | IItemSet<IAction> clone = (IItemSet<IAction>)PossibleActionsParameter.ActualValue.Clone();
|
---|
[9089] | 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 | }
|
---|
[9110] | 102 | if (count > 0) {
|
---|
| 103 | CurrentPopulationSizeParameter.ActualValue.Value += count;
|
---|
[9089] | 104 |
|
---|
[9110] | 105 | int current = CurrentScope.SubScopes.Count;
|
---|
| 106 | for (int i = 0; i < count; i++) {
|
---|
| 107 | CurrentScope.SubScopes.Add(new Scope((current + i).ToString()));
|
---|
| 108 | }
|
---|
[9105] | 109 |
|
---|
[9110] | 110 | creator.ActionParameter.ActualName = "Action";
|
---|
| 111 | OperationCollection variableCreation = new OperationCollection() { Parallel = parallel };
|
---|
| 112 | OperationCollection creation = new OperationCollection();
|
---|
| 113 | OperationCollection evaluation = new OperationCollection() { Parallel = parallel };
|
---|
| 114 | for (int i = 0; i < count; i++) {
|
---|
| 115 | VariableCreator variableCreator = new VariableCreator();
|
---|
| 116 | int pos = RandomParameter.ActualValue.Next(clone.Count);
|
---|
[9194] | 117 | IAction action = clone.ElementAt(pos);
|
---|
[9110] | 118 | clone.Remove(action);
|
---|
[9194] | 119 | variableCreator.CollectedValues.Add(new ValueParameter<IAction>("Action", action));
|
---|
[9110] | 120 | variableCreation.Add(ExecutionContext.CreateOperation(variableCreator, CurrentScope.SubScopes[current + i]));
|
---|
| 121 | creation.Add(ExecutionContext.CreateOperation(creator, CurrentScope.SubScopes[current + i]));
|
---|
| 122 | evaluation.Add(ExecutionContext.CreateOperation(evaluator, CurrentScope.SubScopes[current + i]));
|
---|
| 123 | }
|
---|
| 124 | OperationCollection next = new OperationCollection();
|
---|
| 125 | next.Add(variableCreation);
|
---|
| 126 | next.Add(creation);
|
---|
| 127 | next.Add(evaluation);
|
---|
| 128 | next.Add(base.Apply());
|
---|
| 129 | return next;
|
---|
| 130 | } else {
|
---|
| 131 | return base.Apply();
|
---|
[9089] | 132 | }
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | }
|
---|