[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;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[9226] | 24 | using System.Drawing;
|
---|
[9194] | 25 | using System.Linq;
|
---|
| 26 | using System.Text;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Encodings.ConditionActionEncoding;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Encodings.VariableVector {
|
---|
| 33 | [StorableClass]
|
---|
| 34 | [Item("VariableVectorAction", "")]
|
---|
[9226] | 35 | public class VariableVectorAction : Dictionary<string, IActionVariable>, IAction {
|
---|
[9194] | 36 |
|
---|
[9226] | 37 | public IOrderedEnumerable<string> Order { get { return this.Keys.OrderBy(x => x); } }
|
---|
[9204] | 38 |
|
---|
[9194] | 39 | public int VirtualLength { get { return this.Values.Sum(x => x.VirtualLength); } }
|
---|
| 40 |
|
---|
| 41 | [StorableConstructor]
|
---|
[9226] | 42 | protected VariableVectorAction(bool deserializing) { }
|
---|
| 43 | protected VariableVectorAction(VariableVectorAction original, Cloner cloner) {
|
---|
| 44 | foreach (var item in original) {
|
---|
| 45 | Add((string)item.Key.Clone(), (IActionVariable)item.Value.Clone());
|
---|
| 46 | }
|
---|
[9194] | 47 | }
|
---|
| 48 | public VariableVectorAction() : base() { }
|
---|
| 49 | public VariableVectorAction(int capacity) : base(capacity) { }
|
---|
[9226] | 50 | public VariableVectorAction(IDictionary<string, IActionVariable> dictionary) : base(dictionary) { }
|
---|
| 51 |
|
---|
| 52 | //protected override void OnItemsAdded(IEnumerable<KeyValuePair<string, IActionVariable>> items) {
|
---|
| 53 | // foreach (var item in items) {
|
---|
| 54 | // if (!(item.Value is StringVariable || item.Value is IntVariable)) {
|
---|
| 55 | // throw new ArgumentException("Value has to be of type 'StringVariable' or 'IntVariable'");
|
---|
| 56 | // }
|
---|
| 57 | // }
|
---|
| 58 | //}
|
---|
| 59 |
|
---|
| 60 | public VariableVectorAction GetEmptyCopy() {
|
---|
| 61 | VariableVectorAction emptyCopy = new VariableVectorAction(this.Count);
|
---|
| 62 | foreach (var keyValuePair in this) {
|
---|
| 63 | emptyCopy.Add(keyValuePair.Key, keyValuePair.Value.GetEmptyCopy());
|
---|
| 64 | }
|
---|
| 65 | return emptyCopy;
|
---|
[9194] | 66 | }
|
---|
| 67 |
|
---|
[9226] | 68 | public VariableVectorAction GetSetCopy() {
|
---|
| 69 | VariableVectorAction setCopy = new VariableVectorAction(this.Count);
|
---|
| 70 | foreach (var keyValuePair in this) {
|
---|
| 71 | setCopy.Add(keyValuePair.Key, keyValuePair.Value.GetSetCopy());
|
---|
[9194] | 72 | }
|
---|
[9226] | 73 | return setCopy;
|
---|
[9194] | 74 | }
|
---|
| 75 |
|
---|
| 76 | public bool Match(IAction target) {
|
---|
| 77 | var targetCast = target as VariableVectorAction;
|
---|
| 78 | if (targetCast == null) { return false; }
|
---|
[9226] | 79 | if (!this.Order.SequenceEqual(targetCast.Order)) { return false; }
|
---|
[9194] | 80 | foreach (var keyValuePair in targetCast) {
|
---|
| 81 | if (!this[keyValuePair.Key].MatchVariable(keyValuePair.Value)) {
|
---|
| 82 | return false;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | return true;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | public void Add(IEnumerable<IActionVariable> action) {
|
---|
| 89 | foreach (var variable in action) {
|
---|
[9226] | 90 | this.Add(variable.VariableName, variable);
|
---|
[9194] | 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | public override string ToString() {
|
---|
| 95 | StringBuilder sb = new StringBuilder();
|
---|
| 96 | bool first = true;
|
---|
| 97 | foreach (var variable in this.Values) {
|
---|
[9226] | 98 | if (first) {
|
---|
[9194] | 99 | first = false;
|
---|
| 100 | } else {
|
---|
| 101 | sb.Append(";");
|
---|
| 102 | }
|
---|
| 103 | sb.Append(variable.ToString());
|
---|
| 104 | }
|
---|
| 105 | return sb.ToString();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | public void Randomize(IRandom random) {
|
---|
| 109 | foreach (var variable in this.Values) {
|
---|
| 110 | variable.Randomize(random);
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[9226] | 114 | public bool Identical(VariableVectorAction target) {
|
---|
[9204] | 115 | if (this.Order.SequenceEqual(target.Order)) { return false; }
|
---|
[9194] | 116 | foreach (var keyValuePair in target) {
|
---|
| 117 | if (!this[keyValuePair.Key].Identical(keyValuePair.Value)) {
|
---|
| 118 | return false;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | return true;
|
---|
| 122 | }
|
---|
[9226] | 123 |
|
---|
| 124 | public virtual string ItemName {
|
---|
| 125 | get { return ItemAttribute.GetName(this.GetType()); }
|
---|
| 126 | }
|
---|
| 127 | public virtual string ItemDescription {
|
---|
| 128 | get { return ItemAttribute.GetDescription(this.GetType()); }
|
---|
| 129 | }
|
---|
| 130 | public Version ItemVersion {
|
---|
| 131 | get { return ItemAttribute.GetVersion(this.GetType()); }
|
---|
| 132 | }
|
---|
| 133 | public static Image StaticItemImage {
|
---|
| 134 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
|
---|
| 135 | }
|
---|
| 136 | public virtual Image ItemImage {
|
---|
| 137 | get { return ItemAttribute.GetImage(this.GetType()); }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | public virtual IDeepCloneable Clone(Cloner cloner) {
|
---|
| 141 | return new VariableVectorAction(this, cloner);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | public object Clone() {
|
---|
| 145 | return Clone(new Cloner());
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | public event EventHandler ItemImageChanged;
|
---|
| 149 | protected virtual void OnItemImageChanged() {
|
---|
| 150 | EventHandler handler = ItemImageChanged;
|
---|
| 151 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 152 | }
|
---|
| 153 | public event EventHandler ToStringChanged;
|
---|
| 154 | protected virtual void OnToStringChanged() {
|
---|
| 155 | EventHandler handler = ToStringChanged;
|
---|
| 156 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 157 | }
|
---|
[9194] | 158 | }
|
---|
| 159 | }
|
---|