#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.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Encodings.ConditionActionEncoding {
[Item("ActionSelector", "")]
[StorableClass]
public class ActionSelector : SingleSuccessorOperator, IStochasticOperator {
public ILookupParameter RandomParameter {
get { return (ILookupParameter)Parameters["Random"]; }
}
public IValueLookupParameter SelectedActionParameter {
get { return (IValueLookupParameter)Parameters["SelectedAction"]; }
}
public ILookupParameter> PredictionArrayParameter {
get { return (ILookupParameter>)Parameters["PredictionArray"]; }
}
public ILookupParameter ExplorationProbabilityParameter {
get { return (ILookupParameter)Parameters["ExplorationProbability"]; }
}
[StorableConstructor]
protected ActionSelector(bool deserializing) : base(deserializing) { }
protected ActionSelector(ActionSelector original, Cloner cloner)
: base(original, cloner) {
}
public ActionSelector()
: base() {
Parameters.Add(new LookupParameter("Random"));
Parameters.Add(new ValueLookupParameter("SelectedAction", "Action, which has been selected."));
Parameters.Add(new LookupParameter>("PredictionArray"));
Parameters.Add(new LookupParameter("ExplorationProbability"));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new ActionSelector(this, cloner);
}
public override IOperation Apply() {
if (RandomParameter.ActualValue.NextDouble() > ExplorationProbabilityParameter.ActualValue.Value) {
double maxValue = PredictionArrayParameter.ActualValue.Max(x => x.Value.Value);
SelectedActionParameter.ActualValue = PredictionArrayParameter.ActualValue.Where(x => x.Value.Value.IsAlmost(maxValue))
.Select(x => x.Key)
.First();
} else {
int actionIndex = RandomParameter.ActualValue.Next(0, PredictionArrayParameter.ActualValue.Count);
SelectedActionParameter.ActualValue = PredictionArrayParameter.ActualValue.ElementAt(actionIndex).Key;
}
return base.Apply();
}
}
}