#region License Information
/* HeuristicLab
* Copyright (C) 2002-2014 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.Drawing;
using System.Linq;
using HeuristicLab.Analysis;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.BinaryVectorEncoding;
using HeuristicLab.Encodings.IntegerVectorEncoding;
using HeuristicLab.Encodings.PermutationEncoding;
using HeuristicLab.Encodings.RealVectorEncoding;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.PluginInfrastructure;
namespace HeuristicLab.Problems.Programmable {
[Item("Programmable Problem (single-objective)", "Represents a single-objective problem that can be programmed.")]
[Creatable("Problems")]
[StorableClass]
public class SingleObjectiveProgrammableProblem : SingleObjectiveHeuristicOptimizationProblem, IParameterizedNamedItem, IStorableContent {
public string Filename { get; set; }
public static new Image StaticItemImage {
get { return Common.Resources.VSImageLibrary.Script; }
}
public new ParameterCollection Parameters {
get { return base.Parameters; }
}
IKeyedItemCollection IParameterizedItem.Parameters {
get { return Parameters; }
}
public IValueParameter ScriptParameter {
get { return (IValueParameter)Parameters["Script"]; }
}
public IValueParameter ConfigurationParameter {
get { return (IValueParameter)Parameters["Configuration"]; }
}
[Storable]
protected List DynamicConfigurationParameters;
[StorableConstructor]
protected SingleObjectiveProgrammableProblem(bool deserializing) : base(deserializing) { }
protected SingleObjectiveProgrammableProblem(SingleObjectiveProgrammableProblem original, Cloner cloner)
: base(original, cloner) {
DynamicConfigurationParameters = original.DynamicConfigurationParameters.Select(cloner.Clone).ToList();
RegisterEventHandlers();
}
public SingleObjectiveProgrammableProblem()
: base(new SingleObjectiveEvaluator(), new ParameterVectorCreater()) {
Parameters.Add(new ValueParameter("Script", "Defines the problem.", new SingleObjectiveScript() { Name = Name }));
Parameters.Add(new ValueParameter("Configuration", "Describes which parameters exist, what they're called, what type they are and their bounds if any."));
DynamicConfigurationParameters = new List();
Operators.Add(new BestScopeSolutionAnalyzer());
Operators.Add(Evaluator);
Operators.Add(SolutionCreator);
RegisterEventHandlers();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new SingleObjectiveProgrammableProblem(this, cloner);
}
[StorableHook(HookType.AfterDeserialization)]
// ReSharper disable UnusedMember.Local
private void AfterDeserialization() {
RegisterEventHandlers();
}
// ReSharper restore UnusedMember.Local
private void RegisterEventHandlers() {
ScriptParameter.ValueChanged += ScriptParameterOnValueChanged;
RegisterScriptInstanceChanges();
}
private void ScriptParameterOnValueChanged(object sender, EventArgs eventArgs) {
RegisterScriptInstanceChanges();
}
private void RegisterScriptInstanceChanges() {
ScriptParameter.Value.InstanceChanged += ScriptOnInstanceChanged;
ScriptParameter.Value.NameChanged += ScriptOnNameChanged;
}
private void ScriptOnNameChanged(object sender, EventArgs eventArgs) {
if (sender != ScriptParameter.Value) return;
Name = ScriptParameter.Value.Name;
}
protected override void OnNameChanged() {
base.OnNameChanged();
ScriptParameter.Value.Name = Name;
}
protected virtual void ScriptOnInstanceChanged(object sender, EventArgs eventArgs) {
var instance = ScriptParameter.Value.Instance;
if (instance == null) return;
var configuration = instance.GetConfiguration();
ConfigurationParameter.Value = configuration;
Maximization.Value = instance.IsMaximizationProblem;
var solutionCreators = UpdateDynamicConfigurationParameters(configuration);
if (solutionCreators.Count == 1) {
UpdateSingleVectorEncodingOperators(solutionCreators);
} else {
UpdateMultiVectorEncodingOperators(solutionCreators, configuration);
}
}
protected virtual List UpdateDynamicConfigurationParameters(Configuration configuration) {
foreach (var param in DynamicConfigurationParameters)
if (Parameters.Contains(param)) Parameters.Remove(param);
DynamicConfigurationParameters.Clear();
var solutionCreators = new List();
foreach (var param in configuration.Parameters) {
/*
* Binary Vectors
*/
var binConfig = param.Value as BinaryParameterConfiguration;
if (binConfig != null) {
var p = new ValueParameter(param.Key + "Length", binConfig.Length);
DynamicConfigurationParameters.Add(p);
var creator = new RandomBinaryVectorCreator();
creator.BinaryVectorParameter.ActualName = param.Key;
creator.LengthParameter.ActualName = p.Name;
solutionCreators.Add(creator);
}
/*
* Integer Vectors
*/
var intConfig = param.Value as IntegerParameterConfiguration;
if (intConfig != null) {
var l = new ValueParameter(param.Key + "Length", intConfig.Length);
var b = new ValueParameter(param.Key + "Bounds", intConfig.Bounds);
DynamicConfigurationParameters.Add(l);
DynamicConfigurationParameters.Add(b);
var creator = new UniformRandomIntegerVectorCreator();
creator.IntegerVectorParameter.ActualName = param.Key;
creator.LengthParameter.ActualName = l.Name;
creator.BoundsParameter.ActualName = b.Name;
solutionCreators.Add(creator);
}
/*
* Real Vectors
*/
var realConfig = param.Value as RealParameterConfiguration;
if (realConfig != null) {
var l = new ValueParameter(param.Key + "Length", realConfig.Length);
var b = new ValueParameter(param.Key + "Bounds", realConfig.Bounds);
DynamicConfigurationParameters.Add(l);
DynamicConfigurationParameters.Add(b);
var creator = new UniformRandomRealVectorCreator();
creator.RealVectorParameter.ActualName = param.Key;
creator.LengthParameter.ActualName = l.Name;
creator.BoundsParameter.ActualName = b.Name;
solutionCreators.Add(creator);
}
/*
* Permutations
*/
var permConfig = param.Value as PermutationParameterConfiguration;
if (permConfig != null) {
var l = new ValueParameter(param.Key + "Length", permConfig.Length);
DynamicConfigurationParameters.Add(l);
var creator = new RandomPermutationCreator();
creator.PermutationParameter.ActualName = param.Key;
creator.LengthParameter.ActualName = l.Name;
creator.PermutationTypeParameter.Value = permConfig.Type;
solutionCreators.Add(creator);
}
}
foreach (var param in DynamicConfigurationParameters) {
param.Hidden = true;
Parameters.Add(param);
}
return solutionCreators;
}
protected virtual void UpdateSingleVectorEncodingOperators(List solutionCreators) {
var newCreator = solutionCreators.Single();
var binCreator = newCreator as IBinaryVectorCreator;
if (binCreator != null) {
var paramName = binCreator.BinaryVectorParameter.ActualName;
// do not replace a binary vector creator that was manually set
if (!(SolutionCreator is IBinaryVectorCreator) || ((IBinaryVectorCreator)SolutionCreator).BinaryVectorParameter.ActualName != paramName) {
Operators.Remove(SolutionCreator);
SolutionCreator = newCreator;
Operators.Add(SolutionCreator);
}
Operators.RemoveAll(x => x is ICrossover && !(x is IBinaryVectorCrossover) || x is IBinaryVectorCrossover && ((IBinaryVectorCrossover)x).ChildParameter.ActualName != paramName);
Operators.RemoveAll(x => x is IManipulator && !(x is IBinaryVectorManipulator) || x is IBinaryVectorManipulator && ((IBinaryVectorManipulator)x).BinaryVectorParameter.ActualName != paramName);
var crossovers = ApplicationManager.Manager.GetInstances().ToList();
var manipulators = ApplicationManager.Manager.GetInstances().ToList();
foreach (var xo in crossovers) {
xo.ChildParameter.ActualName = binCreator.BinaryVectorParameter.ActualName;
xo.ParentsParameter.ActualName = binCreator.BinaryVectorParameter.ActualName;
}
foreach (var m in manipulators)
m.BinaryVectorParameter.ActualName = binCreator.BinaryVectorParameter.ActualName;
Operators.AddRange(crossovers.Where(x => Operators.All(y => x.GetType() != y.GetType())));
Operators.AddRange(manipulators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
}
var intCreator = newCreator as IIntegerVectorCreator;
if (intCreator != null) {
var paramName = intCreator.IntegerVectorParameter.ActualName;
// do not replace an integer vector creator that was manually set
if (!(SolutionCreator is IIntegerVectorCreator)
|| ((IIntegerVectorCreator)SolutionCreator).IntegerVectorParameter.ActualName != intCreator.IntegerVectorParameter.ActualName) {
Operators.Remove(SolutionCreator);
SolutionCreator = newCreator;
Operators.Add(SolutionCreator);
}
Operators.RemoveAll(x => x is ICrossover && !(x is IIntegerVectorCrossover) || x is IIntegerVectorCrossover && ((IIntegerVectorCrossover)x).ChildParameter.ActualName != paramName);
Operators.RemoveAll(x => x is IManipulator && !(x is IIntegerVectorManipulator) || x is IIntegerVectorManipulator && ((IIntegerVectorManipulator)x).IntegerVectorParameter.ActualName != paramName);
var crossovers = ApplicationManager.Manager.GetInstances().ToList();
var manipulators = ApplicationManager.Manager.GetInstances().ToList();
foreach (var xo in crossovers) {
xo.ChildParameter.ActualName = intCreator.IntegerVectorParameter.ActualName;
xo.ParentsParameter.ActualName = intCreator.IntegerVectorParameter.ActualName;
}
foreach (var m in manipulators)
m.IntegerVectorParameter.ActualName = intCreator.IntegerVectorParameter.ActualName;
foreach (var bo in crossovers.OfType()
.Concat(manipulators.OfType())
.ToList()) {
bo.BoundsParameter.ActualName = intCreator.BoundsParameter.ActualName;
}
Operators.AddRange(crossovers.Where(x => Operators.All(y => x.GetType() != y.GetType())));
Operators.AddRange(manipulators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
}
var realCreator = newCreator as IRealVectorCreator;
if (realCreator != null) {
var paramName = realCreator.RealVectorParameter.ActualName;
// do not replace a real vector creator that was manually set
if (!(SolutionCreator is IRealVectorCreator)
|| ((IRealVectorCreator)SolutionCreator).RealVectorParameter.ActualName != realCreator.RealVectorParameter.ActualName) {
Operators.Remove(SolutionCreator);
SolutionCreator = newCreator;
Operators.Add(SolutionCreator);
}
Operators.RemoveAll(x => x is ICrossover && !(x is IRealVectorCrossover) || x is IRealVectorCrossover && ((IRealVectorCrossover)x).ChildParameter.ActualName != paramName);
Operators.RemoveAll(x => x is IManipulator && !(x is IRealVectorManipulator) || x is IRealVectorManipulator && ((IRealVectorManipulator)x).RealVectorParameter.ActualName != paramName);
var crossovers = ApplicationManager.Manager.GetInstances().ToList();
var manipulators = ApplicationManager.Manager.GetInstances().ToList();
foreach (var xo in crossovers) {
xo.ChildParameter.ActualName = realCreator.RealVectorParameter.ActualName;
xo.ParentsParameter.ActualName = realCreator.RealVectorParameter.ActualName;
xo.BoundsParameter.ActualName = realCreator.BoundsParameter.ActualName;
}
foreach (var m in manipulators) {
m.RealVectorParameter.ActualName = realCreator.RealVectorParameter.ActualName;
m.BoundsParameter.ActualName = realCreator.BoundsParameter.ActualName;
}
Operators.AddRange(crossovers.Where(x => Operators.All(y => x.GetType() != y.GetType())));
Operators.AddRange(manipulators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
}
var permCreator = newCreator as IPermutationCreator;
if (permCreator != null) {
var paramName = permCreator.PermutationParameter.ActualName;
// do not replace a permutation creator that was manually set
if (!(SolutionCreator is IPermutationCreator)
|| ((IPermutationCreator)SolutionCreator).PermutationParameter.ActualName != permCreator.PermutationParameter.ActualName) {
Operators.Remove(SolutionCreator);
SolutionCreator = newCreator;
Operators.Add(SolutionCreator);
}
Operators.RemoveAll(x => x is ICrossover && !(x is IPermutationCrossover) || x is IPermutationCrossover && ((IPermutationCrossover)x).ChildParameter.ActualName != paramName);
Operators.RemoveAll(x => x is IManipulator && !(x is IPermutationManipulator) || x is IPermutationManipulator && ((IPermutationManipulator)x).PermutationParameter.ActualName != paramName);
var crossovers = ApplicationManager.Manager.GetInstances().ToList();
var manipulators = ApplicationManager.Manager.GetInstances().ToList();
foreach (var xo in crossovers) {
xo.ChildParameter.ActualName = permCreator.PermutationParameter.ActualName;
xo.ParentsParameter.ActualName = permCreator.PermutationParameter.ActualName;
}
foreach (var m in manipulators)
m.PermutationParameter.ActualName = permCreator.PermutationParameter.ActualName;
Operators.AddRange(crossovers.Where(x => Operators.All(y => x.GetType() != y.GetType())));
Operators.AddRange(manipulators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
}
}
protected virtual void UpdateMultiVectorEncodingOperators(List solutionCreators, Configuration configuration) {
var oldCreator = SolutionCreator as ParameterVectorCreater;
var newCreator = new ParameterVectorCreater();
/*
* Binary Vectors
*/
var newBinParams = new HashSet(solutionCreators.OfType().Select(x => x.BinaryVectorParameter.ActualName));
if (oldCreator != null) {
// we want to reuse the old creator
var oldParams = new HashSet(oldCreator.Operators.OfType()
.Select(x => x.BinaryVectorParameter.ActualName));
foreach (var toAdd in newBinParams.Except(oldParams)) {
var paramName = toAdd;
oldCreator.Operators.Add(
solutionCreators.OfType().Single(x => x.BinaryVectorParameter.ActualName == paramName));
}
foreach (var toRemove in oldParams.Except(newBinParams)) {
var paramName = toRemove;
var op =
oldCreator.Operators.OfType()
.SingleOrDefault(x => x.BinaryVectorParameter.ActualName == paramName);
if (op != null) oldCreator.Operators.Remove(op);
}
} else {
// we will use the new creator
foreach (var binCreator in solutionCreators.OfType()) {
newCreator.Operators.Add(binCreator);
}
}
/*
* Integer Vectors
*/
var newIntParams = new HashSet(solutionCreators.OfType().Select(x => x.IntegerVectorParameter.ActualName));
if (oldCreator != null) {
// we want to reuse the old creator
var oldParams = new HashSet(oldCreator
.Operators.OfType()
.Select(x => x.IntegerVectorParameter.ActualName));
foreach (var toAdd in newIntParams.Except(oldParams)) {
var paramName = toAdd;
oldCreator.Operators.Add(
solutionCreators.OfType().Single(x => x.IntegerVectorParameter.ActualName == paramName));
}
foreach (var toRemove in oldParams.Except(newIntParams)) {
var paramName = toRemove;
var op =
oldCreator.Operators.OfType()
.SingleOrDefault(x => x.IntegerVectorParameter.ActualName == paramName);
if (op != null) oldCreator.Operators.Remove(op);
}
} else {
// we will use the new creator
foreach (var intCreator in solutionCreators.OfType()) {
newCreator.Operators.Add(intCreator);
}
}
/*
* Real Vectors
*/
var newRealParams = new HashSet(solutionCreators.OfType().Select(x => x.RealVectorParameter.ActualName));
if (oldCreator != null) {
// we want to reuse the old creator
var oldParams = new HashSet(oldCreator
.Operators.OfType()
.Select(x => x.RealVectorParameter.ActualName));
foreach (var toAdd in newRealParams.Except(oldParams)) {
var paramName = toAdd;
oldCreator.Operators.Add(
solutionCreators.OfType().Single(x => x.RealVectorParameter.ActualName == paramName));
}
foreach (var toRemove in oldParams.Except(newRealParams)) {
var paramName = toRemove;
var op =
oldCreator.Operators.OfType()
.SingleOrDefault(x => x.RealVectorParameter.ActualName == paramName);
if (op != null) oldCreator.Operators.Remove(op);
}
} else {
// we will use the new creator
foreach (var realCreator in solutionCreators.OfType()) {
newCreator.Operators.Add(realCreator);
}
}
/*
* Permutations
*/
var newPermParams = new HashSet(solutionCreators.OfType().Select(x => x.PermutationParameter.ActualName));
if (oldCreator != null) {
// we want to reuse the old creator
var oldParams = new HashSet(oldCreator
.Operators.OfType()
.Select(x => x.PermutationParameter.ActualName));
foreach (var toAdd in newPermParams.Except(oldParams)) {
var paramName = toAdd;
oldCreator.Operators.Add(
solutionCreators.OfType().Single(x => x.PermutationParameter.ActualName == paramName));
}
foreach (var toRemove in oldParams.Except(newPermParams)) {
var paramName = toRemove;
var op =
oldCreator.Operators.OfType()
.SingleOrDefault(x => x.PermutationParameter.ActualName == paramName);
if (op != null) oldCreator.Operators.Remove(op);
}
// we also have to sync the permutation type (in case this changes, as it is a value parameter)
foreach (var intersect in newPermParams.Intersect(oldParams)) {
var paramName = intersect;
var oldPermCreator = oldCreator.Operators.OfType()
.Single(x => x.PermutationParameter.ActualName == paramName);
var newPermCreator = solutionCreators.OfType()
.Single(x => x.PermutationParameter.ActualName == paramName);
oldPermCreator.PermutationTypeParameter.Value = newPermCreator.PermutationTypeParameter.Value;
}
} else {
// we will use the new creator
foreach (var permCreator in solutionCreators.OfType()) {
newCreator.Operators.Add(permCreator);
}
}
if (oldCreator == null) SolutionCreator = newCreator;
// crossover and manipulator for multi-vector encoding
// the condition checks if a multi-vector encoding is to be updated (there already exists ParameterVectorCrossover and ParameterVectorManipulator)
if (Operators.OfType().Any() && Operators.OfType().Any()) {
// Update the crossovers
foreach (var oldXo in Operators.OfType()) {
/*
* Binary Vectors
*/
var oldBinParams = new HashSet(oldXo.Operators.OfType()
.Select(x => x.ChildParameter.ActualName));
foreach (var toAdd in newBinParams.Except(oldBinParams))
oldXo.Operators.Add(GetDefaultBinaryCrossover(toAdd, configuration));
foreach (var toRemove in oldBinParams.Except(newBinParams)) {
var op =
oldXo.Operators.OfType().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove);
if (op != null) oldXo.Operators.Remove(op);
}
/*
* Integer Vectors
*/
var oldIntParams = new HashSet(oldXo.Operators.OfType()
.Select(x => x.ChildParameter.ActualName));
foreach (var toAdd in newIntParams.Except(oldIntParams))
oldXo.Operators.Add(GetDefaultIntegerCrossover(toAdd, configuration));
foreach (var toRemove in oldIntParams.Except(newIntParams)) {
var op =
oldXo.Operators.OfType()
.SingleOrDefault(x => x.ChildParameter.ActualName == toRemove);
if (op != null) oldXo.Operators.Remove(op);
}
/*
* Real Vectors
*/
var oldRealParams = new HashSet(oldXo.Operators.OfType()
.Select(x => x.ChildParameter.ActualName));
foreach (var toAdd in newRealParams.Except(oldRealParams))
oldXo.Operators.Add(GetDefaultRealCrossover(toAdd, configuration));
foreach (var toRemove in oldRealParams.Except(newRealParams)) {
var op =
oldXo.Operators.OfType().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove);
if (op != null) oldXo.Operators.Remove(op);
}
/*
* Permutations
*/
var oldPermParams = new HashSet(oldXo.Operators.OfType()
.Select(x => x.ChildParameter.ActualName));
foreach (var toAdd in newPermParams.Except(oldPermParams))
oldXo.Operators.Add(GetDefaultPermutationCrossover(toAdd, configuration));
foreach (var toRemove in oldPermParams.Except(newPermParams)) {
var op =
oldXo.Operators.OfType().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove);
if (op != null) oldXo.Operators.Remove(op);
}
}
// Update the manipulators
foreach (var oldM in Operators.OfType()) {
/*
* Binary Vectors
*/
var oldBinParams = new HashSet(oldM.Operators.OfType()
.Select(x => x.BinaryVectorParameter.ActualName));
foreach (var toAdd in newBinParams.Except(oldBinParams))
oldM.Operators.Add(GetDefaultBinaryManipulator(toAdd, configuration));
foreach (var toRemove in oldBinParams.Except(newBinParams)) {
var op =
oldM.Operators.OfType()
.SingleOrDefault(x => x.BinaryVectorParameter.ActualName == toRemove);
if (op != null) oldM.Operators.Remove(op);
}
/*
* Integer Vectors
*/
var oldIntParams = new HashSet(oldM.Operators.OfType()
.Select(x => x.IntegerVectorParameter.ActualName));
foreach (var toAdd in newIntParams.Except(oldIntParams))
oldM.Operators.Add(GetDefaultIntegerManipulator(toAdd, configuration));
foreach (var toRemove in oldIntParams.Except(newIntParams)) {
var op =
oldM.Operators.OfType()
.SingleOrDefault(x => x.IntegerVectorParameter.ActualName == toRemove);
if (op != null) oldM.Operators.Remove(op);
}
/*
* Real Vectors
*/
var oldRealParams = new HashSet(oldM.Operators.OfType()
.Select(x => x.RealVectorParameter.ActualName));
foreach (var toAdd in newRealParams.Except(oldRealParams))
oldM.Operators.Add(GetDefaultRealManipulator(toAdd, configuration));
foreach (var toRemove in oldRealParams.Except(newRealParams)) {
var op =
oldM.Operators.OfType()
.SingleOrDefault(x => x.RealVectorParameter.ActualName == toRemove);
if (op != null) oldM.Operators.Remove(op);
}
/*
* Permutations
*/
var oldPermParams = new HashSet(oldM.Operators.OfType()
.Select(x => x.PermutationParameter.ActualName));
foreach (var toAdd in newPermParams.Except(oldPermParams))
oldM.Operators.Add(GetDefaultPermutationManipulator(toAdd, configuration));
foreach (var toRemove in oldPermParams.Except(newPermParams)) {
var op =
oldM.Operators.OfType()
.SingleOrDefault(x => x.PermutationParameter.ActualName == toRemove);
if (op != null) oldM.Operators.Remove(op);
}
}
} else {
// change from single-vector encoding to multi-vector encoding
Operators.RemoveAll(x => x is ICrossover);
Operators.RemoveAll(x => x is IManipulator);
var crossover = new ParameterVectorCrossover();
var manipulator = new ParameterVectorManipulator();
foreach (var param in configuration.Parameters) {
if (param.Value is BinaryParameterConfiguration) {
crossover.Operators.Add(GetDefaultBinaryCrossover(param.Key, configuration));
manipulator.Operators.Add(GetDefaultBinaryManipulator(param.Key, configuration));
continue;
}
var intConfig = param.Value as IntegerParameterConfiguration;
if (intConfig != null) {
crossover.Operators.Add(GetDefaultIntegerCrossover(param.Key, configuration));
manipulator.Operators.Add(GetDefaultIntegerManipulator(param.Key, configuration));
continue;
}
var realConfig = param.Value as RealParameterConfiguration;
if (realConfig != null) {
crossover.Operators.Add(GetDefaultRealCrossover(param.Key, configuration));
manipulator.Operators.Add(GetDefaultRealManipulator(param.Key, configuration));
continue;
}
var permConfig = param.Value as PermutationParameterConfiguration;
if (permConfig != null) {
crossover.Operators.Add(GetDefaultPermutationCrossover(param.Key, configuration));
manipulator.Operators.Add(GetDefaultPermutationManipulator(param.Key, configuration));
continue;
}
throw new InvalidOperationException("Unknown type for parameter " + param.Key);
}
Operators.Add(crossover);
Operators.Add(manipulator);
}
}
// ReSharper disable RedundantNameQualifier
protected virtual IBinaryVectorCrossover GetDefaultBinaryCrossover(string paramName, Configuration config) {
var binConfig = (BinaryParameterConfiguration)config.Parameters[paramName];
IBinaryVectorCrossover binXo;
if (binConfig.Length.Value > 3) binXo = new Encodings.BinaryVectorEncoding.SinglePointCrossover();
else binXo = new Encodings.BinaryVectorEncoding.UniformCrossover();
binXo.ChildParameter.ActualName = paramName;
binXo.ParentsParameter.ActualName = paramName;
return binXo;
}
protected virtual IBinaryVectorManipulator GetDefaultBinaryManipulator(string paramName, Configuration config) {
var binM = new Encodings.BinaryVectorEncoding.SomePositionsBitflipManipulator();
binM.BinaryVectorParameter.ActualName = paramName;
binM.MutationProbabilityParameter.Value = new DoubleValue(0.1);
return binM;
}
protected virtual IIntegerVectorCrossover GetDefaultIntegerCrossover(string paramName, Configuration config) {
var intXo = new Encodings.IntegerVectorEncoding.RoundedBlendAlphaBetaCrossover();
intXo.ChildParameter.ActualName = paramName;
intXo.ParentsParameter.ActualName = paramName;
intXo.BoundsParameter.ActualName = paramName + "Bounds";
intXo.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
intXo.MaximizationParameter.ActualName = MaximizationParameter.Name;
return intXo;
}
protected virtual IIntegerVectorManipulator GetDefaultIntegerManipulator(string paramName, Configuration configuration) {
var intM = new Encodings.IntegerVectorEncoding.UniformSomePositionsManipulator();
intM.IntegerVectorParameter.ActualName = paramName;
intM.BoundsParameter.ActualName = paramName + "Bounds";
intM.ProbabilityParameter.Value = new DoubleValue(0.1);
return intM;
}
protected virtual IRealVectorCrossover GetDefaultRealCrossover(string paramName, Configuration configuration) {
var realXo = new Encodings.RealVectorEncoding.BlendAlphaBetaCrossover();
realXo.ChildParameter.ActualName = paramName;
realXo.ParentsParameter.ActualName = paramName;
realXo.BoundsParameter.ActualName = paramName + "Bounds";
realXo.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
realXo.MaximizationParameter.ActualName = MaximizationParameter.Name;
return realXo;
}
protected virtual IRealVectorManipulator GetDefaultRealManipulator(string paramName, Configuration configuration) {
var realM = new Encodings.RealVectorEncoding.BreederGeneticAlgorithmManipulator();
realM.RealVectorParameter.ActualName = paramName;
realM.BoundsParameter.ActualName = paramName + "Bounds";
return realM;
}
protected virtual IPermutationCrossover GetDefaultPermutationCrossover(string paramName, Configuration configuration) {
var permXo = new Encodings.PermutationEncoding.PartiallyMatchedCrossover();
permXo.ChildParameter.ActualName = paramName;
permXo.ParentsParameter.ActualName = paramName;
return permXo;
}
protected virtual IPermutationManipulator GetDefaultPermutationManipulator(string paramName, Configuration configuration) {
var permM = new Encodings.PermutationEncoding.Swap2Manipulator();
permM.PermutationParameter.ActualName = paramName;
return permM;
}
// ReSharper restore RedundantNameQualifier
}
}