#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.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Algorithms.ScatterSearch {
///
/// An operator that updates the solution pool.
///
[Item("SolutionPoolUpdateMethod", "An operator that updates the solution pool.")]
[StorableClass]
public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator, IScatterSearchTargetProcessor {
#region Parameter properties
public ScopeParameter CurrentScopeParameter {
get { return (ScopeParameter)Parameters["CurrentScope"]; }
}
public IValueLookupParameter MaximizationParameter {
get { return (IValueLookupParameter)Parameters["Maximization"]; }
}
public IValueLookupParameter NewSolutionsParameter {
get { return (IValueLookupParameter)Parameters["NewSolutions"]; }
}
public IValueLookupParameter ReferenceSetSizeParameter {
get { return (IValueLookupParameter)Parameters["ReferenceSetSize"]; }
}
public IValueLookupParameter QualityParameter {
get { return (IValueLookupParameter)Parameters["Quality"]; }
}
public IValueLookupParameter TargetParameter {
get { return (IValueLookupParameter)Parameters["Target"]; }
}
#endregion
#region Properties
private IScope CurrentScope {
get { return CurrentScopeParameter.ActualValue; }
}
private BoolValue Maximization {
get { return MaximizationParameter.ActualValue; }
set { MaximizationParameter.ActualValue = value; }
}
private BoolValue NewSolutions {
get { return NewSolutionsParameter.ActualValue; }
}
private IntValue ReferenceSetSize {
get { return ReferenceSetSizeParameter.ActualValue; }
set { ReferenceSetSizeParameter.ActualValue = value; }
}
private IItem Quality {
get { return QualityParameter.ActualValue; }
}
private IItem Target {
get { return TargetParameter.ActualValue; }
}
#endregion
[StorableConstructor]
private SolutionPoolUpdateMethod(bool deserializing) : base(deserializing) { }
private SolutionPoolUpdateMethod(SolutionPoolUpdateMethod original, Cloner cloner) : base(original, cloner) { }
public SolutionPoolUpdateMethod() : base() { Initialize(); }
public override IDeepCloneable Clone(Cloner cloner) {
return new SolutionPoolUpdateMethod(this, cloner);
}
private void Initialize() {
#region Create parameters
Parameters.Add(new ScopeParameter("CurrentScope"));
Parameters.Add(new ValueLookupParameter("Maximization"));
Parameters.Add(new ValueLookupParameter("NewSolutions"));
Parameters.Add(new ValueLookupParameter("ReferenceSetSize"));
Parameters.Add(new ValueLookupParameter("Quality"));
Parameters.Add(new ValueLookupParameter("Target"));
#endregion
TargetParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem
}
public override IOperation Apply() {
var parentsScope = new Scope("Parents");
var offspringScope = new Scope("Offspring");
// split parents and offspring
foreach (var scope in CurrentScope.SubScopes) {
parentsScope.SubScopes.AddRange(scope.SubScopes.Take(scope.SubScopes.Count - 1));
offspringScope.SubScopes.AddRange(scope.SubScopes.Last().SubScopes);
}
CurrentScope.SubScopes.Clear();
var orderedParents = Maximization.Value ? parentsScope.SubScopes.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
parentsScope.SubScopes.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
var orderedOffspring = Maximization.Value ? offspringScope.SubScopes.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
offspringScope.SubScopes.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
CurrentScope.SubScopes.AddRange(orderedParents);
var worstParentQuality = (orderedParents.Last().Variables[QualityParameter.ActualName].Value as DoubleValue).Value;
var Constraint = Maximization.Value ? (Func)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value > worstParentQuality; }) :
(Func)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value < worstParentQuality; });
// is there any offspring better than the worst parent?
if (orderedOffspring.Any(Constraint)) {
// produce the set union
// attention: distinction might cause a too small reference set! (e.g. reference set = {1, 2, 2, 2, ..., 2} -> union = {1, 2}
var union = orderedParents.Union(orderedOffspring.Where(Constraint), new KeyEqualityComparer(x => x.Variables[TargetParameter.ActualName].Value.ToString()));
if (union.Count() > orderedParents./*Distinct(new KeyEqualityComparer(x => x.Variables[TargetParameter.ActualName].Value.ToString())).*/Count()) {
var orderedUnion = Maximization.Value ? union.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
union.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
CurrentScope.SubScopes.Replace(orderedUnion.Take(ReferenceSetSize.Value).ToList());
NewSolutions.Value = true;
}
}
return base.Apply();
}
private class KeyEqualityComparer : IEqualityComparer {
private readonly Func keyExtractor;
public KeyEqualityComparer(Func keyExtractor) {
this.keyExtractor = keyExtractor;
}
public bool Equals(T x, T y) {
return keyExtractor(x).Equals(keyExtractor(y));
}
public int GetHashCode(T obj) {
return keyExtractor(obj).GetHashCode();
}
}
}
}