#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;
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("VariableVector", "")]
public class VariableVector : Item, IClassifier {
[Storable]
protected VariableVectorCondition condition;
public VariableVectorCondition Condition { get { return condition; } }
[Storable]
protected VariableVectorAction action;
public VariableVectorAction Action { get { return action; } }
public int Count {
get { return condition.VariableDictionary.Count + action.VariableDictionary.Count; }
}
public int VirtualLength {
get { return condition.VariableDictionary.Values.Sum(x => x.VirtualLength) + action.VariableDictionary.Values.Sum(x => x.VirtualLength); }
}
[StorableConstructor]
protected VariableVector(bool deserializing) : base(deserializing) { }
protected VariableVector(VariableVector original, Cloner cloner)
: base(original, cloner) {
condition = (VariableVectorCondition)original.condition.Clone();
action = (VariableVectorAction)original.action;
}
public VariableVector() {
condition = new VariableVectorCondition();
action = new VariableVectorAction();
}
public VariableVector(IEnumerable condition, IEnumerable action)
: this() {
if (condition == null || action == null) { throw new ArgumentNullException(); }
if (!action.All(x => x is IActionVariable)) { throw new ArgumentException("Action can only use IActionVariable"); }
this.Condition.Add(condition);
this.Action.Add(action.Cast());
}
public override IDeepCloneable Clone(Cloner cloner) {
return new VariableVector(this, cloner);
}
public VariableVector GetEmptyCopy() {
return new VariableVector(Condition.VariableDictionary.Values.Select(x => x.GetEmptyCopy()), Action.VariableDictionary.Values.Select(x => x.GetEmptyCopy()));
}
public override string ToString() {
StringBuilder sb = new StringBuilder();
sb.Append("[");
sb.Append(condition.ToString());
sb.Append("||");
sb.Append(action.ToString());
sb.Append("]");
return sb.ToString();
}
public void Randomize(IRandom random, double changeSymbolProbability, double spreadPercentage) {
condition.Randomize(random, changeSymbolProbability, spreadPercentage);
action.Randomize(random);
}
ICondition IClassifier.Condition {
get { return Condition; }
}
IAction IClassifier.Action {
get { return Action; }
}
public bool MatchInput(IInput target) {
return Condition.Match(target);
}
public bool MatchAction(IAction target) {
return Action.Match(target);
}
public bool IsMoreGeneral(IClassifier target) {
return IsMoreGeneral(target.Condition);
}
public bool IsMoreGeneral(ICondition target) {
var targetCast = target as VariableVectorCondition;
if (targetCast == null) { return false; }
if (Condition.VariableDictionary.Keys.Except(targetCast.VariableDictionary.Keys).Count() != 0
|| targetCast.VariableDictionary.Keys.Except(Condition.VariableDictionary.Keys).Count() != 0) { return false; }
double thisGenerality = Condition.VariableDictionary.Values.Sum(x => x.GetGenerality());
double targetGenerality = targetCast.VariableDictionary.Values.Sum(x => x.GetGenerality());
if (thisGenerality <= targetGenerality) { return false; }
foreach (var variables in targetCast.VariableDictionary) {
if (!Condition.VariableDictionary[variables.Key].IsGreaterThanOrEquallyGeneral(variables.Value)) {
return false;
}
}
return true;
}
public bool Identical(IClassifier target) {
var targetCast = target as VariableVector;
if (targetCast == null) { return false; }
if (!condition.Identical(targetCast.Condition)) { return false; }
if (!action.Identical(targetCast.Action)) { return false; }
return true;
}
}
}