#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 HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Optimization.Operators.LCS; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Encodings.DecisionList { [StorableClass] [Item("StringAction", "")] public class StringAction : Item, IAction { public static IEqualityComparer Comparer { get { return new GAssistNicheComparer(); } } IEqualityComparer IGAssistNiche.Comparer { get { return Comparer; } } [Storable] protected string variableName; public string VariableName { get { return variableName; } } [Storable] protected IList possibleFeatures; public IEnumerable PossibleFeatures { get { return possibleFeatures; } } [Storable] protected int currentActionIndex; public int CurrentActionIndex { get { return currentActionIndex; } } public int CurrentPosition { get { return CurrentActionIndex; } } public string CurrentAction { get { return possibleFeatures[currentActionIndex]; } } public int Possibilities { get { return possibleFeatures.Count; } } [StorableConstructor] protected StringAction(bool deserializing) : base(deserializing) { } protected StringAction(StringAction original, Cloner cloner) : base(original, cloner) { variableName = original.variableName; possibleFeatures = new List(original.possibleFeatures); currentActionIndex = original.currentActionIndex; } public StringAction(string variableName, IList variableValues) : base() { if (variableValues.Count() > 0 && variableValues.Count() != variableValues.Distinct().Count()) { throw new ArgumentException("variableValues have to be distinct and there has to be at least one value."); } this.possibleFeatures = variableValues; this.variableName = variableName; } public override IDeepCloneable Clone(Cloner cloner) { return new StringAction(this, cloner); } public IEnumerable GetPossibleActionPositions() { return Enumerable.Range(0, possibleFeatures.Count); } public IEnumerable GetPossibleActions() { return PossibleFeatures; } public void Randomize(IRandom random) { currentActionIndex = random.Next(0, possibleFeatures.Count()); } public void Randomize(IRandom random, IEnumerable except) { if (except.Count() == 0) { Randomize(random); return; } try { var exceptInt = except.Cast().Select(x => x.CurrentActionIndex); var newPossibleFeatures = Enumerable.Range(0, possibleFeatures.Count()).Except(exceptInt); currentActionIndex = newPossibleFeatures.ElementAt(random.Next(0, newPossibleFeatures.Count())); } catch (InvalidCastException) { throw new InvalidCastException("Actions have to be of type IntAction"); } } public bool Match(IAction action) { var targetCast = action as StringAction; return targetCast != null && this.variableName.Equals(targetCast.VariableName) && CurrentAction.Equals(targetCast.CurrentAction); } public void SetTo(string value) { SetToPosition(possibleFeatures.IndexOf(value)); } public void SetTo(IGAssistNiche action) { var castAction = action as StringAction; if (castAction == null) throw new ArgumentException("action has to be IntAction"); SetToPosition(castAction.CurrentPosition); } public void SetToPosition(int value) { if (value < 0 || value > possibleFeatures.Count) { throw new ArgumentOutOfRangeException(); } currentActionIndex = value; } public override string ToString() { return variableName + ": " + CurrentAction; } public bool SameNiche(IGAssistNiche niche) { return Match(niche as IAction); } public int GetNicheHashCode() { int result = 1; result = 37 * result + currentActionIndex; result = 37 * result + variableName.GetHashCode(); foreach (var feature in possibleFeatures) { result = 37 * result + feature.GetHashCode(); } return result; } } }