#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("IntAction", "")]
public class IntAction : 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 currentAction;
public int CurrentAction {
get { return currentAction; }
}
public int CurrentPosition {
get { return CurrentAction; }
}
public int Possibilities { get { return possibleFeatures.Count(); } }
[StorableConstructor]
protected IntAction(bool deserializing) : base(deserializing) { }
protected IntAction(IntAction original, Cloner cloner)
: base(original, cloner) {
variableName = original.variableName;
possibleFeatures = new List(original.possibleFeatures);
currentAction = original.currentAction;
}
public IntAction(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 IntAction(this, cloner);
}
public IEnumerable GetPossibleActionPositions() {
return possibleFeatures;
}
public IEnumerable GetPossibleActions() {
return PossibleFeatures;
}
public void Randomize(IRandom random) {
currentAction = possibleFeatures.ElementAt(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.currentAction);
var newPossibleFeatures = possibleFeatures.Except(exceptInt);
currentAction = 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 IntAction;
return targetCast != null && this.variableName.Equals(targetCast.VariableName) && CurrentAction.Equals(targetCast.CurrentAction);
}
public void SetTo(string value) {
SetToPosition(int.Parse(value));
}
public void SetTo(IGAssistNiche action) {
var castAction = action as IntAction;
if (castAction == null) throw new ArgumentException("action has to be IntAction");
SetToPosition(castAction.CurrentAction);
}
public void SetToPosition(int value) {
if (!possibleFeatures.Contains(value)) { throw new ArgumentOutOfRangeException(); }
currentAction = value;
}
public override string ToString() {
return variableName + ": " + currentAction.ToString();
}
public bool SameNiche(IGAssistNiche niche) {
return Match(niche as IAction);
}
public int GetNicheHashCode() {
int result = 1;
result = 37 * result + currentAction;
result = 37 * result + variableName.GetHashCode();
foreach (var feature in possibleFeatures) {
result = 37 * result + feature;
}
return result;
}
}
}