#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("VariableVectorCondition", "")]
public class VariableVectorCondition : Item, ICondition {
[Storable]
private Dictionary variableDictionary;
public Dictionary VariableDictionary {
get { return variableDictionary; }
protected set { variableDictionary = value; }
}
public int VirtualLength { get { return VariableDictionary.Values.Sum(x => x.VirtualLength); } }
[StorableConstructor]
protected VariableVectorCondition(bool deserializing) { }
protected VariableVectorCondition(VariableVectorCondition original, Cloner cloner) {
VariableDictionary = new Dictionary(original.VariableDictionary.Count);
foreach (var item in original.VariableDictionary) {
VariableDictionary.Add(item.Key, cloner.Clone(item.Value));
}
}
public VariableVectorCondition()
: base() {
VariableDictionary = new Dictionary();
}
public VariableVectorCondition(int capacity)
: base() {
VariableDictionary = new Dictionary(capacity);
}
public VariableVectorCondition(IDictionary dictionary)
: base() {
VariableDictionary = new Dictionary(dictionary);
}
public override IDeepCloneable Clone(Cloner cloner) {
return new VariableVectorCondition(this, cloner);
}
public bool Match(IInput target) {
var targetCast = target as VariableVectorInput;
if (targetCast == null) { return false; }
foreach (var key in VariableDictionary.Keys) {
if (!targetCast.InputDictionary.ContainsKey(key) || !VariableDictionary[key].MatchInput(targetCast.InputDictionary[key])) {
return false;
}
}
return true;
}
public void Add(IEnumerable condition) {
foreach (var variable in condition) {
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, double changeSymbolProbability, double spreadPercentage) {
foreach (var variable in VariableDictionary.Values) {
if (variable is DoubleVariable) {
((DoubleVariable)variable).Randomize(random, changeSymbolProbability, spreadPercentage);
} else {
variable.Randomize(random, changeSymbolProbability);
}
}
}
public bool Identical(VariableVectorCondition target) {
var thisEnumerator = VariableDictionary.GetEnumerator();
var targetEnumerator = target.VariableDictionary.GetEnumerator();
while (thisEnumerator.MoveNext() && targetEnumerator.MoveNext()) {
if (thisEnumerator.Current.Key != targetEnumerator.Current.Key ||
!thisEnumerator.Current.Value.Identical(targetEnumerator.Current.Value)) {
return false;
}
}
return !thisEnumerator.MoveNext() && !targetEnumerator.MoveNext();
}
}
}