#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 HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Encodings.ConditionActionEncoding {
[Item("PredictionArrayCalculator", "")]
[StorableClass]
public class PredictionArrayCalculator : SingleSuccessorOperator {
public ILookupParameter ClassifierComparerParameter {
get { return (ILookupParameter)Parameters["ClassifierComparer"]; }
}
public ILookupParameter> FitnessParameter {
get { return (ILookupParameter>)Parameters["Fitness"]; }
}
public ILookupParameter> PredictionParameter {
get { return (ILookupParameter>)Parameters["Prediction"]; }
}
public IValueParameter> PredictionArrayParameter {
get { return (IValueParameter>)Parameters["PredictionArray"]; }
}
public ILookupParameter> MatchParameter {
get { return (ILookupParameter>)Parameters["MatchParameter"]; }
}
[StorableConstructor]
protected PredictionArrayCalculator(bool deserializing) : base(deserializing) { }
protected PredictionArrayCalculator(PredictionArrayCalculator original, Cloner cloner)
: base(original, cloner) {
}
public PredictionArrayCalculator()
: base() {
Parameters.Add(new LookupParameter("ClassifierComparer"));
Parameters.Add(new ScopeTreeLookupParameter("Fitness"));
Parameters.Add(new ScopeTreeLookupParameter("Prediction"));
Parameters.Add(new ValueLookupParameter>("PredictionArray"));
Parameters.Add(new ScopeParameter("CurrentScope", "The current scope from which sub-scopes should be selected."));
Parameters.Add(new ScopeTreeLookupParameter("MatchParameter", "The matching encoding contained in each sub-scope which is used for selection."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new PredictionArrayCalculator(this, cloner);
}
public sealed override IOperation Apply() {
IItemDictionary predictionArray = new ItemDictionary(ClassifierComparerParameter.ActualValue);
IDictionary fitnessSumPerAction = new Dictionary(ClassifierComparerParameter.ActualValue);
ItemArray fitnesses = FitnessParameter.ActualValue;
ItemArray predictions = PredictionParameter.ActualValue;
for (int i = 0; i < MatchParameter.ActualValue.Length; i++) {
var action = MatchParameter.ActualValue[i].Action;
if (predictionArray.ContainsKey(action)) {
predictionArray[action].Value += predictions[i].Value * fitnesses[i].Value;
fitnessSumPerAction[action] += fitnesses[i].Value;
} else {
predictionArray[action] = new DoubleValue(predictions[i].Value * fitnesses[i].Value);
fitnessSumPerAction[action] = fitnesses[i].Value;
}
}
foreach (var action in predictionArray.Keys) {
if (fitnessSumPerAction[action] != 0) {
predictionArray[action].Value /= fitnessSumPerAction[action];
}
}
PredictionArrayParameter.ActualValue = predictionArray;
return base.Apply();
}
}
}