- Timestamp:
- 04/03/19 15:37:38 (6 years ago)
- Location:
- branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems
- Files:
-
- 26 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/CombinedSolution.cs
r16724 r16751 22 22 using System; 23 23 using System.Linq; 24 using HEAL.Attic; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; 26 using HEAL.Attic;27 27 28 28 namespace HeuristicLab.Optimization { 29 29 [Item("CombinedSolution", "A solution that consists of other solutions.")] 30 30 [StorableType("B5ED00CB-E533-4ED6-AB2D-95BF7A654AAD")] 31 public sealed class CombinedSolution : Item, I Solution {31 public sealed class CombinedSolution : Item, IEncodedSolution { 32 32 33 33 private CombinedEncoding Encoding { get; set; } … … 50 50 } 51 51 52 public I Solution this[string name] {53 get { return ScopeUtil.Get Solution(Scope, name); }54 set { ScopeUtil.Copy SolutionToScope(Scope, name, value); }52 public IEncodedSolution this[string name] { 53 get { return ScopeUtil.GetEncodedSolution(Scope, name); } 54 set { ScopeUtil.CopyEncodedSolutionToScope(Scope, name, value); } 55 55 } 56 56 … … 66 66 } 67 67 68 public T Solution GetSolution<TSolution>(string name) where TSolution : class, ISolution {69 return (T Solution)ScopeUtil.GetSolution(Scope, name);68 public TEncodedSolution GetEncodedSolution<TEncodedSolution>(string name) where TEncodedSolution : class, IEncodedSolution { 69 return (TEncodedSolution)ScopeUtil.GetEncodedSolution(Scope, name); 70 70 } 71 71 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Encoding.cs
r16723 r16751 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HEAL.Attic; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 28 using HeuristicLab.Parameters; 28 using HEAL.Attic;29 29 30 30 namespace HeuristicLab.Optimization { 31 31 [Item("Encoding", "Base class for describing different encodings.")] 32 32 [StorableType("395B1372-FA54-4649-9EBE-5402A0AA9494")] 33 public abstract class Encoding<T Solution> : ParameterizedNamedItem, IEncoding<TSolution>34 where T Solution : class,ISolution {35 public override sealedbool CanChangeName {33 public abstract class Encoding<TEncodedSolution> : ParameterizedNamedItem, IEncoding<TEncodedSolution> 34 where TEncodedSolution : class, IEncodedSolution { 35 public sealed override bool CanChangeName { 36 36 get { return false; } 37 37 } … … 48 48 get { return encodingOperators; } 49 49 set { 50 if (!value.OfType<ISolutionCreator<T Solution>>().Any())50 if (!value.OfType<ISolutionCreator<TEncodedSolution>>().Any()) 51 51 throw new ArgumentException("The provided operators contain no suitable solution creator"); 52 52 encodingOperators.Clear(); 53 53 foreach (var op in value) encodingOperators.Add(op); 54 54 55 ISolutionCreator<T Solution> newSolutionCreator = (ISolutionCreator<TSolution>)encodingOperators.FirstOrDefault(o => o.GetType() == SolutionCreator.GetType()) ??56 encodingOperators.OfType<ISolutionCreator<T Solution>>().First();55 ISolutionCreator<TEncodedSolution> newSolutionCreator = (ISolutionCreator<TEncodedSolution>)encodingOperators.FirstOrDefault(o => o.GetType() == SolutionCreator.GetType()) ?? 56 encodingOperators.OfType<ISolutionCreator<TEncodedSolution>>().First(); 57 57 SolutionCreator = newSolutionCreator; 58 58 OnOperatorsChanged(); … … 67 67 get { return SolutionCreator; } 68 68 } 69 public ISolutionCreator<T Solution> SolutionCreator {70 get { return (ISolutionCreator<T Solution>)SolutionCreatorParameter.Value; }69 public ISolutionCreator<TEncodedSolution> SolutionCreator { 70 get { return (ISolutionCreator<TEncodedSolution>)SolutionCreatorParameter.Value; } 71 71 set { 72 72 if (value == null) throw new ArgumentNullException("SolutionCreator must not be null."); … … 86 86 } 87 87 88 protected Encoding(Encoding<T Solution> original, Cloner cloner)88 protected Encoding(Encoding<TEncodedSolution> original, Cloner cloner) 89 89 : base(original, cloner) { 90 90 encodingOperators = cloner.Clone(original.encodingOperators); … … 95 95 protected Encoding(string name) 96 96 : base(name) { 97 Parameters.Add(new ValueParameter<ISolutionCreator<T Solution>>(name + ".SolutionCreator", "The operator to create a solution."));97 Parameters.Add(new ValueParameter<ISolutionCreator<TEncodedSolution>>(name + ".SolutionCreator", "The operator to create a solution.")); 98 98 Parameters.Add(new FixedValueParameter<ReadOnlyItemSet<IOperator>>(name + ".Operators", "The operators that the encoding specifies.", encodingOperators.AsReadOnly())); 99 99 -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IEncodedSolution.cs
r16750 r16751 22 22 #endregion 23 23 24 using System;25 using System.Collections.Generic;26 using System.Linq;27 using System.Text;28 using System.Threading.Tasks;29 24 using HeuristicLab.Core; 30 25 31 26 namespace HeuristicLab.Optimization { 32 public interface I Solution : IItem { }27 public interface IEncodedSolution : IItem { } 33 28 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IEncoding.cs
r16723 r16751 22 22 using System; 23 23 using System.Collections.Generic; 24 using HEAL.Attic; 24 25 using HeuristicLab.Core; 25 using HEAL.Attic;26 26 27 27 namespace HeuristicLab.Optimization { … … 40 40 } 41 41 42 public interface IEncoding<T Solution> : IEncoding43 where T Solution : class, ISolution {44 //new ISolutionCreator<T Solution> SolutionCreator { get; }42 public interface IEncoding<TEncodedSolution> : IEncoding 43 where TEncodedSolution : class, IEncodedSolution { 44 //new ISolutionCreator<TEncodedSolution> SolutionCreator { get; } 45 45 } 46 46 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IEncodingOperator.cs
r16723 r16751 20 20 #endregion 21 21 22 using HEAL.Attic; 22 23 using HeuristicLab.Core; 23 using HEAL.Attic;24 24 25 25 namespace HeuristicLab.Optimization { 26 26 [StorableType("20faaf8b-dd4f-4f0e-a772-4c4dec7fcccb")] 27 public interface IEncodingOperator<T Solution> : IOperator where TSolution : class, ISolution {28 ILookupParameter<IEncoding<T Solution>> EncodingParameter { get; }27 public interface IEncodingOperator<TEncodedSolution> : IOperator where TEncodedSolution : class, IEncodedSolution { 28 ILookupParameter<IEncoding<TEncodedSolution>> EncodingParameter { get; } 29 29 } 30 30 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IMultiObjectiveProblem.cs
r13362 r16751 23 23 24 24 namespace HeuristicLab.Optimization { 25 public interface IMultiObjectiveProblem<TEncoding, T Solution> : IProblem<TEncoding, TSolution>, IMultiObjectiveHeuristicOptimizationProblem26 where TEncoding : class, IEncoding<T Solution>27 where T Solution : class, ISolution {25 public interface IMultiObjectiveProblem<TEncoding, TEncodedSolution> : IProblem<TEncoding, TEncodedSolution>, IMultiObjectiveHeuristicOptimizationProblem 26 where TEncoding : class, IEncoding<TEncodedSolution> 27 where TEncodedSolution : class, IEncodedSolution { 28 28 29 29 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IMultiObjectiveProblemDefinition.cs
r16723 r16751 20 20 #endregion 21 21 22 using HEAL.Attic; 22 23 using HeuristicLab.Core; 23 using HEAL.Attic;24 24 25 25 namespace HeuristicLab.Optimization { 26 26 [StorableType("39eacdb5-80a0-425d-902a-00eb3e1d6610")] 27 public interface IMultiObjectiveProblemDefinition<TEncoding, T Solution> : IProblemDefinition<TEncoding, TSolution>28 where TEncoding : class, IEncoding<T Solution>29 where T Solution : class, ISolution {27 public interface IMultiObjectiveProblemDefinition<TEncoding, TEncodedSolution> : IProblemDefinition<TEncoding, TEncodedSolution> 28 where TEncoding : class, IEncoding<TEncodedSolution> 29 where TEncodedSolution : class, IEncodedSolution { 30 30 bool[] Maximization { get; } 31 double[] Evaluate(T Solution individual, IRandom random);32 void Analyze(T Solution[] individuals, double[][] qualities, ResultCollection results, IRandom random);31 double[] Evaluate(TEncodedSolution individual, IRandom random); 32 void Analyze(TEncodedSolution[] individuals, double[][] qualities, ResultCollection results, IRandom random); 33 33 } 34 34 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IProblemDefinition.cs
r16723 r16751 24 24 namespace HeuristicLab.Optimization { 25 25 [StorableType("747a3cea-b9ba-4322-a5c2-050cd7e16e2a")] 26 public interface IProblemDefinition<TEncoding, T Solution>27 where TEncoding : class, IEncoding<T Solution>28 where T Solution : class, ISolution {26 public interface IProblemDefinition<TEncoding, TEncodedSolution> 27 where TEncoding : class, IEncoding<TEncodedSolution> 28 where TEncodedSolution : class, IEncodedSolution { 29 29 TEncoding Encoding { get; } 30 30 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/ISingleObjectiveProblem.cs
r13361 r16751 24 24 25 25 namespace HeuristicLab.Optimization { 26 public interface ISingleObjectiveProblem<TEncoding, T Solution> : IProblem<TEncoding, TSolution>, ISingleObjectiveHeuristicOptimizationProblem27 where TEncoding : class, IEncoding<T Solution>28 where T Solution : class, ISolution {26 public interface ISingleObjectiveProblem<TEncoding, TEncodedSolution> : IProblem<TEncoding, TEncodedSolution>, ISingleObjectiveHeuristicOptimizationProblem 27 where TEncoding : class, IEncoding<TEncodedSolution> 28 where TEncodedSolution : class, IEncodedSolution { 29 29 30 30 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/ISingleObjectiveProblemDefinition.cs
r16723 r16751 21 21 22 22 using System.Collections.Generic; 23 using HEAL.Attic; 23 24 using HeuristicLab.Core; 24 using HEAL.Attic;25 25 26 26 namespace HeuristicLab.Optimization { 27 27 [StorableType("7ec7bf7e-aaa7-4681-828b-3401cf67e2b3")] 28 public interface ISingleObjectiveProblemDefinition<TEncoding, T Solution> : IProblemDefinition<TEncoding, TSolution>29 where TEncoding : class, IEncoding<T Solution>30 where T Solution : class, ISolution {28 public interface ISingleObjectiveProblemDefinition<TEncoding, TEncodedSolution> : IProblemDefinition<TEncoding, TEncodedSolution> 29 where TEncoding : class, IEncoding<TEncodedSolution> 30 where TEncodedSolution : class, IEncodedSolution { 31 31 bool Maximization { get; } 32 double Evaluate(T Solution solution, IRandom random);33 void Analyze(T Solution[] solutions, double[] qualities, ResultCollection results, IRandom random);34 IEnumerable<T Solution> GetNeighbors(TSolution solution, IRandom random);32 double Evaluate(TEncodedSolution solution, IRandom random); 33 void Analyze(TEncodedSolution[] solutions, double[] qualities, ResultCollection results, IRandom random); 34 IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution solution, IRandom random); 35 35 bool IsBetter(double quality, double bestQuality); 36 36 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/IMultiObjectiveAnalysisOperator.cs
r16723 r16751 21 21 22 22 using System; 23 using HEAL.Attic; 23 24 using HeuristicLab.Core; 24 using HEAL.Attic;25 25 26 26 namespace HeuristicLab.Optimization { 27 27 [StorableType("c9325602-3262-48a4-8985-03657fb0b34f")] 28 internal interface IMultiObjectiveAnalysisOperator<T Solution> : IEncodingOperator<TSolution>, IAnalyzer, IMultiObjectiveOperator29 where T Solution : class, ISolution {30 Action<T Solution[], double[][], ResultCollection, IRandom> AnalyzeAction { get; set; }28 internal interface IMultiObjectiveAnalysisOperator<TEncodedSolution> : IEncodingOperator<TEncodedSolution>, IAnalyzer, IMultiObjectiveOperator 29 where TEncodedSolution : class, IEncodedSolution { 30 Action<TEncodedSolution[], double[][], ResultCollection, IRandom> AnalyzeAction { get; set; } 31 31 } 32 32 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/IMultiObjectiveEvaluationOperator.cs
r16723 r16751 21 21 22 22 using System; 23 using HEAL.Attic; 23 24 using HeuristicLab.Core; 24 using HEAL.Attic;25 25 26 26 namespace HeuristicLab.Optimization { 27 27 [StorableType("89da568c-70a2-48fb-8e6b-ea078bb6fc3f")] 28 internal interface IMultiObjectiveEvaluationOperator<T Solution> : IMultiObjectiveEvaluator, IEncodingOperator<TSolution>29 where T Solution : class, ISolution {30 Func<T Solution, IRandom, double[]> EvaluateFunc { get; set; }28 internal interface IMultiObjectiveEvaluationOperator<TEncodedSolution> : IMultiObjectiveEvaluator, IEncodingOperator<TEncodedSolution> 29 where TEncodedSolution : class, IEncodedSolution { 30 Func<TEncodedSolution, IRandom, double[]> EvaluateFunc { get; set; } 31 31 } 32 32 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/INeighborBasedOperator.cs
r16723 r16751 22 22 using System; 23 23 using System.Collections.Generic; 24 using HEAL.Attic; 24 25 using HeuristicLab.Core; 25 using HEAL.Attic;26 26 27 27 namespace HeuristicLab.Optimization { 28 28 [StorableType("fda56e0b-9392-4711-9af1-55211bfa24ac")] 29 internal interface INeighborBasedOperator<T Solution> : IEncodingOperator<TSolution>30 where T Solution : class, ISolution {31 Func<T Solution, IRandom, IEnumerable<TSolution>> GetNeighborsFunc { get; set; }29 internal interface INeighborBasedOperator<TEncodedSolution> : IEncodingOperator<TEncodedSolution> 30 where TEncodedSolution : class, IEncodedSolution { 31 Func<TEncodedSolution, IRandom, IEnumerable<TEncodedSolution>> GetNeighborsFunc { get; set; } 32 32 } 33 33 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/ISingleObjectiveAnalysisOperator.cs
r16723 r16751 26 26 namespace HeuristicLab.Optimization { 27 27 [StorableType("9731981c-10c6-4850-9308-a4720ac07da7")] 28 internal interface ISingleObjectiveAnalysisOperator<T Solution> : IEncodingOperator<TSolution>, ISingleObjectiveOperator29 where T Solution : class, ISolution {30 Action<T Solution[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; }28 internal interface ISingleObjectiveAnalysisOperator<TEncodedSolution> : IEncodingOperator<TEncodedSolution>, ISingleObjectiveOperator 29 where TEncodedSolution : class, IEncodedSolution { 30 Action<TEncodedSolution[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; } 31 31 } 32 32 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/ISingleObjectiveEvaluationOperator.cs
r16723 r16751 26 26 namespace HeuristicLab.Optimization { 27 27 [StorableType("5a9cf334-4815-4f0e-a2f8-f3d4edfcc829")] 28 internal interface ISingleObjectiveEvaluationOperator<T Solution> : ISingleObjectiveEvaluator, IEncodingOperator<TSolution>29 where T Solution : class, ISolution {30 Func<T Solution, IRandom, double> EvaluateFunc { get; set; }28 internal interface ISingleObjectiveEvaluationOperator<TEncodedSolution> : ISingleObjectiveEvaluator, IEncodingOperator<TEncodedSolution> 29 where TEncodedSolution : class, IEncodedSolution { 30 Func<TEncodedSolution, IRandom, double> EvaluateFunc { get; set; } 31 31 } 32 32 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/MultiObjectiveProblem.cs
r16723 r16751 21 21 22 22 using System.Linq; 23 using HEAL.Attic; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 26 using HeuristicLab.Data; 26 27 using HeuristicLab.Parameters; 27 using HEAL.Attic;28 28 29 29 namespace HeuristicLab.Optimization { 30 30 [StorableType("6F2EC371-0309-4848-B7B1-C9B9C7E3436F")] 31 public abstract class MultiObjectiveProblem<TEncoding, T Solution> :32 Problem<TEncoding, T Solution, MultiObjectiveEvaluator<TSolution>>,33 IMultiObjectiveProblem<TEncoding, T Solution>,34 IMultiObjectiveProblemDefinition<TEncoding, T Solution>35 where TEncoding : class, IEncoding<T Solution>36 where T Solution : class, ISolution {31 public abstract class MultiObjectiveProblem<TEncoding, TEncodedSolution> : 32 Problem<TEncoding, TEncodedSolution, MultiObjectiveEvaluator<TEncodedSolution>>, 33 IMultiObjectiveProblem<TEncoding, TEncodedSolution>, 34 IMultiObjectiveProblemDefinition<TEncoding, TEncodedSolution> 35 where TEncoding : class, IEncoding<TEncodedSolution> 36 where TEncodedSolution : class, IEncodedSolution { 37 37 38 38 [StorableConstructor] 39 39 protected MultiObjectiveProblem(StorableConstructorFlag _) : base(_) { } 40 40 41 protected MultiObjectiveProblem(MultiObjectiveProblem<TEncoding, T Solution> original, Cloner cloner)41 protected MultiObjectiveProblem(MultiObjectiveProblem<TEncoding, TEncodedSolution> original, Cloner cloner) 42 42 : base(original, cloner) { 43 43 ParameterizeOperators(); … … 49 49 50 50 Operators.Add(Evaluator); 51 Operators.Add(new MultiObjectiveAnalyzer<T Solution>());51 Operators.Add(new MultiObjectiveAnalyzer<TEncodedSolution>()); 52 52 53 53 ParameterizeOperators(); … … 60 60 61 61 public abstract bool[] Maximization { get; } 62 public abstract double[] Evaluate(T Solution individual, IRandom random);63 public virtual void Analyze(T Solution[] individuals, double[][] qualities, ResultCollection results, IRandom random) { }64 62 public abstract double[] Evaluate(TEncodedSolution individual, IRandom random); 63 public virtual void Analyze(TEncodedSolution[] individuals, double[][] qualities, ResultCollection results, IRandom random) { } 64 65 65 protected override void OnOperatorsChanged() { 66 66 base.OnOperatorsChanged(); … … 93 93 94 94 private void ParameterizeOperators() { 95 foreach (var op in Operators.OfType<IMultiObjectiveEvaluationOperator<T Solution>>())95 foreach (var op in Operators.OfType<IMultiObjectiveEvaluationOperator<TEncodedSolution>>()) 96 96 op.EvaluateFunc = Evaluate; 97 foreach (var op in Operators.OfType<IMultiObjectiveAnalysisOperator<T Solution>>())97 foreach (var op in Operators.OfType<IMultiObjectiveAnalysisOperator<TEncodedSolution>>()) 98 98 op.AnalyzeAction = Analyze; 99 99 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiObjectiveAnalyzer.cs
r16723 r16751 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HEAL.Attic; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; … … 28 29 using HeuristicLab.Operators; 29 30 using HeuristicLab.Parameters; 30 using HEAL.Attic;31 31 32 32 namespace HeuristicLab.Optimization { 33 33 [Item("Multi-objective Analyzer", "Calls the Analyze method of the problem definition.")] 34 34 [StorableType("903FE3D1-3179-4EA5-A7E1-63DE26239F9B")] 35 public class MultiObjectiveAnalyzer<T Solution> : SingleSuccessorOperator, IMultiObjectiveAnalysisOperator<TSolution>, IStochasticOperator36 where T Solution : class, ISolution {35 public class MultiObjectiveAnalyzer<TEncodedSolution> : SingleSuccessorOperator, IMultiObjectiveAnalysisOperator<TEncodedSolution>, IStochasticOperator 36 where TEncodedSolution : class, IEncodedSolution { 37 37 public bool EnabledByDefault { get { return true; } } 38 38 39 public ILookupParameter<IEncoding<T Solution>> EncodingParameter {40 get { return (ILookupParameter<IEncoding<T Solution>>)Parameters["Encoding"]; }39 public ILookupParameter<IEncoding<TEncodedSolution>> EncodingParameter { 40 get { return (ILookupParameter<IEncoding<TEncodedSolution>>)Parameters["Encoding"]; } 41 41 } 42 42 … … 53 53 } 54 54 55 public Action<T Solution[], double[][], ResultCollection, IRandom> AnalyzeAction { get; set; }55 public Action<TEncodedSolution[], double[][], ResultCollection, IRandom> AnalyzeAction { get; set; } 56 56 57 57 [StorableConstructor] 58 58 protected MultiObjectiveAnalyzer(StorableConstructorFlag _) : base(_) { } 59 protected MultiObjectiveAnalyzer(MultiObjectiveAnalyzer<T Solution> original, Cloner cloner) : base(original, cloner) { }59 protected MultiObjectiveAnalyzer(MultiObjectiveAnalyzer<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { } 60 60 public MultiObjectiveAnalyzer() { 61 61 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 62 Parameters.Add(new LookupParameter<IEncoding<T Solution>>("Encoding", "An item that holds the problem's encoding."));62 Parameters.Add(new LookupParameter<IEncoding<TEncodedSolution>>("Encoding", "An item that holds the problem's encoding.")); 63 63 Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The qualities of the parameter vector.")); 64 64 Parameters.Add(new LookupParameter<ResultCollection>("Results", "The results collection to write to.")); … … 66 66 67 67 public override IDeepCloneable Clone(Cloner cloner) { 68 return new MultiObjectiveAnalyzer<T Solution>(this, cloner);68 return new MultiObjectiveAnalyzer<TEncodedSolution>(this, cloner); 69 69 } 70 70 … … 78 78 scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b)); 79 79 80 var individuals = scopes.Select(s => ScopeUtil.Get Solution(s, encoding)).ToArray();80 var individuals = scopes.Select(s => ScopeUtil.GetEncodedSolution(s, encoding)).ToArray(); 81 81 AnalyzeAction(individuals, QualitiesParameter.ActualValue.Select(x => x.ToArray()).ToArray(), results, random); 82 82 return base.Apply(); -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiObjectiveEvaluator.cs
r16723 r16751 21 21 22 22 using System; 23 using HEAL.Attic; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 26 27 using HeuristicLab.Operators; 27 28 using HeuristicLab.Parameters; 28 using HEAL.Attic;29 29 30 30 namespace HeuristicLab.Optimization { 31 31 [Item("Multi-objective Evaluator", "Calls the Evaluate method of the problem definition and writes the return value into the scope.")] 32 32 [StorableType("C5605ED8-0ED2-4C7B-97A1-E7EB68A4FDBF")] 33 public class MultiObjectiveEvaluator<T Solution> : InstrumentedOperator, IMultiObjectiveEvaluationOperator<TSolution>, IStochasticOperator34 where T Solution : class, ISolution {33 public class MultiObjectiveEvaluator<TEncodedSolution> : InstrumentedOperator, IMultiObjectiveEvaluationOperator<TEncodedSolution>, IStochasticOperator 34 where TEncodedSolution : class, IEncodedSolution { 35 35 36 36 public ILookupParameter<IRandom> RandomParameter { … … 38 38 } 39 39 40 public ILookupParameter<IEncoding<T Solution>> EncodingParameter {41 get { return (ILookupParameter<IEncoding<T Solution>>)Parameters["Encoding"]; }40 public ILookupParameter<IEncoding<TEncodedSolution>> EncodingParameter { 41 get { return (ILookupParameter<IEncoding<TEncodedSolution>>)Parameters["Encoding"]; } 42 42 } 43 43 … … 46 46 } 47 47 48 public Func<T Solution, IRandom, double[]> EvaluateFunc { get; set; }48 public Func<TEncodedSolution, IRandom, double[]> EvaluateFunc { get; set; } 49 49 50 50 [StorableConstructor] 51 51 protected MultiObjectiveEvaluator(StorableConstructorFlag _) : base(_) { } 52 protected MultiObjectiveEvaluator(MultiObjectiveEvaluator<T Solution> original, Cloner cloner) : base(original, cloner) { }52 protected MultiObjectiveEvaluator(MultiObjectiveEvaluator<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { } 53 53 public MultiObjectiveEvaluator() { 54 54 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 55 Parameters.Add(new LookupParameter<IEncoding<T Solution>>("Encoding", "An item that holds the problem's encoding."));55 Parameters.Add(new LookupParameter<IEncoding<TEncodedSolution>>("Encoding", "An item that holds the problem's encoding.")); 56 56 Parameters.Add(new LookupParameter<DoubleArray>("Qualities", "The qualities of the parameter vector.")); 57 57 } 58 58 59 59 public override IDeepCloneable Clone(Cloner cloner) { 60 return new MultiObjectiveEvaluator<T Solution>(this, cloner);60 return new MultiObjectiveEvaluator<TEncodedSolution>(this, cloner); 61 61 } 62 62 … … 64 64 var random = RandomParameter.ActualValue; 65 65 var encoding = EncodingParameter.ActualValue; 66 var solution = ScopeUtil.Get Solution(ExecutionContext.Scope, encoding);66 var solution = ScopeUtil.GetEncodedSolution(ExecutionContext.Scope, encoding); 67 67 QualitiesParameter.ActualValue = new DoubleArray(EvaluateFunc(solution, random)); 68 68 return base.InstrumentedApply(); -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/ScopeUtil.cs
r13359 r16751 29 29 public static class ScopeUtil { 30 30 31 public static T Solution CopySolutionToScope<TSolution>(IScope scope, IEncoding<TSolution> encoding, TSolution solution)32 where T Solution : class,ISolution {33 return Copy SolutionToScope(scope, encoding.Name, solution);31 public static TEncodedSolution CopyEncodedSolutionToScope<TEncodedSolution>(IScope scope, IEncoding<TEncodedSolution> encoding, TEncodedSolution solution) 32 where TEncodedSolution : class, IEncodedSolution { 33 return CopyEncodedSolutionToScope(scope, encoding.Name, solution); 34 34 } 35 35 36 public static T Solution CopySolutionToScope<TSolution>(IScope scope, string name, TSolution solution)37 where T Solution : class,ISolution {38 var copy = (T Solution)solution.Clone();36 public static TEncodedSolution CopyEncodedSolutionToScope<TEncodedSolution>(IScope scope, string name, TEncodedSolution solution) 37 where TEncodedSolution : class, IEncodedSolution { 38 var copy = (TEncodedSolution)solution.Clone(); 39 39 if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, copy)); 40 40 else scope.Variables[name].Value = copy; … … 42 42 } 43 43 44 public static T Solution GetSolution<TSolution>(IScope scope, IEncoding<TSolution> encoding)45 where T Solution : class,ISolution {44 public static TEncodedSolution GetEncodedSolution<TEncodedSolution>(IScope scope, IEncoding<TEncodedSolution> encoding) 45 where TEncodedSolution : class, IEncodedSolution { 46 46 var name = encoding.Name; 47 47 if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name)); 48 var value = scope.Variables[name].Value as T Solution;49 if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null or not of type {1}.", name, typeof(T Solution).GetPrettyName()));48 var value = scope.Variables[name].Value as TEncodedSolution; 49 if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null or not of type {1}.", name, typeof(TEncodedSolution).GetPrettyName())); 50 50 return value; 51 51 } 52 52 53 public static I Solution GetSolution(IScope scope, IEncoding encoding) {54 return Get Solution(scope, encoding.Name);53 public static IEncodedSolution GetEncodedSolution(IScope scope, IEncoding encoding) { 54 return GetEncodedSolution(scope, encoding.Name); 55 55 } 56 56 57 public static I Solution GetSolution(IScope scope, string name) {57 public static IEncodedSolution GetEncodedSolution(IScope scope, string name) { 58 58 IVariable variable; 59 59 if (!scope.Variables.TryGetValue(name, out variable)) throw new ArgumentException(string.Format("{0} cannot be found in the provided scope.", name)); 60 var solution = variable.Value as I Solution;60 var solution = variable.Value as IEncodedSolution; 61 61 if (solution == null) throw new InvalidOperationException(string.Format("{0} is null or not of type ISolution.", name)); 62 62 return solution; -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveAnalyzer.cs
r16723 r16751 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HEAL.Attic; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; … … 28 29 using HeuristicLab.Operators; 29 30 using HeuristicLab.Parameters; 30 using HEAL.Attic;31 31 32 32 namespace HeuristicLab.Optimization { 33 33 [Item("Single-objective Analyzer", "Calls the script's Analyze method to be able to write into the results collection.")] 34 34 [StorableType("3D20F8E2-CE11-4021-A05B-CFCB02C0FD6F")] 35 public sealed class SingleObjectiveAnalyzer<T Solution> : SingleSuccessorOperator, ISingleObjectiveAnalysisOperator<TSolution>, IAnalyzer, IStochasticOperator36 where T Solution : class, ISolution {35 public sealed class SingleObjectiveAnalyzer<TEncodedSolution> : SingleSuccessorOperator, ISingleObjectiveAnalysisOperator<TEncodedSolution>, IAnalyzer, IStochasticOperator 36 where TEncodedSolution : class, IEncodedSolution { 37 37 public bool EnabledByDefault { get { return true; } } 38 38 39 public ILookupParameter<IEncoding<T Solution>> EncodingParameter {40 get { return (ILookupParameter<IEncoding<T Solution>>)Parameters["Encoding"]; }39 public ILookupParameter<IEncoding<TEncodedSolution>> EncodingParameter { 40 get { return (ILookupParameter<IEncoding<TEncodedSolution>>)Parameters["Encoding"]; } 41 41 } 42 42 … … 53 53 } 54 54 55 public Action<T Solution[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; }55 public Action<TEncodedSolution[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; } 56 56 57 57 [StorableConstructor] 58 58 private SingleObjectiveAnalyzer(StorableConstructorFlag _) : base(_) { } 59 private SingleObjectiveAnalyzer(SingleObjectiveAnalyzer<T Solution> original, Cloner cloner) : base(original, cloner) { }59 private SingleObjectiveAnalyzer(SingleObjectiveAnalyzer<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { } 60 60 public SingleObjectiveAnalyzer() { 61 Parameters.Add(new LookupParameter<IEncoding<T Solution>>("Encoding", "An item that holds the problem's encoding."));61 Parameters.Add(new LookupParameter<IEncoding<TEncodedSolution>>("Encoding", "An item that holds the problem's encoding.")); 62 62 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality of the parameter vector.")); 63 63 Parameters.Add(new LookupParameter<ResultCollection>("Results", "The results collection to write to.")); … … 66 66 67 67 public override IDeepCloneable Clone(Cloner cloner) { 68 return new SingleObjectiveAnalyzer<T Solution>(this, cloner);68 return new SingleObjectiveAnalyzer<TEncodedSolution>(this, cloner); 69 69 } 70 70 … … 78 78 scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b)); 79 79 80 var individuals = scopes.Select(s => ScopeUtil.Get Solution(s, encoding)).ToArray();80 var individuals = scopes.Select(s => ScopeUtil.GetEncodedSolution(s, encoding)).ToArray(); 81 81 AnalyzeAction(individuals, QualityParameter.ActualValue.Select(x => x.Value).ToArray(), results, random); 82 82 return base.Apply(); -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveEvaluator.cs
r16723 r16751 31 31 [Item("Single-objective Evaluator", "Calls the script's Evaluate method to get the quality value of the parameter vector.")] 32 32 [StorableType("E8914B68-D0D7-407F-8D58-002FDF2F45CF")] 33 public sealed class SingleObjectiveEvaluator<T Solution> : InstrumentedOperator, ISingleObjectiveEvaluationOperator<TSolution>, IStochasticOperator34 where T Solution : class, ISolution {33 public sealed class SingleObjectiveEvaluator<TEncodedSolution> : InstrumentedOperator, ISingleObjectiveEvaluationOperator<TEncodedSolution>, IStochasticOperator 34 where TEncodedSolution : class, IEncodedSolution { 35 35 36 36 public ILookupParameter<IRandom> RandomParameter { … … 38 38 } 39 39 40 public ILookupParameter<IEncoding<T Solution>> EncodingParameter {41 get { return (ILookupParameter<IEncoding<T Solution>>)Parameters["Encoding"]; }40 public ILookupParameter<IEncoding<TEncodedSolution>> EncodingParameter { 41 get { return (ILookupParameter<IEncoding<TEncodedSolution>>)Parameters["Encoding"]; } 42 42 } 43 43 … … 46 46 } 47 47 48 public Func<T Solution, IRandom, double> EvaluateFunc { get; set; }48 public Func<TEncodedSolution, IRandom, double> EvaluateFunc { get; set; } 49 49 50 50 [StorableConstructor] 51 51 private SingleObjectiveEvaluator(StorableConstructorFlag _) : base(_) { } 52 private SingleObjectiveEvaluator(SingleObjectiveEvaluator<T Solution> original, Cloner cloner) : base(original, cloner) { }52 private SingleObjectiveEvaluator(SingleObjectiveEvaluator<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { } 53 53 public SingleObjectiveEvaluator() { 54 54 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 55 Parameters.Add(new LookupParameter<IEncoding<T Solution>>("Encoding", "An item that holds the problem's encoding."));55 Parameters.Add(new LookupParameter<IEncoding<TEncodedSolution>>("Encoding", "An item that holds the problem's encoding.")); 56 56 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector.")); 57 57 } 58 58 59 public override IDeepCloneable Clone(Cloner cloner) { return new SingleObjectiveEvaluator<T Solution>(this, cloner); }59 public override IDeepCloneable Clone(Cloner cloner) { return new SingleObjectiveEvaluator<TEncodedSolution>(this, cloner); } 60 60 61 61 public override IOperation InstrumentedApply() { 62 62 var random = RandomParameter.ActualValue; 63 63 var encoding = EncodingParameter.ActualValue; 64 var solution = ScopeUtil.Get Solution(ExecutionContext.Scope, encoding);64 var solution = ScopeUtil.GetEncodedSolution(ExecutionContext.Scope, encoding); 65 65 QualityParameter.ActualValue = new DoubleValue(EvaluateFunc(solution, random)); 66 66 return base.InstrumentedApply(); -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveImprover.cs
r16723 r16751 33 33 [Item("Single-objective Improver", "Improves a solution by calling GetNeighbors and Evaluate of the corresponding problem definition.")] 34 34 [StorableType("7A917E09-920C-4B47-9599-67371101B35F")] 35 public sealed class SingleObjectiveImprover<T Solution> : SingleSuccessorOperator, INeighborBasedOperator<TSolution>, IImprovementOperator, ISingleObjectiveEvaluationOperator<TSolution>, IStochasticOperator36 where T Solution : class, ISolution {35 public sealed class SingleObjectiveImprover<TEncodedSolution> : SingleSuccessorOperator, INeighborBasedOperator<TEncodedSolution>, IImprovementOperator, ISingleObjectiveEvaluationOperator<TEncodedSolution>, IStochasticOperator 36 where TEncodedSolution : class, IEncodedSolution { 37 37 public ILookupParameter<IRandom> RandomParameter { 38 38 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 39 39 } 40 40 41 public ILookupParameter<IEncoding<T Solution>> EncodingParameter {42 get { return (ILookupParameter<IEncoding<T Solution>>)Parameters["Encoding"]; }41 public ILookupParameter<IEncoding<TEncodedSolution>> EncodingParameter { 42 get { return (ILookupParameter<IEncoding<TEncodedSolution>>)Parameters["Encoding"]; } 43 43 } 44 44 … … 63 63 } 64 64 65 public Func<T Solution, IRandom, double> EvaluateFunc { get; set; }66 public Func<T Solution, IRandom, IEnumerable<TSolution>> GetNeighborsFunc { get; set; }65 public Func<TEncodedSolution, IRandom, double> EvaluateFunc { get; set; } 66 public Func<TEncodedSolution, IRandom, IEnumerable<TEncodedSolution>> GetNeighborsFunc { get; set; } 67 67 68 68 [StorableConstructor] 69 69 private SingleObjectiveImprover(StorableConstructorFlag _) : base(_) { } 70 private SingleObjectiveImprover(SingleObjectiveImprover<T Solution> original, Cloner cloner) : base(original, cloner) { }70 private SingleObjectiveImprover(SingleObjectiveImprover<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { } 71 71 public SingleObjectiveImprover() { 72 72 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 73 Parameters.Add(new LookupParameter<IEncoding<T Solution>>("Encoding", "An item that holds the problem's encoding."));73 Parameters.Add(new LookupParameter<IEncoding<TEncodedSolution>>("Encoding", "An item that holds the problem's encoding.")); 74 74 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector.")); 75 75 Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Whether the problem should be minimized or maximized.")); … … 80 80 81 81 public override IDeepCloneable Clone(Cloner cloner) { 82 return new SingleObjectiveImprover<T Solution>(this, cloner);82 return new SingleObjectiveImprover<TEncodedSolution>(this, cloner); 83 83 } 84 84 … … 89 89 var maxAttempts = ImprovementAttemptsParameter.ActualValue.Value; 90 90 var sampleSize = SampleSizeParameter.ActualValue.Value; 91 var solution = ScopeUtil.Get Solution(ExecutionContext.Scope, encoding);91 var solution = ScopeUtil.GetEncodedSolution(ExecutionContext.Scope, encoding); 92 92 var quality = QualityParameter.ActualValue == null ? EvaluateFunc(solution, random) : QualityParameter.ActualValue.Value; 93 93 94 94 var count = 0; 95 95 for (var i = 0; i < maxAttempts; i++) { 96 T Solution best = default(TSolution);96 TEncodedSolution best = default(TEncodedSolution); 97 97 var bestQuality = quality; 98 98 foreach (var neighbor in GetNeighborsFunc(solution, random).Take(sampleSize)) { … … 111 111 QualityParameter.ActualValue = new DoubleValue(quality); 112 112 113 ScopeUtil.Copy SolutionToScope(ExecutionContext.Scope, encoding, solution);113 ScopeUtil.CopyEncodedSolutionToScope(ExecutionContext.Scope, encoding, solution); 114 114 return base.Apply(); 115 115 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveEvaluator.cs
r16723 r16751 21 21 22 22 using System; 23 using HEAL.Attic; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 26 27 using HeuristicLab.Operators; 27 28 using HeuristicLab.Parameters; 28 using HEAL.Attic;29 29 30 30 namespace HeuristicLab.Optimization { 31 31 [Item("Single-objective MoveEvaluator", "Evaluates a parameter vector that results from a move.")] 32 32 [StorableType("EE4B1EBA-50BF-40C7-B338-F4A9D9CC554E")] 33 public class SingleObjectiveMoveEvaluator<T Solution> : SingleSuccessorOperator, ISingleObjectiveEvaluationOperator<TSolution>, ISingleObjectiveMoveEvaluator, IStochasticOperator, ISingleObjectiveMoveOperator34 where T Solution : class, ISolution {33 public class SingleObjectiveMoveEvaluator<TEncodedSolution> : SingleSuccessorOperator, ISingleObjectiveEvaluationOperator<TEncodedSolution>, ISingleObjectiveMoveEvaluator, IStochasticOperator, ISingleObjectiveMoveOperator 34 where TEncodedSolution : class, IEncodedSolution { 35 35 36 36 public ILookupParameter<IRandom> RandomParameter { … … 38 38 } 39 39 40 public ILookupParameter<IEncoding<T Solution>> EncodingParameter {41 get { return (ILookupParameter<IEncoding<T Solution>>)Parameters["Encoding"]; }40 public ILookupParameter<IEncoding<TEncodedSolution>> EncodingParameter { 41 get { return (ILookupParameter<IEncoding<TEncodedSolution>>)Parameters["Encoding"]; } 42 42 } 43 43 … … 50 50 } 51 51 52 public Func<T Solution, IRandom, double> EvaluateFunc { get; set; }52 public Func<TEncodedSolution, IRandom, double> EvaluateFunc { get; set; } 53 53 54 54 [StorableConstructor] 55 55 protected SingleObjectiveMoveEvaluator(StorableConstructorFlag _) : base(_) { } 56 protected SingleObjectiveMoveEvaluator(SingleObjectiveMoveEvaluator<T Solution> original, Cloner cloner) : base(original, cloner) { }56 protected SingleObjectiveMoveEvaluator(SingleObjectiveMoveEvaluator<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { } 57 57 public SingleObjectiveMoveEvaluator() { 58 58 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 59 Parameters.Add(new LookupParameter<IEncoding<T Solution>>("Encoding", "An item that holds the problem's encoding."));59 Parameters.Add(new LookupParameter<IEncoding<TEncodedSolution>>("Encoding", "An item that holds the problem's encoding.")); 60 60 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector.")); 61 61 Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The quality of the move.")); … … 63 63 64 64 public override IDeepCloneable Clone(Cloner cloner) { 65 return new SingleObjectiveMoveEvaluator<T Solution>(this, cloner);65 return new SingleObjectiveMoveEvaluator<TEncodedSolution>(this, cloner); 66 66 } 67 67 … … 69 69 var random = RandomParameter.ActualValue; 70 70 var encoding = EncodingParameter.ActualValue; 71 var individual = ScopeUtil.Get Solution(ExecutionContext.Scope, encoding);71 var individual = ScopeUtil.GetEncodedSolution(ExecutionContext.Scope, encoding); 72 72 MoveQualityParameter.ActualValue = new DoubleValue(EvaluateFunc(individual, random)); 73 73 return base.Apply(); -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveGenerator.cs
r16723 r16751 34 34 [Item("Single-objective MoveGenerator", "Calls the GetNeighbors method of the problem definition to obtain the moves.")] 35 35 [StorableType("CB37E7D8-EAC3-4061-9D39-20538CD1064D")] 36 public class SingleObjectiveMoveGenerator<T Solution> : SingleSuccessorOperator, INeighborBasedOperator<TSolution>, IMultiMoveGenerator, IStochasticOperator, ISingleObjectiveMoveOperator37 where T Solution : class, ISolution {36 public class SingleObjectiveMoveGenerator<TEncodedSolution> : SingleSuccessorOperator, INeighborBasedOperator<TEncodedSolution>, IMultiMoveGenerator, IStochasticOperator, ISingleObjectiveMoveOperator 37 where TEncodedSolution : class, IEncodedSolution { 38 38 public ILookupParameter<IRandom> RandomParameter { 39 39 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } … … 44 44 } 45 45 46 public ILookupParameter<IEncoding<T Solution>> EncodingParameter {47 get { return (ILookupParameter<IEncoding<T Solution>>)Parameters["Encoding"]; }46 public ILookupParameter<IEncoding<TEncodedSolution>> EncodingParameter { 47 get { return (ILookupParameter<IEncoding<TEncodedSolution>>)Parameters["Encoding"]; } 48 48 } 49 49 50 public Func<T Solution, IRandom, IEnumerable<TSolution>> GetNeighborsFunc { get; set; }50 public Func<TEncodedSolution, IRandom, IEnumerable<TEncodedSolution>> GetNeighborsFunc { get; set; } 51 51 52 52 [StorableConstructor] 53 53 protected SingleObjectiveMoveGenerator(StorableConstructorFlag _) : base(_) { } 54 protected SingleObjectiveMoveGenerator(SingleObjectiveMoveGenerator<T Solution> original, Cloner cloner)54 protected SingleObjectiveMoveGenerator(SingleObjectiveMoveGenerator<TEncodedSolution> original, Cloner cloner) 55 55 : base(original, cloner) { } 56 56 public SingleObjectiveMoveGenerator() { 57 57 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 58 58 Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to sample.")); 59 Parameters.Add(new LookupParameter<IEncoding<T Solution>>("Encoding", "An item that holds the problem's encoding."));59 Parameters.Add(new LookupParameter<IEncoding<TEncodedSolution>>("Encoding", "An item that holds the problem's encoding.")); 60 60 } 61 61 62 62 public override IDeepCloneable Clone(Cloner cloner) { 63 return new SingleObjectiveMoveGenerator<T Solution>(this, cloner);63 return new SingleObjectiveMoveGenerator<TEncodedSolution>(this, cloner); 64 64 } 65 65 … … 68 68 var sampleSize = SampleSizeParameter.ActualValue.Value; 69 69 var encoding = EncodingParameter.ActualValue; 70 var solution = ScopeUtil.Get Solution(ExecutionContext.Scope, encoding);70 var solution = ScopeUtil.GetEncodedSolution(ExecutionContext.Scope, encoding); 71 71 var nbhood = GetNeighborsFunc(solution, random).Take(sampleSize).ToList(); 72 72 … … 74 74 for (int i = 0; i < moveScopes.Length; i++) { 75 75 moveScopes[i] = new Scope(i.ToString(CultureInfo.InvariantCulture.NumberFormat)); 76 ScopeUtil.Copy SolutionToScope(moveScopes[i], encoding, nbhood[i]);76 ScopeUtil.CopyEncodedSolutionToScope(moveScopes[i], encoding, nbhood[i]); 77 77 } 78 78 ExecutionContext.Scope.SubScopes.AddRange(moveScopes); -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveMaker.cs
r16723 r16751 21 21 22 22 using System; 23 using HEAL.Attic; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 26 27 using HeuristicLab.Operators; 27 28 using HeuristicLab.Parameters; 28 using HEAL.Attic;29 29 30 30 namespace HeuristicLab.Optimization { 31 31 [Item("Single-objective MoveMaker", "Applies a move.")] 32 32 [StorableType("C0ABF392-C825-4B98-8FB9-5749A9091FD6")] 33 public class SingleObjectiveMoveMaker<T Solution> : InstrumentedOperator, IMoveMaker, ISingleObjectiveMoveOperator34 where T Solution : class, ISolution {35 public ILookupParameter<IEncoding<T Solution>> EncodingParameter {36 get { return (ILookupParameter<IEncoding<T Solution>>)Parameters["Encoding"]; }33 public class SingleObjectiveMoveMaker<TEncodedSolution> : InstrumentedOperator, IMoveMaker, ISingleObjectiveMoveOperator 34 where TEncodedSolution : class, IEncodedSolution { 35 public ILookupParameter<IEncoding<TEncodedSolution>> EncodingParameter { 36 get { return (ILookupParameter<IEncoding<TEncodedSolution>>)Parameters["Encoding"]; } 37 37 } 38 38 … … 47 47 [StorableConstructor] 48 48 protected SingleObjectiveMoveMaker(StorableConstructorFlag _) : base(_) { } 49 protected SingleObjectiveMoveMaker(SingleObjectiveMoveMaker<T Solution> original, Cloner cloner) : base(original, cloner) { }49 protected SingleObjectiveMoveMaker(SingleObjectiveMoveMaker<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { } 50 50 public SingleObjectiveMoveMaker() { 51 Parameters.Add(new LookupParameter<IEncoding<T Solution>>("Encoding", "An item that holds the problem's encoding."));51 Parameters.Add(new LookupParameter<IEncoding<TEncodedSolution>>("Encoding", "An item that holds the problem's encoding.")); 52 52 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector.")); 53 53 Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The quality of the move.")); … … 55 55 56 56 public override IDeepCloneable Clone(Cloner cloner) { 57 return new SingleObjectiveMoveMaker<T Solution>(this, cloner);57 return new SingleObjectiveMoveMaker<TEncodedSolution>(this, cloner); 58 58 } 59 59 … … 62 62 63 63 var encoding = EncodingParameter.ActualValue; 64 var solution = ScopeUtil.Get Solution(ExecutionContext.Scope, encoding);65 ScopeUtil.Copy SolutionToScope(ExecutionContext.Scope.Parent.Parent, encoding, solution);64 var solution = ScopeUtil.GetEncodedSolution(ExecutionContext.Scope, encoding); 65 ScopeUtil.CopyEncodedSolutionToScope(ExecutionContext.Scope.Parent.Parent, encoding, solution); 66 66 67 67 if (QualityParameter.ActualValue == null) QualityParameter.ActualValue = new DoubleValue(MoveQualityParameter.ActualValue.Value); -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Problem.cs
r16723 r16751 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HEAL.Attic; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 28 using HeuristicLab.Parameters; 28 using HEAL.Attic;29 29 30 30 namespace HeuristicLab.Optimization { 31 31 [StorableType("D877082E-9E77-4CB1-ABDB-35F63878E116")] 32 public abstract class Problem<TEncoding, T Solution, TEvaluator> : Problem,33 IHeuristicOptimizationProblem, IProblemDefinition<TEncoding, T Solution>, IStorableContent34 where TEncoding : class, IEncoding<T Solution>35 where T Solution : class, ISolution32 public abstract class Problem<TEncoding, TEncodedSolution, TEvaluator> : Problem, 33 IHeuristicOptimizationProblem, IProblemDefinition<TEncoding, TEncodedSolution>, IStorableContent 34 where TEncoding : class, IEncoding<TEncodedSolution> 35 where TEncodedSolution : class, IEncodedSolution 36 36 where TEvaluator : class, IEvaluator { 37 37 38 38 public string Filename { get; set; } // TODO: Really okay here? should be in Problem (non-generic) 39 39 40 //TODO remove paramet r for encoding?40 //TODO remove parameter for encoding? 41 41 protected IValueParameter<TEncoding> EncodingParameter { 42 42 get { return (IValueParameter<TEncoding>)Parameters["Encoding"]; } … … 115 115 } 116 116 117 protected Problem(Problem<TEncoding, T Solution, TEvaluator> original, Cloner cloner)117 protected Problem(Problem<TEncoding, TEncodedSolution, TEvaluator> original, Cloner cloner) 118 118 : base(original, cloner) { 119 119 oldEncoding = cloner.Clone(original.oldEncoding); … … 152 152 oldEncoding = Encoding; 153 153 154 foreach (var op in Operators.OfType<IEncodingOperator<T Solution>>())154 foreach (var op in Operators.OfType<IEncodingOperator<TEncodedSolution>>()) 155 155 op.EncodingParameter.ActualName = EncodingParameter.Name; 156 156 -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveProblem.cs
r16723 r16751 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HEAL.Attic; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 28 using HeuristicLab.Data; 28 29 using HeuristicLab.Parameters; 29 using HEAL.Attic;30 30 31 31 namespace HeuristicLab.Optimization { 32 32 [StorableType("2697320D-0259-44BB-BD71-7EE1B10F664C")] 33 public abstract class SingleObjectiveProblem<TEncoding, T Solution> :34 Problem<TEncoding, T Solution, SingleObjectiveEvaluator<TSolution>>,35 ISingleObjectiveProblem<TEncoding, T Solution>,36 ISingleObjectiveProblemDefinition<TEncoding, T Solution>37 where TEncoding : class, IEncoding<T Solution>38 where T Solution : class, ISolution {33 public abstract class SingleObjectiveProblem<TEncoding, TEncodedSolution> : 34 Problem<TEncoding, TEncodedSolution, SingleObjectiveEvaluator<TEncodedSolution>>, 35 ISingleObjectiveProblem<TEncoding, TEncodedSolution>, 36 ISingleObjectiveProblemDefinition<TEncoding, TEncodedSolution> 37 where TEncoding : class, IEncoding<TEncodedSolution> 38 where TEncodedSolution : class, IEncodedSolution { 39 39 40 40 protected IValueParameter<DoubleValue> BestKnownQualityParameter { … … 64 64 protected SingleObjectiveProblem(StorableConstructorFlag _) : base(_) { } 65 65 66 protected SingleObjectiveProblem(SingleObjectiveProblem<TEncoding, T Solution> original, Cloner cloner)66 protected SingleObjectiveProblem(SingleObjectiveProblem<TEncoding, TEncodedSolution> original, Cloner cloner) 67 67 : base(original, cloner) { 68 68 ParameterizeOperators(); … … 75 75 76 76 Operators.Add(Evaluator); 77 Operators.Add(new SingleObjectiveAnalyzer<T Solution>());78 Operators.Add(new SingleObjectiveImprover<T Solution>());79 Operators.Add(new SingleObjectiveMoveEvaluator<T Solution>());80 Operators.Add(new SingleObjectiveMoveGenerator<T Solution>());81 Operators.Add(new SingleObjectiveMoveMaker<T Solution>());77 Operators.Add(new SingleObjectiveAnalyzer<TEncodedSolution>()); 78 Operators.Add(new SingleObjectiveImprover<TEncodedSolution>()); 79 Operators.Add(new SingleObjectiveMoveEvaluator<TEncodedSolution>()); 80 Operators.Add(new SingleObjectiveMoveGenerator<TEncodedSolution>()); 81 Operators.Add(new SingleObjectiveMoveMaker<TEncodedSolution>()); 82 82 83 83 ParameterizeOperators(); … … 90 90 91 91 Operators.Add(Evaluator); 92 Operators.Add(new SingleObjectiveAnalyzer<T Solution>());93 Operators.Add(new SingleObjectiveImprover<T Solution>());94 Operators.Add(new SingleObjectiveMoveEvaluator<T Solution>());95 Operators.Add(new SingleObjectiveMoveGenerator<T Solution>());96 Operators.Add(new SingleObjectiveMoveMaker<T Solution>());92 Operators.Add(new SingleObjectiveAnalyzer<TEncodedSolution>()); 93 Operators.Add(new SingleObjectiveImprover<TEncodedSolution>()); 94 Operators.Add(new SingleObjectiveMoveEvaluator<TEncodedSolution>()); 95 Operators.Add(new SingleObjectiveMoveGenerator<TEncodedSolution>()); 96 Operators.Add(new SingleObjectiveMoveMaker<TEncodedSolution>()); 97 97 98 98 ParameterizeOperators(); … … 105 105 106 106 public abstract bool Maximization { get; } 107 public abstract double Evaluate(T Solution solution, IRandom random);108 public virtual void Analyze(T Solution[] solutions, double[] qualities, ResultCollection results, IRandom random) { }109 public virtual IEnumerable<T Solution> GetNeighbors(TSolution solution, IRandom random) {110 return Enumerable.Empty<T Solution>();107 public abstract double Evaluate(TEncodedSolution solution, IRandom random); 108 public virtual void Analyze(TEncodedSolution[] solutions, double[] qualities, ResultCollection results, IRandom random) { } 109 public virtual IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution solution, IRandom random) { 110 return Enumerable.Empty<TEncodedSolution>(); 111 111 } 112 112 … … 115 115 } 116 116 117 protected Tuple<T Solution, double> GetBestSolution(TSolution[] solutions, double[] qualities) {117 protected Tuple<TEncodedSolution, double> GetBestSolution(TEncodedSolution[] solutions, double[] qualities) { 118 118 return GetBestSolution(solutions, qualities, Maximization); 119 119 } 120 public static Tuple<T Solution, double> GetBestSolution(TSolution[] solutions, double[] qualities, bool maximization) {120 public static Tuple<TEncodedSolution, double> GetBestSolution(TEncodedSolution[] solutions, double[] qualities, bool maximization) { 121 121 var zipped = solutions.Zip(qualities, (s, q) => new { Solution = s, Quality = q }); 122 122 var best = (maximization ? zipped.OrderByDescending(z => z.Quality) : zipped.OrderBy(z => z.Quality)).First(); … … 154 154 155 155 private void ParameterizeOperators() { 156 foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator<T Solution>>())156 foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator<TEncodedSolution>>()) 157 157 op.EvaluateFunc = Evaluate; 158 foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator<T Solution>>())158 foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator<TEncodedSolution>>()) 159 159 op.AnalyzeAction = Analyze; 160 foreach (var op in Operators.OfType<INeighborBasedOperator<T Solution>>())160 foreach (var op in Operators.OfType<INeighborBasedOperator<TEncodedSolution>>()) 161 161 op.GetNeighborsFunc = GetNeighbors; 162 162 }
Note: See TracChangeset
for help on using the changeset viewer.