#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.ConditionActionEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Encodings.VariableVector { [StorableClass] [Item("VariableVectorAction", "")] public class VariableVectorAction : Item, IAction { [Storable] private Dictionary variableDictionary; public Dictionary VariableDictionary { get { return variableDictionary; } protected set { variableDictionary = value; } } public IOrderedEnumerable Order { get { return VariableDictionary.Keys.OrderBy(x => x); } } public int VirtualLength { get { return VariableDictionary.Values.Sum(x => x.VirtualLength); } } [StorableConstructor] protected VariableVectorAction(bool deserializing) { } protected VariableVectorAction(VariableVectorAction original, Cloner cloner) { VariableDictionary = new Dictionary(original.VariableDictionary.Count); foreach (var item in original.VariableDictionary) { VariableDictionary.Add(item.Key, cloner.Clone(item.Value)); } } public VariableVectorAction() : base() { VariableDictionary = new Dictionary(); } public VariableVectorAction(int capacity) : base() { VariableDictionary = new Dictionary(capacity); } public VariableVectorAction(IDictionary dictionary) : base() { VariableDictionary = new Dictionary(dictionary); } public override IDeepCloneable Clone(Cloner cloner) { return new VariableVectorAction(this, cloner); } public VariableVectorAction GetEmptyCopy() { VariableVectorAction emptyCopy = new VariableVectorAction(VariableDictionary.Count); emptyCopy.Add(VariableDictionary.Values.Select(x => x.GetEmptyCopy())); return emptyCopy; } public VariableVectorAction GetSetCopy() { VariableVectorAction setCopy = new VariableVectorAction(VariableDictionary.Count); setCopy.Add(VariableDictionary.Values.Select(x => x.GetSetCopy())); return setCopy; } public bool Match(IAction target) { var targetCast = target as VariableVectorAction; if (targetCast == null) { return false; } foreach (var key in targetCast.VariableDictionary.Keys) { if (!VariableDictionary.ContainsKey(key) || !VariableDictionary[key].MatchVariable(targetCast.VariableDictionary[key])) { return false; } } return true; } public void Add(IEnumerable action) { foreach (var variable in action) { VariableDictionary.Add(variable.VariableName, variable); } } public override string ToString() { StringBuilder sb = new StringBuilder(); bool first = true; foreach (var variable in VariableDictionary.Values) { if (first) { first = false; } else { sb.Append(";"); } sb.Append(variable.ToString()); } return sb.ToString(); } public void Randomize(IRandom random) { foreach (var variable in VariableDictionary.Values) { variable.RandomizeAction(random); } } public bool Identical(VariableVectorAction target) { if (!this.Order.SequenceEqual(target.Order)) { return false; } foreach (var keyValuePair in target.VariableDictionary) { if (!VariableDictionary[keyValuePair.Key].Identical(keyValuePair.Value)) { return false; } } return true; } } }