#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.Data;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Optimization.Operators.LCS.DefaultRule {
[Item("AutoDefaultRule", "Description missing")]
[StorableClass]
public class AutoDefaultRule : DefaultRuleOperator, IStochasticOperator {
#region Parameter Properties
public ILookupParameter RandomParameter {
get { return (ILookupParameter)Parameters["Random"]; }
}
public IValueLookupParameter>> AverageAccuraciesParameter {
get { return (IValueLookupParameter>>)Parameters["AverageAccuracies"]; }
}
public ILookupParameter> QualityParameter {
get { return (ILookupParameter>)Parameters["Quality"]; }
}
public IValueLookupParameter AccuraciesCountParameter {
get { return (IValueLookupParameter)Parameters["AccuraciesCount"]; }
}
public IValueLookupParameter NicheComparerParameter {
get { return (IValueLookupParameter)Parameters["NicheComparer"]; }
}
public IValueLookupParameter NicheStandardDeviationParameter {
get { return (IValueLookupParameter)Parameters["NicheStandardDeviation"]; }
}
#endregion
public ItemDictionary> AverageAccuracies { get { return AverageAccuraciesParameter.ActualValue; } }
public int AccuraciesCount { get { return AccuraciesCountParameter.ActualValue.Value; } }
[StorableConstructor]
protected AutoDefaultRule(bool deserializing) : base(deserializing) { }
protected AutoDefaultRule(AutoDefaultRule original, Cloner cloner)
: base(original, cloner) {
}
public AutoDefaultRule()
: base() {
Parameters.Add(new ValueLookupParameter>>("AverageAccuracies"));
Parameters.Add(new ScopeTreeLookupParameter("Quality"));
Parameters.Add(new LookupParameter("Random"));
Parameters.Add(new ValueLookupParameter("AccuraciesCount", new IntValue(15)));
Parameters.Add(new ValueLookupParameter("NicheComparer"));
Parameters.Add(new ValueLookupParameter("NicheStandardDeviation", new DoubleValue(0.005)));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new AutoDefaultRule(this, cloner);
}
public override IOperation Apply() {
if (NichingParameter.ActualValue == null) {
IList niches = GAssistNichesProblemDataParameter.ActualValue.GetPossibleNiches().ToList();
NichingParameter.ActualValue = new BoolValue(true);
NichesParameter.ActualValue = new IntValue(niches.Count);
AverageAccuraciesParameter.ActualValue = new ItemDictionary>(NicheComparerParameter.ActualValue as IEqualityComparer);
foreach (var niche in GAssistNichesProblemDataParameter.ActualValue.GetPossibleNiches()) {
AverageAccuracies.Add(niche, new ItemList(AccuraciesCount));
}
} else {
var nicheFitness = new Dictionary(NicheComparerParameter.ActualValue);
foreach (var niche in GAssistNichesProblemDataParameter.ActualValue.GetPossibleNiches()) {
nicheFitness[niche] = 0.0;
while (AverageAccuracies[niche].Count >= AccuraciesCount) {
AverageAccuracies[niche].RemoveAt(AverageAccuracies.Count - 1);
}
}
var qualityEnumerator = QualityParameter.ActualValue.GetEnumerator();
var individualEnumerator = IndividualParameter.ActualValue.GetEnumerator();
while (qualityEnumerator.MoveNext() && individualEnumerator.MoveNext()) {
if (nicheFitness[individualEnumerator.Current.Niche] < qualityEnumerator.Current.Value) {
nicheFitness[individualEnumerator.Current.Niche] = qualityEnumerator.Current.Value;
}
}
foreach (var niche in GAssistNichesProblemDataParameter.ActualValue.GetPossibleNiches()) {
AverageAccuracies[niche].Insert(0, new DoubleValue(nicheFitness[niche]));
}
var averages = new List(GAssistNichesProblemDataParameter.ActualValue.GetPossibleNiches().Count());
if (AverageAccuracies.Values.First().Count >= AccuraciesCount) {
foreach (var averageAccuracy in AverageAccuracies.Values) {
averages.Add(averageAccuracy.Select(x => x.Value).Average());
}
double avg = averages.Average();
double sum = averages.Sum(d => Math.Pow(d - avg, 2));
double std = Math.Sqrt(sum / averages.Count);
if (std < NicheStandardDeviationParameter.ActualValue.Value) {
NichingParameter.ActualValue.Value = false;
}
}
}
return base.Apply();
}
}
}