[9194] | 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.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Encodings.ConditionActionEncoding;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Encodings.VariableVector {
|
---|
| 31 | [StorableClass]
|
---|
| 32 | [Item("VariableVectorAction", "")]
|
---|
[9242] | 33 | public class VariableVectorAction : Item, IAction {
|
---|
[9194] | 34 |
|
---|
[9242] | 35 | [Storable]
|
---|
| 36 | private Dictionary<string, IActionVariable> variableDictionary;
|
---|
| 37 | public Dictionary<string, IActionVariable> VariableDictionary {
|
---|
| 38 | get { return variableDictionary; }
|
---|
| 39 | protected set { variableDictionary = value; }
|
---|
| 40 | }
|
---|
[9204] | 41 |
|
---|
[9242] | 42 | public IOrderedEnumerable<string> Order { get { return VariableDictionary.Keys.OrderBy(x => x); } }
|
---|
[9194] | 43 |
|
---|
[9242] | 44 | public int VirtualLength { get { return VariableDictionary.Values.Sum(x => x.VirtualLength); } }
|
---|
| 45 |
|
---|
[9194] | 46 | [StorableConstructor]
|
---|
[9226] | 47 | protected VariableVectorAction(bool deserializing) { }
|
---|
| 48 | protected VariableVectorAction(VariableVectorAction original, Cloner cloner) {
|
---|
[9242] | 49 | VariableDictionary = new Dictionary<string, IActionVariable>(original.VariableDictionary.Count);
|
---|
| 50 | foreach (var item in original.VariableDictionary) {
|
---|
| 51 | VariableDictionary.Add(item.Key, cloner.Clone(item.Value));
|
---|
[9226] | 52 | }
|
---|
[9194] | 53 | }
|
---|
[9242] | 54 | public VariableVectorAction()
|
---|
| 55 | : base() {
|
---|
| 56 | VariableDictionary = new Dictionary<string, IActionVariable>();
|
---|
| 57 | }
|
---|
| 58 | public VariableVectorAction(int capacity)
|
---|
| 59 | : base() {
|
---|
| 60 | VariableDictionary = new Dictionary<string, IActionVariable>(capacity);
|
---|
| 61 | }
|
---|
| 62 | public VariableVectorAction(IDictionary<string, IActionVariable> dictionary)
|
---|
| 63 | : base() {
|
---|
| 64 | VariableDictionary = new Dictionary<string, IActionVariable>(dictionary);
|
---|
| 65 | }
|
---|
| 66 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 67 | return new VariableVectorAction(this, cloner);
|
---|
| 68 | }
|
---|
[9226] | 69 |
|
---|
| 70 | public VariableVectorAction GetEmptyCopy() {
|
---|
[9242] | 71 | VariableVectorAction emptyCopy = new VariableVectorAction(VariableDictionary.Count);
|
---|
| 72 | emptyCopy.Add(VariableDictionary.Values.Select(x => x.GetEmptyCopy()));
|
---|
[9226] | 73 | return emptyCopy;
|
---|
[9194] | 74 | }
|
---|
| 75 |
|
---|
[9226] | 76 | public VariableVectorAction GetSetCopy() {
|
---|
[9242] | 77 | VariableVectorAction setCopy = new VariableVectorAction(VariableDictionary.Count);
|
---|
| 78 | setCopy.Add(VariableDictionary.Values.Select(x => x.GetSetCopy()));
|
---|
[9226] | 79 | return setCopy;
|
---|
[9194] | 80 | }
|
---|
| 81 |
|
---|
| 82 | public bool Match(IAction target) {
|
---|
| 83 | var targetCast = target as VariableVectorAction;
|
---|
| 84 | if (targetCast == null) { return false; }
|
---|
[9411] | 85 | foreach (var key in targetCast.VariableDictionary.Keys) {
|
---|
| 86 | if (!VariableDictionary.ContainsKey(key) || !VariableDictionary[key].MatchVariable(targetCast.VariableDictionary[key])) {
|
---|
[9194] | 87 | return false;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | return true;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | public void Add(IEnumerable<IActionVariable> action) {
|
---|
| 94 | foreach (var variable in action) {
|
---|
[9242] | 95 | VariableDictionary.Add(variable.VariableName, variable);
|
---|
[9194] | 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | public override string ToString() {
|
---|
| 100 | StringBuilder sb = new StringBuilder();
|
---|
| 101 | bool first = true;
|
---|
[9242] | 102 | foreach (var variable in VariableDictionary.Values) {
|
---|
[9226] | 103 | if (first) {
|
---|
[9194] | 104 | first = false;
|
---|
| 105 | } else {
|
---|
| 106 | sb.Append(";");
|
---|
| 107 | }
|
---|
| 108 | sb.Append(variable.ToString());
|
---|
| 109 | }
|
---|
| 110 | return sb.ToString();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | public void Randomize(IRandom random) {
|
---|
[9242] | 114 | foreach (var variable in VariableDictionary.Values) {
|
---|
[9467] | 115 | variable.RandomizeAction(random);
|
---|
[9194] | 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[9226] | 119 | public bool Identical(VariableVectorAction target) {
|
---|
[9242] | 120 | if (!this.Order.SequenceEqual(target.Order)) { return false; }
|
---|
| 121 | foreach (var keyValuePair in target.VariableDictionary) {
|
---|
| 122 | if (!VariableDictionary[keyValuePair.Key].Identical(keyValuePair.Value)) {
|
---|
[9194] | 123 | return false;
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | return true;
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | }
|
---|