Changeset 13336
- Timestamp:
- 11/23/15 15:15:27 (9 years ago)
- Location:
- branches/ProblemRefactoring
- Files:
-
- 34 edited
- 3 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVector.cs
r12012 r13336 23 23 using HeuristicLab.Core; 24 24 using HeuristicLab.Data; 25 using HeuristicLab.Optimization; 25 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 27 … … 28 29 [StorableClass] 29 30 [Item("BinaryVector", "Represents a vector of binary values.")] 30 public class BinaryVector : BoolArray {31 public class BinaryVector : BoolArray, ISolution { 31 32 [StorableConstructor] 32 33 protected BinaryVector(bool deserializing) : base(deserializing) { } -
branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorEncoding.cs
r12012 r13336 34 34 [Item("BinaryVectorEncoding", "Describes a binary vector encoding.")] 35 35 [StorableClass] 36 public sealed class BinaryVectorEncoding : Encoding< IBinaryVectorCreator> {36 public sealed class BinaryVectorEncoding : Encoding<BinaryVector> { 37 37 #region Encoding Parameters 38 38 [Storable] … … 166 166 #endregion 167 167 } 168 169 public static class IndividualExtensionMethods {170 public static BinaryVector BinaryVector(this Individual individual) {171 var encoding = individual.GetEncoding<BinaryVectorEncoding>();172 return individual.BinaryVector(encoding.Name);173 }174 175 public static BinaryVector BinaryVector(this Individual individual, string name) {176 return (BinaryVector)individual[name];177 }178 }179 168 } -
branches/ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Interfaces/IBinaryVectorCreator.cs
r12012 r13336 28 28 /// An interface which represents an operator for creating vectors of bool-valued data. 29 29 /// </summary> 30 public interface IBinaryVectorCreator : IBinaryVectorOperator, ISolutionCreator {30 public interface IBinaryVectorCreator : IBinaryVectorOperator, ISolutionCreator<BinaryVector> { 31 31 IValueLookupParameter<IntValue> LengthParameter { get; } 32 32 ILookupParameter<BinaryVector> BinaryVectorParameter { get; } -
branches/ProblemRefactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeProblem.cs
r12921 r13336 32 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 33 33 [StorableClass] 34 public abstract class SymbolicExpressionTreeProblem : SingleObjective BasicProblem<SymbolicExpressionTreeEncoding> {34 public abstract class SymbolicExpressionTreeProblem : SingleObjectiveProblem<SymbolicExpressionTreeEncoding> { 35 35 36 36 // persistence -
branches/ProblemRefactoring/HeuristicLab.Optimization.Views/3.3/HeuristicLab.Optimization.Views-3.3.csproj
r12975 r13336 142 142 <DependentUpon>BatchRunView.cs</DependentUpon> 143 143 </Compile> 144 <Compile Include="CreateNewSingleEncodingDialog.cs">145 <SubType>Form</SubType>146 </Compile>147 <Compile Include="CreateNewSingleEncodingDialog.Designer.cs">148 <DependentUpon>CreateNewSingleEncodingDialog.cs</DependentUpon>149 </Compile>150 144 <Compile Include="ISolutionSimilarityCalculatorView.cs"> 151 145 <SubType>UserControl</SubType> … … 159 153 <Compile Include="ExperimentListView.Designer.cs"> 160 154 <DependentUpon>ExperimentListView.cs</DependentUpon> 161 </Compile>162 <Compile Include="MultiEncodingView.cs">163 <SubType>UserControl</SubType>164 </Compile>165 <Compile Include="MultiEncodingView.Designer.cs">166 <DependentUpon>MultiEncodingView.cs</DependentUpon>167 155 </Compile> 168 156 <Compile Include="Plugin.cs" /> … … 427 415 </ProjectReference> 428 416 </ItemGroup> 429 <ItemGroup>430 <EmbeddedResource Include="CreateNewSingleEncodingDialog.resx">431 <DependentUpon>CreateNewSingleEncodingDialog.cs</DependentUpon>432 </EmbeddedResource>433 </ItemGroup>434 417 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 435 418 <!-- To modify your build process, add your task inside one of the targets below and uncomment it. -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Encoding.cs
r12981 r13336 31 31 [Item("Encoding", "Base class for describing different encodings.")] 32 32 [StorableClass] 33 public abstract class Encoding<T > : ParameterizedNamedItem, IEncoding34 where T : class,ISolutionCreator{33 public abstract class Encoding<TSolution> : ParameterizedNamedItem, IEncoding<TSolution> 34 where TSolution : class,ISolution { 35 35 public override sealed bool CanChangeName { 36 36 get { return false; } … … 48 48 get { return encodingOperators; } 49 49 set { 50 if (!value.OfType< T>().Any())50 if (!value.OfType<ISolutionCreator<TSolution>>().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 T newSolutionCreator = (T)encodingOperators.FirstOrDefault(o => o.GetType() == solutionCreator.GetType()) ??56 encodingOperators.OfType< T>().First();55 ISolutionCreator<TSolution> newSolutionCreator = (ISolutionCreator<TSolution>)encodingOperators.FirstOrDefault(o => o.GetType() == solutionCreator.GetType()) ?? 56 encodingOperators.OfType<ISolutionCreator<TSolution>>().First(); 57 57 SolutionCreator = newSolutionCreator; 58 58 OnOperatorsChanged(); … … 60 60 } 61 61 62 ISolutionCreator IEncoding.SolutionCreator {63 get { return SolutionCreator; }64 set {65 if (!(value is T)) throw new ArgumentException(string.Format("Cannot assign the solution creator {0} to the encoding {1}.", value.GetType().GetPrettyName(), GetType().GetPrettyName()));66 SolutionCreator = (T)value;67 }68 }69 62 [Storable] 70 private TsolutionCreator;71 public TSolutionCreator {63 private ISolutionCreator<TSolution> solutionCreator; 64 public ISolutionCreator<TSolution> SolutionCreator { 72 65 get { 73 66 return solutionCreator; … … 85 78 [StorableConstructor] 86 79 protected Encoding(bool deserializing) : base(deserializing) { } 87 protected Encoding(Encoding<T > original, Cloner cloner)80 protected Encoding(Encoding<TSolution> original, Cloner cloner) 88 81 : base(original, cloner) { 89 82 encodingOperators = cloner.Clone(original.encodingOperators); … … 93 86 : base(name) { 94 87 Parameters.Add(new FixedValueParameter<ReadOnlyItemSet<IOperator>>(name + ".Operators", "The operators that the encoding specifies.", encodingOperators.AsReadOnly())); 95 }96 97 public virtual Individual GetIndividual(IScope scope) {98 return new SingleEncodingIndividual(this, scope);99 88 } 100 89 -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IEncoding.cs
r12012 r13336 25 25 26 26 namespace HeuristicLab.Optimization { 27 public interface IEncoding : IParameterizedNamedItem { 28 ISolutionCreator SolutionCreator { get; set; } 27 public interface IEncoding<TSolution> : IParameterizedNamedItem 28 where TSolution : class, ISolution { 29 ISolutionCreator<TSolution> SolutionCreator { get; set; } 29 30 IEnumerable<IOperator> Operators { get; set; } 30 31 31 Individual GetIndividual(IScope scope);32 32 void ConfigureOperator(IOperator @operator); 33 33 void ConfigureOperators(IEnumerable<IOperator> operators); … … 36 36 event EventHandler OperatorsChanged; 37 37 } 38 39 38 40 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IEncodingOperator.cs
r12012 r13336 23 23 24 24 namespace HeuristicLab.Optimization { 25 public interface IEncodingOperator : IOperator{26 ILookupParameter<IEncoding > EncodingParameter { get; }25 public interface IEncodingOperator<TSolution> : IOperator where TSolution : class, ISolution{ 26 ILookupParameter<IEncoding<TSolution>> EncodingParameter { get; } 27 27 } 28 28 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IMultiEncodingOperator.cs
r12012 r13336 23 23 24 24 namespace HeuristicLab.Optimization { 25 public interface IMultiEncodingOperator : IOperator{26 void AddEncoding(IEncoding encoding);27 bool RemoveEncoding(IEncoding encoding);25 public interface IMultiEncodingOperator<TSolution> : IOperator where TSolution : class,ISolution { 26 void AddEncoding(IEncoding<TSolution> encoding); 27 bool RemoveEncoding(IEncoding<TSolution> encoding); 28 28 } 29 29 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IMultiObjectiveProblemDefinition.cs
r12012 r13336 23 23 24 24 namespace HeuristicLab.Optimization { 25 public interface IMultiObjectiveProblemDefinition : IProblemDefinition { 25 public interface IMultiObjectiveProblemDefinition<TSolution> : IProblemDefinition 26 where TSolution : class, ISolution { 26 27 bool[] Maximization { get; } 27 double[] Evaluate( Individualindividual, IRandom random);28 void Analyze( Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random);28 double[] Evaluate(TSolution individual, IRandom random); 29 void Analyze(TSolution[] individuals, double[][] qualities, ResultCollection results, IRandom random); 29 30 } 30 31 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IProblemDefinition.cs
r12012 r13336 22 22 namespace HeuristicLab.Optimization { 23 23 public interface IProblemDefinition { 24 IEncoding Encoding { get; }24 //IEncoding Encoding { get; } 25 25 } 26 26 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/ISingleObjectiveProblemDefinition.cs
r12012 r13336 24 24 25 25 namespace HeuristicLab.Optimization { 26 public interface ISingleObjectiveProblemDefinition : IProblemDefinition { 26 public interface ISingleObjectiveProblemDefinition<TSolution> : IProblemDefinition 27 where TSolution : class, ISolution { 27 28 bool Maximization { get; } 28 double Evaluate( Individualindividual, IRandom random);29 void Analyze( Individual[] individuals, double[] qualities, ResultCollection results, IRandom random);30 IEnumerable< Individual> GetNeighbors(Individualindividual, IRandom random);29 double Evaluate(TSolution individual, IRandom random); 30 void Analyze(TSolution[] individuals, double[] qualities, ResultCollection results, IRandom random); 31 IEnumerable<TSolution> GetNeighbors(TSolution individual, IRandom random); 31 32 } 32 33 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/IMultiObjectiveAnalysisOperator.cs
r12012 r13336 24 24 25 25 namespace HeuristicLab.Optimization { 26 internal interface IMultiObjectiveAnalysisOperator : IEncodingOperator, IAnalyzer, IMultiObjectiveOperator { 27 Action<Individual[], double[][], ResultCollection, IRandom> AnalyzeAction { get; set; } 26 internal interface IMultiObjectiveAnalysisOperator<TSolution> : IEncodingOperator<TSolution>, IAnalyzer, IMultiObjectiveOperator 27 where TSolution : class, ISolution { 28 Action<TSolution[], double[][], ResultCollection, IRandom> AnalyzeAction { get; set; } 28 29 } 29 30 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/IMultiObjectiveEvaluationOperator.cs
r12012 r13336 24 24 25 25 namespace HeuristicLab.Optimization { 26 internal interface IMultiObjectiveEvaluationOperator : IMultiObjectiveEvaluator, IEncodingOperator { 27 Func<Individual, IRandom, double[]> EvaluateFunc { get; set; } 26 internal interface IMultiObjectiveEvaluationOperator<TSolution> : IMultiObjectiveEvaluator, IEncodingOperator<TSolution> 27 where TSolution : class, ISolution { 28 Func<TSolution, IRandom, double[]> EvaluateFunc { get; set; } 28 29 } 29 30 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/INeighborBasedOperator.cs
r12012 r13336 25 25 26 26 namespace HeuristicLab.Optimization { 27 internal interface INeighborBasedOperator : IEncodingOperator { 28 Func<Individual, IRandom, IEnumerable<Individual>> GetNeighborsFunc { get; set; } 27 internal interface INeighborBasedOperator<TSolution> : IEncodingOperator<TSolution> 28 where TSolution : class, ISolution { 29 Func<TSolution, IRandom, IEnumerable<TSolution>> GetNeighborsFunc { get; set; } 29 30 } 30 31 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/ISingleObjectiveAnalysisOperator.cs
r12012 r13336 24 24 25 25 namespace HeuristicLab.Optimization { 26 internal interface ISingleObjectiveAnalysisOperator : IEncodingOperator, ISingleObjectiveOperator { 27 Action<Individual[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; } 26 internal interface ISingleObjectiveAnalysisOperator<TSolution> : IEncodingOperator<TSolution>, ISingleObjectiveOperator 27 where TSolution : class, ISolution { 28 Action<TSolution[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; } 28 29 } 29 30 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/ISingleObjectiveEvaluationOperator.cs
r12012 r13336 24 24 25 25 namespace HeuristicLab.Optimization { 26 internal interface ISingleObjectiveEvaluationOperator : ISingleObjectiveEvaluator, IEncodingOperator { 27 Func<Individual, IRandom, double> EvaluateFunc { get; set; } 26 internal interface ISingleObjectiveEvaluationOperator<TSolution> : ISingleObjectiveEvaluator, IEncodingOperator<TSolution> 27 where TSolution : class, ISolution { 28 Func<TSolution, IRandom, double> EvaluateFunc { get; set; } 28 29 } 29 30 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/MultiObjectiveProblem.cs
r13335 r13336 29 29 namespace HeuristicLab.Optimization { 30 30 [StorableClass] 31 public abstract class MultiObjective BasicProblem<TEncoding> : BasicProblem<TEncoding, MultiObjectiveEvaluator>, IMultiObjectiveHeuristicOptimizationProblem, IMultiObjectiveProblemDefinition32 where T Encoding : class, IEncoding{31 public abstract class MultiObjectiveProblem<TSolution> : Problem<TSolution, MultiObjectiveEvaluator<TSolution>>, IMultiObjectiveHeuristicOptimizationProblem, IMultiObjectiveProblemDefinition<TSolution> 32 where TSolution : class, ISolution { 33 33 [StorableConstructor] 34 protected MultiObjective BasicProblem(bool deserializing) : base(deserializing) { }34 protected MultiObjectiveProblem(bool deserializing) : base(deserializing) { } 35 35 36 protected MultiObjective BasicProblem(MultiObjectiveBasicProblem<TEncoding> original, Cloner cloner)36 protected MultiObjectiveProblem(MultiObjectiveProblem<TSolution> original, Cloner cloner) 37 37 : base(original, cloner) { 38 38 ParameterizeOperators(); 39 39 } 40 40 41 protected MultiObjective BasicProblem()41 protected MultiObjectiveProblem() 42 42 : base() { 43 43 Parameters.Add(new ValueParameter<BoolArray>("Maximization", "Set to false if the problem should be minimized.", (BoolArray)new BoolArray(Maximization).AsReadOnly())); 44 44 45 45 Operators.Add(Evaluator); 46 Operators.Add(new MultiObjectiveAnalyzer ());46 Operators.Add(new MultiObjectiveAnalyzer<TSolution>()); 47 47 48 48 ParameterizeOperators(); … … 55 55 56 56 public abstract bool[] Maximization { get; } 57 public abstract double[] Evaluate( Individualindividual, IRandom random);58 public virtual void Analyze( Individual[] individuals, double[][] qualities, ResultCollection results, IRandom random) { }57 public abstract double[] Evaluate(TSolution individual, IRandom random); 58 public virtual void Analyze(TSolution[] individuals, double[][] qualities, ResultCollection results, IRandom random) { } 59 59 60 protected override void OnOperatorsChanged() { 61 base.OnOperatorsChanged(); 62 if (Encoding != null) { 63 PruneSingleObjectiveOperators(Encoding); 64 var multiEncoding = Encoding as MultiEncoding; 65 if (multiEncoding != null) { 66 foreach (var encoding in multiEncoding.Encodings.ToList()) { 67 PruneSingleObjectiveOperators(encoding); 68 } 69 } 70 } 71 } 60 //TODO 61 //protected override void OnOperatorsChanged() { 62 // base.OnOperatorsChanged(); 63 // if (Encoding != null) { 64 // PruneSingleObjectiveOperators(Encoding); 65 // var multiEncoding = Encoding as MultiEncoding; 66 // if (multiEncoding != null) { 67 // foreach (var encoding in multiEncoding.Encodings.ToList()) { 68 // PruneSingleObjectiveOperators(encoding); 69 // } 70 // } 71 // } 72 //} 72 73 73 private void PruneSingleObjectiveOperators(IEncoding encoding) {74 if (encoding != null && encoding.Operators.Any(x => x is ISingleObjectiveOperator && !(x is IMultiObjectiveOperator)))75 encoding.Operators = encoding.Operators.Where(x => !(x is ISingleObjectiveOperator) || x is IMultiObjectiveOperator).ToList();76 }74 //private void PruneSingleObjectiveOperators(IEncoding encoding) { 75 // if (encoding != null && encoding.Operators.Any(x => x is ISingleObjectiveOperator && !(x is IMultiObjectiveOperator))) 76 // encoding.Operators = encoding.Operators.Where(x => !(x is ISingleObjectiveOperator) || x is IMultiObjectiveOperator).ToList(); 77 //} 77 78 78 79 protected override void OnEvaluatorChanged() { … … 82 83 83 84 private void ParameterizeOperators() { 84 foreach (var op in Operators.OfType<IMultiObjectiveEvaluationOperator >())85 foreach (var op in Operators.OfType<IMultiObjectiveEvaluationOperator<TSolution>>()) 85 86 op.EvaluateFunc = Evaluate; 86 foreach (var op in Operators.OfType<IMultiObjectiveAnalysisOperator >())87 foreach (var op in Operators.OfType<IMultiObjectiveAnalysisOperator<TSolution>>()) 87 88 op.AnalyzeAction = Analyze; 88 89 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiObjectiveAnalyzer.cs
r11961 r13336 33 33 [Item("Multi-objective Analyzer", "Calls the Analyze method of the problem definition.")] 34 34 [StorableClass] 35 public class MultiObjectiveAnalyzer : SingleSuccessorOperator, IMultiObjectiveAnalysisOperator, IStochasticOperator { 35 public class MultiObjectiveAnalyzer<TSolution> : SingleSuccessorOperator, IMultiObjectiveAnalysisOperator<TSolution>, IStochasticOperator 36 where TSolution : class, ISolution { 36 37 public bool EnabledByDefault { get { return true; } } 37 38 38 public ILookupParameter<IEncoding > EncodingParameter {39 get { return (ILookupParameter<IEncoding >)Parameters["Encoding"]; }39 public ILookupParameter<IEncoding<TSolution>> EncodingParameter { 40 get { return (ILookupParameter<IEncoding<TSolution>>)Parameters["Encoding"]; } 40 41 } 41 42 … … 52 53 } 53 54 54 public Action< Individual[], double[][], ResultCollection, IRandom> AnalyzeAction { get; set; }55 public Action<TSolution[], double[][], ResultCollection, IRandom> AnalyzeAction { get; set; } 55 56 56 57 [StorableConstructor] 57 58 protected MultiObjectiveAnalyzer(bool deserializing) : base(deserializing) { } 58 protected MultiObjectiveAnalyzer(MultiObjectiveAnalyzer original, Cloner cloner) : base(original, cloner) { }59 protected MultiObjectiveAnalyzer(MultiObjectiveAnalyzer<TSolution> original, Cloner cloner) : base(original, cloner) { } 59 60 public MultiObjectiveAnalyzer() { 60 61 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 61 Parameters.Add(new LookupParameter<IEncoding >("Encoding", "An item that holds the problem's encoding."));62 Parameters.Add(new LookupParameter<IEncoding<TSolution>>("Encoding", "An item that holds the problem's encoding.")); 62 63 Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The qualities of the parameter vector.")); 63 64 Parameters.Add(new LookupParameter<ResultCollection>("Results", "The results collection to write to.")); … … 65 66 66 67 public override IDeepCloneable Clone(Cloner cloner) { 67 return new MultiObjectiveAnalyzer (this, cloner);68 return new MultiObjectiveAnalyzer<TSolution>(this, cloner); 68 69 } 69 70 … … 77 78 scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b)); 78 79 79 var individuals = scopes.Select( encoding.GetIndividual).ToArray();80 var individuals = scopes.Select(s => ScopeUtil.GetSolution(s, encoding)).ToArray(); 80 81 AnalyzeAction(individuals, QualitiesParameter.ActualValue.Select(x => x.ToArray()).ToArray(), results, random); 81 82 return base.Apply(); -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiObjectiveEvaluator.cs
r12617 r13336 31 31 [Item("Multi-objective Evaluator", "Calls the Evaluate method of the problem definition and writes the return value into the scope.")] 32 32 [StorableClass] 33 public class MultiObjectiveEvaluator : InstrumentedOperator, IMultiObjectiveEvaluationOperator, IStochasticOperator { 33 public class MultiObjectiveEvaluator<TSolution> : InstrumentedOperator, IMultiObjectiveEvaluationOperator<TSolution>, IStochasticOperator 34 where TSolution : class, ISolution { 34 35 35 36 public ILookupParameter<IRandom> RandomParameter { … … 37 38 } 38 39 39 public ILookupParameter<IEncoding > EncodingParameter {40 get { return (ILookupParameter<IEncoding >)Parameters["Encoding"]; }40 public ILookupParameter<IEncoding<TSolution>> EncodingParameter { 41 get { return (ILookupParameter<IEncoding<TSolution>>)Parameters["Encoding"]; } 41 42 } 42 43 … … 45 46 } 46 47 47 public Func< Individual, IRandom, double[]> EvaluateFunc { get; set; }48 public Func<TSolution, IRandom, double[]> EvaluateFunc { get; set; } 48 49 49 50 [StorableConstructor] 50 51 protected MultiObjectiveEvaluator(bool deserializing) : base(deserializing) { } 51 protected MultiObjectiveEvaluator(MultiObjectiveEvaluator original, Cloner cloner) : base(original, cloner) { }52 protected MultiObjectiveEvaluator(MultiObjectiveEvaluator<TSolution> original, Cloner cloner) : base(original, cloner) { } 52 53 public MultiObjectiveEvaluator() { 53 54 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 54 Parameters.Add(new LookupParameter<IEncoding >("Encoding", "An item that holds the problem's encoding."));55 Parameters.Add(new LookupParameter<IEncoding<TSolution>>("Encoding", "An item that holds the problem's encoding.")); 55 56 Parameters.Add(new LookupParameter<DoubleArray>("Qualities", "The qualities of the parameter vector.")); 56 57 } 57 58 58 59 public override IDeepCloneable Clone(Cloner cloner) { 59 return new MultiObjectiveEvaluator (this, cloner);60 return new MultiObjectiveEvaluator<TSolution>(this, cloner); 60 61 } 61 62 … … 63 64 var random = RandomParameter.ActualValue; 64 65 var encoding = EncodingParameter.ActualValue; 65 var individual = encoding.GetIndividual(ExecutionContext.Scope);66 QualitiesParameter.ActualValue = new DoubleArray(EvaluateFunc( individual, random));66 var solution = ScopeUtil.GetSolution(ExecutionContext.Scope, encoding); 67 QualitiesParameter.ActualValue = new DoubleArray(EvaluateFunc(solution, random)); 67 68 return base.InstrumentedApply(); 68 69 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveAnalyzer.cs
r11961 r13336 33 33 [Item("Single-objective Analyzer", "Calls the script's Analyze method to be able to write into the results collection.")] 34 34 [StorableClass] 35 public sealed class SingleObjectiveAnalyzer : SingleSuccessorOperator, ISingleObjectiveAnalysisOperator, IAnalyzer, IStochasticOperator { 35 public sealed class SingleObjectiveAnalyzer<TSolution> : SingleSuccessorOperator, ISingleObjectiveAnalysisOperator<TSolution>, IAnalyzer, IStochasticOperator 36 where TSolution : class, ISolution { 36 37 public bool EnabledByDefault { get { return true; } } 37 38 38 public ILookupParameter<IEncoding > EncodingParameter {39 get { return (ILookupParameter<IEncoding >)Parameters["Encoding"]; }39 public ILookupParameter<IEncoding<TSolution>> EncodingParameter { 40 get { return (ILookupParameter<IEncoding<TSolution>>)Parameters["Encoding"]; } 40 41 } 41 42 … … 52 53 } 53 54 54 public Action< Individual[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; }55 public Action<TSolution[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; } 55 56 56 57 [StorableConstructor] 57 58 private SingleObjectiveAnalyzer(bool deserializing) : base(deserializing) { } 58 private SingleObjectiveAnalyzer(SingleObjectiveAnalyzer original, Cloner cloner) : base(original, cloner) { }59 private SingleObjectiveAnalyzer(SingleObjectiveAnalyzer<TSolution> original, Cloner cloner) : base(original, cloner) { } 59 60 public SingleObjectiveAnalyzer() { 60 Parameters.Add(new LookupParameter<IEncoding >("Encoding", "An item that holds the problem's encoding."));61 Parameters.Add(new LookupParameter<IEncoding<TSolution>>("Encoding", "An item that holds the problem's encoding.")); 61 62 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality of the parameter vector.")); 62 63 Parameters.Add(new LookupParameter<ResultCollection>("Results", "The results collection to write to.")); … … 65 66 66 67 public override IDeepCloneable Clone(Cloner cloner) { 67 return new SingleObjectiveAnalyzer (this, cloner);68 return new SingleObjectiveAnalyzer<TSolution>(this, cloner); 68 69 } 69 70 … … 77 78 scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b)); 78 79 79 var individuals = scopes.Select( encoding.GetIndividual).ToArray();80 var individuals = scopes.Select(s => ScopeUtil.GetSolution(s, encoding)).ToArray(); 80 81 AnalyzeAction(individuals, QualityParameter.ActualValue.Select(x => x.Value).ToArray(), results, random); 81 82 return base.Apply(); -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveEvaluator.cs
r12617 r13336 31 31 [Item("Single-objective Evaluator", "Calls the script's Evaluate method to get the quality value of the parameter vector.")] 32 32 [StorableClass] 33 public sealed class SingleObjectiveEvaluator : InstrumentedOperator, ISingleObjectiveEvaluationOperator, IStochasticOperator { 33 public sealed class SingleObjectiveEvaluator<TSolution> : InstrumentedOperator, ISingleObjectiveEvaluationOperator<TSolution>, IStochasticOperator 34 where TSolution : class, ISolution { 34 35 35 36 public ILookupParameter<IRandom> RandomParameter { … … 37 38 } 38 39 39 public ILookupParameter<IEncoding > EncodingParameter {40 get { return (ILookupParameter<IEncoding >)Parameters["Encoding"]; }40 public ILookupParameter<IEncoding<TSolution>> EncodingParameter { 41 get { return (ILookupParameter<IEncoding<TSolution>>)Parameters["Encoding"]; } 41 42 } 42 43 … … 45 46 } 46 47 47 public Func< Individual, IRandom, double> EvaluateFunc { get; set; }48 public Func<TSolution, IRandom, double> EvaluateFunc { get; set; } 48 49 49 50 [StorableConstructor] 50 51 private SingleObjectiveEvaluator(bool deserializing) : base(deserializing) { } 51 private SingleObjectiveEvaluator(SingleObjectiveEvaluator original, Cloner cloner) : base(original, cloner) { }52 private SingleObjectiveEvaluator(SingleObjectiveEvaluator<TSolution> original, Cloner cloner) : base(original, cloner) { } 52 53 public SingleObjectiveEvaluator() { 53 54 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 54 Parameters.Add(new LookupParameter<IEncoding >("Encoding", "An item that holds the problem's encoding."));55 Parameters.Add(new LookupParameter<IEncoding<TSolution>>("Encoding", "An item that holds the problem's encoding.")); 55 56 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector.")); 56 57 } 57 58 58 public override IDeepCloneable Clone(Cloner cloner) { return new SingleObjectiveEvaluator (this, cloner); }59 public override IDeepCloneable Clone(Cloner cloner) { return new SingleObjectiveEvaluator<TSolution>(this, cloner); } 59 60 60 61 public override IOperation InstrumentedApply() { 61 62 var random = RandomParameter.ActualValue; 62 63 var encoding = EncodingParameter.ActualValue; 63 var individual = encoding.GetIndividual(ExecutionContext.Scope);64 QualityParameter.ActualValue = new DoubleValue(EvaluateFunc( individual, random));64 var solution = ScopeUtil.GetSolution(ExecutionContext.Scope, encoding); 65 QualityParameter.ActualValue = new DoubleValue(EvaluateFunc(solution, random)); 65 66 return base.InstrumentedApply(); 66 67 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveImprover.cs
r12012 r13336 33 33 [Item("Single-objective Improver", "Improves a solution by calling GetNeighbors and Evaluate of the corresponding problem definition.")] 34 34 [StorableClass] 35 public sealed class SingleObjectiveImprover : SingleSuccessorOperator, INeighborBasedOperator, IImprovementOperator, ISingleObjectiveEvaluationOperator, IStochasticOperator { 35 public sealed class SingleObjectiveImprover<TSolution> : SingleSuccessorOperator, INeighborBasedOperator<TSolution>, IImprovementOperator, ISingleObjectiveEvaluationOperator<TSolution>, IStochasticOperator 36 where TSolution : class, ISolution { 36 37 public ILookupParameter<IRandom> RandomParameter { 37 38 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 38 39 } 39 40 40 public ILookupParameter<IEncoding > EncodingParameter {41 get { return (ILookupParameter<IEncoding >)Parameters["Encoding"]; }41 public ILookupParameter<IEncoding<TSolution>> EncodingParameter { 42 get { return (ILookupParameter<IEncoding<TSolution>>)Parameters["Encoding"]; } 42 43 } 43 44 … … 62 63 } 63 64 64 public Func< Individual, IRandom, double> EvaluateFunc { get; set; }65 public Func< Individual, IRandom, IEnumerable<Individual>> GetNeighborsFunc { get; set; }65 public Func<TSolution, IRandom, double> EvaluateFunc { get; set; } 66 public Func<TSolution, IRandom, IEnumerable<TSolution>> GetNeighborsFunc { get; set; } 66 67 67 68 [StorableConstructor] 68 69 private SingleObjectiveImprover(bool deserializing) : base(deserializing) { } 69 private SingleObjectiveImprover(SingleObjectiveImprover original, Cloner cloner) : base(original, cloner) { }70 private SingleObjectiveImprover(SingleObjectiveImprover<TSolution> original, Cloner cloner) : base(original, cloner) { } 70 71 public SingleObjectiveImprover() { 71 72 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 72 Parameters.Add(new LookupParameter<IEncoding >("Encoding", "An item that holds the problem's encoding."));73 Parameters.Add(new LookupParameter<IEncoding<TSolution>>("Encoding", "An item that holds the problem's encoding.")); 73 74 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector.")); 74 75 Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Whether the problem should be minimized or maximized.")); … … 79 80 80 81 public override IDeepCloneable Clone(Cloner cloner) { 81 return new SingleObjectiveImprover (this, cloner);82 return new SingleObjectiveImprover<TSolution>(this, cloner); 82 83 } 83 84 … … 88 89 var maxAttempts = ImprovementAttemptsParameter.ActualValue.Value; 89 90 var sampleSize = SampleSizeParameter.ActualValue.Value; 90 var individual = encoding.GetIndividual(ExecutionContext.Scope);91 var quality = QualityParameter.ActualValue == null ? EvaluateFunc( individual, random) : QualityParameter.ActualValue.Value;91 var solution = ScopeUtil.GetSolution(ExecutionContext.Scope, encoding); 92 var quality = QualityParameter.ActualValue == null ? EvaluateFunc(solution, random) : QualityParameter.ActualValue.Value; 92 93 93 94 var count = 0; 94 95 for (var i = 0; i < maxAttempts; i++) { 95 Individual best = null;96 TSolution best = default(TSolution); 96 97 var bestQuality = quality; 97 foreach (var neighbor in GetNeighborsFunc( individual, random).Take(sampleSize)) {98 foreach (var neighbor in GetNeighborsFunc(solution, random).Take(sampleSize)) { 98 99 var q = EvaluateFunc(neighbor, random); 99 100 count++; … … 103 104 } 104 105 if (best == null) break; 105 individual= best;106 solution = best; 106 107 quality = bestQuality; 107 108 } … … 109 110 LocalEvaluatedSolutionsParameter.ActualValue = new IntValue(count); 110 111 QualityParameter.ActualValue = new DoubleValue(quality); 111 individual.CopyToScope(ExecutionContext.Scope); 112 113 ScopeUtil.CopySolutionToScope(ExecutionContext.Scope, encoding, solution); 112 114 return base.Apply(); 113 115 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveEvaluator.cs
r12012 r13336 31 31 [Item("Single-objective MoveEvaluator", "Evaluates a parameter vector that results from a move.")] 32 32 [StorableClass] 33 public class SingleObjectiveMoveEvaluator : SingleSuccessorOperator, ISingleObjectiveEvaluationOperator, ISingleObjectiveMoveEvaluator, IStochasticOperator, ISingleObjectiveMoveOperator { 33 public class SingleObjectiveMoveEvaluator<TSolution> : SingleSuccessorOperator, ISingleObjectiveEvaluationOperator<TSolution>, ISingleObjectiveMoveEvaluator, IStochasticOperator, ISingleObjectiveMoveOperator 34 where TSolution : class, ISolution { 34 35 35 36 public ILookupParameter<IRandom> RandomParameter { … … 37 38 } 38 39 39 public ILookupParameter<IEncoding > EncodingParameter {40 get { return (ILookupParameter<IEncoding >)Parameters["Encoding"]; }40 public ILookupParameter<IEncoding<TSolution>> EncodingParameter { 41 get { return (ILookupParameter<IEncoding<TSolution>>)Parameters["Encoding"]; } 41 42 } 42 43 … … 49 50 } 50 51 51 public Func< Individual, IRandom, double> EvaluateFunc { get; set; }52 public Func<TSolution, IRandom, double> EvaluateFunc { get; set; } 52 53 53 54 [StorableConstructor] 54 55 protected SingleObjectiveMoveEvaluator(bool deserializing) : base(deserializing) { } 55 protected SingleObjectiveMoveEvaluator(SingleObjectiveMoveEvaluator original, Cloner cloner) : base(original, cloner) { }56 protected SingleObjectiveMoveEvaluator(SingleObjectiveMoveEvaluator<TSolution> original, Cloner cloner) : base(original, cloner) { } 56 57 public SingleObjectiveMoveEvaluator() { 57 58 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 58 Parameters.Add(new LookupParameter<IEncoding >("Encoding", "An item that holds the problem's encoding."));59 Parameters.Add(new LookupParameter<IEncoding<TSolution>>("Encoding", "An item that holds the problem's encoding.")); 59 60 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector.")); 60 61 Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The quality of the move.")); … … 62 63 63 64 public override IDeepCloneable Clone(Cloner cloner) { 64 return new SingleObjectiveMoveEvaluator (this, cloner);65 return new SingleObjectiveMoveEvaluator<TSolution>(this, cloner); 65 66 } 66 67 … … 68 69 var random = RandomParameter.ActualValue; 69 70 var encoding = EncodingParameter.ActualValue; 70 var individual = encoding.GetIndividual(ExecutionContext.Scope);71 var individual = ScopeUtil.GetSolution(ExecutionContext.Scope, encoding); 71 72 MoveQualityParameter.ActualValue = new DoubleValue(EvaluateFunc(individual, random)); 72 73 return base.Apply(); -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveGenerator.cs
r12012 r13336 34 34 [Item("Single-objective MoveGenerator", "Calls the GetNeighbors method of the problem definition to obtain the moves.")] 35 35 [StorableClass] 36 public class SingleObjectiveMoveGenerator : SingleSuccessorOperator, INeighborBasedOperator, IMultiMoveGenerator, IStochasticOperator, ISingleObjectiveMoveOperator { 36 public class SingleObjectiveMoveGenerator<TSolution> : SingleSuccessorOperator, INeighborBasedOperator<TSolution>, IMultiMoveGenerator, IStochasticOperator, ISingleObjectiveMoveOperator 37 where TSolution : class, ISolution { 37 38 public ILookupParameter<IRandom> RandomParameter { 38 39 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } … … 43 44 } 44 45 45 public ILookupParameter<IEncoding > EncodingParameter {46 get { return (ILookupParameter<IEncoding >)Parameters["Encoding"]; }46 public ILookupParameter<IEncoding<TSolution>> EncodingParameter { 47 get { return (ILookupParameter<IEncoding<TSolution>>)Parameters["Encoding"]; } 47 48 } 48 49 49 public Func< Individual, IRandom, IEnumerable<Individual>> GetNeighborsFunc { get; set; }50 public Func<TSolution, IRandom, IEnumerable<TSolution>> GetNeighborsFunc { get; set; } 50 51 51 52 [StorableConstructor] 52 53 protected SingleObjectiveMoveGenerator(bool deserializing) : base(deserializing) { } 53 protected SingleObjectiveMoveGenerator(SingleObjectiveMoveGenerator original, Cloner cloner)54 protected SingleObjectiveMoveGenerator(SingleObjectiveMoveGenerator<TSolution> original, Cloner cloner) 54 55 : base(original, cloner) { } 55 56 public SingleObjectiveMoveGenerator() { 56 57 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 57 58 Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to sample.")); 58 Parameters.Add(new LookupParameter<IEncoding >("Encoding", "An item that holds the problem's encoding."));59 Parameters.Add(new LookupParameter<IEncoding<TSolution>>("Encoding", "An item that holds the problem's encoding.")); 59 60 } 60 61 61 62 public override IDeepCloneable Clone(Cloner cloner) { 62 return new SingleObjectiveMoveGenerator (this, cloner);63 return new SingleObjectiveMoveGenerator<TSolution>(this, cloner); 63 64 } 64 65 … … 67 68 var sampleSize = SampleSizeParameter.ActualValue.Value; 68 69 var encoding = EncodingParameter.ActualValue; 69 var individual = encoding.GetIndividual(ExecutionContext.Scope);70 var nbhood = GetNeighborsFunc( individual, random).Take(sampleSize).ToList();70 var solution = ScopeUtil.GetSolution(ExecutionContext.Scope, encoding); 71 var nbhood = GetNeighborsFunc(solution, random).Take(sampleSize).ToList(); 71 72 72 73 var moveScopes = new Scope[nbhood.Count]; 73 74 for (int i = 0; i < moveScopes.Length; i++) { 74 75 moveScopes[i] = new Scope(i.ToString(CultureInfo.InvariantCulture.NumberFormat)); 75 nbhood[i].CopyToScope(moveScopes[i]);76 ScopeUtil.CopySolutionToScope(moveScopes[i], encoding, nbhood[i]); 76 77 } 77 78 ExecutionContext.Scope.SubScopes.AddRange(moveScopes); -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveMaker.cs
r12012 r13336 31 31 [Item("Single-objective MoveMaker", "Applies a move.")] 32 32 [StorableClass] 33 public class SingleObjectiveMoveMaker : InstrumentedOperator, IMoveMaker, ISingleObjectiveMoveOperator { 34 public ILookupParameter<IEncoding> EncodingParameter { 35 get { return (ILookupParameter<IEncoding>)Parameters["Encoding"]; } 33 public class SingleObjectiveMoveMaker<TSolution> : InstrumentedOperator, IMoveMaker, ISingleObjectiveMoveOperator 34 where TSolution : class, ISolution { 35 public ILookupParameter<IEncoding<TSolution>> EncodingParameter { 36 get { return (ILookupParameter<IEncoding<TSolution>>)Parameters["Encoding"]; } 36 37 } 37 38 … … 46 47 [StorableConstructor] 47 48 protected SingleObjectiveMoveMaker(bool deserializing) : base(deserializing) { } 48 protected SingleObjectiveMoveMaker(SingleObjectiveMoveMaker original, Cloner cloner) : base(original, cloner) { }49 protected SingleObjectiveMoveMaker(SingleObjectiveMoveMaker<TSolution> original, Cloner cloner) : base(original, cloner) { } 49 50 public SingleObjectiveMoveMaker() { 50 Parameters.Add(new LookupParameter<IEncoding >("Encoding", "An item that holds the problem's encoding."));51 Parameters.Add(new LookupParameter<IEncoding<TSolution>>("Encoding", "An item that holds the problem's encoding.")); 51 52 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector.")); 52 53 Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The quality of the move.")); … … 54 55 55 56 public override IDeepCloneable Clone(Cloner cloner) { 56 return new SingleObjectiveMoveMaker (this, cloner);57 return new SingleObjectiveMoveMaker<TSolution>(this, cloner); 57 58 } 58 59 … … 61 62 62 63 var encoding = EncodingParameter.ActualValue; 63 var individual = encoding.GetIndividual(ExecutionContext.Scope);64 individual.CopyToScope(ExecutionContext.Scope.Parent.Parent);64 var solution = ScopeUtil.GetSolution(ExecutionContext.Scope, encoding); 65 ScopeUtil.CopySolutionToScope(ExecutionContext.Scope.Parent.Parent, encoding, solution); 65 66 66 67 if (QualityParameter.ActualValue == null) QualityParameter.ActualValue = new DoubleValue(MoveQualityParameter.ActualValue.Value); -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Problem.cs
r13335 r13336 30 30 namespace HeuristicLab.Optimization { 31 31 [StorableClass] 32 public abstract class BasicProblem<TEncoding, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator>, IProblemDefinition, IStorableContent33 where T Encoding : class, IEncoding32 public abstract class Problem<TSolution, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator<TSolution>>, IProblemDefinition, IStorableContent 33 where TSolution : class, ISolution 34 34 where TEvaluator : class, IEvaluator { 35 35 36 36 public string Filename { get; set; } 37 37 38 protected IValueParameter< TEncoding> EncodingParameter {39 get { return (IValueParameter< TEncoding>)Parameters["Encoding"]; }38 protected IValueParameter<IEncoding<TSolution>> EncodingParameter { 39 get { return (IValueParameter<IEncoding<TSolution>>)Parameters["Encoding"]; } 40 40 } 41 41 42 //mkommend necessary for reuse of operators if the encoding changes 43 private TEncoding oldEncoding; 44 45 IEncoding IProblemDefinition.Encoding { get { return Encoding; } } 46 public TEncoding Encoding { 42 public IEncoding<TSolution> Encoding { 47 43 get { return EncodingParameter.Value; } 48 44 protected set { … … 63 59 } 64 60 65 protected BasicProblem()61 protected Problem() 66 62 : base() { 67 Parameters.Add(new ValueParameter<TEncoding>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any.")); 68 oldEncoding = Encoding; 69 if(Encoding != null) Parameterize(); 63 Parameters.Add(new ValueParameter<IEncoding<TSolution>>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any.")); 64 if (Encoding != null) Parameterize(); 70 65 RegisterEvents(); 71 66 } 72 67 73 protected BasicProblem(BasicProblem<TEncoding, TEvaluator> original, Cloner cloner)68 protected Problem(Problem<TSolution, TEvaluator> original, Cloner cloner) 74 69 : base(original, cloner) { 75 oldEncoding = cloner.Clone(original.oldEncoding);76 70 RegisterEvents(); 77 71 } 78 72 79 73 [StorableConstructor] 80 protected BasicProblem(bool deserializing) : base(deserializing) { }74 protected Problem(bool deserializing) : base(deserializing) { } 81 75 [StorableHook(HookType.AfterDeserialization)] 82 76 private void AfterDeserialization() { 83 oldEncoding = Encoding;84 77 RegisterEvents(); 85 78 } … … 87 80 private void RegisterEvents() { 88 81 EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged(); 89 var multiEncoding = Encoding as MultiEncoding;90 if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;82 //var multiEncoding = Encoding as MultiEncoding; 83 //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged; 91 84 } 92 85 … … 99 92 100 93 private void Parameterize() { 101 if (oldEncoding != null) { 102 AdaptEncodingOperators(oldEncoding, Encoding); 103 var oldMultiEncoding = oldEncoding as MultiEncoding; 104 if (oldMultiEncoding != null) 105 oldMultiEncoding.EncodingsChanged -= MultiEncodingOnEncodingsChanged; 106 } 107 oldEncoding = Encoding; 108 109 foreach (var op in Operators.OfType<IEncodingOperator>()) 94 foreach (var op in Operators.OfType<IEncodingOperator<TSolution>>()) 110 95 op.EncodingParameter.ActualName = EncodingParameter.Name; 111 96 112 var solutionCreatorType = Encoding.GetType().BaseType.GetGenericArguments(); 113 var paramType = typeof(ValueParameter<>).MakeGenericType(solutionCreatorType); 114 var solutionCreatorParam = (IParameter)Activator.CreateInstance(paramType, SolutionCreatorParameter.Name, SolutionCreatorParameter.Description, 115 Encoding.SolutionCreator); 116 Parameters.Remove(SolutionCreatorParameter); 117 Parameters.Add(solutionCreatorParam); 118 ((IValueParameter)solutionCreatorParam).ValueChanged += SolutionCreatorParameter_ValueChanged; 119 120 var multiEncoding = Encoding as MultiEncoding; 121 if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged; 97 //var multiEncoding = Encoding as MultiEncoding; 98 //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged; 122 99 } 123 100 … … 127 104 } 128 105 129 private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) {130 if (oldEncoding.GetType() != newEncoding.GetType()) return;131 132 if (oldEncoding.GetType() == typeof(MultiEncoding)) {133 var oldMultiEncoding = (MultiEncoding)oldEncoding;134 var newMultiEncoding = (MultiEncoding)newEncoding;135 if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return;136 137 var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n });138 foreach (var multi in nestedEncodings)139 AdaptEncodingOperators(multi.oldEnc, multi.newEnc);140 }141 142 var comparer = new TypeEqualityComparer<IOperator>();143 var cloner = new Cloner();144 var oldOperators = oldEncoding.Operators;145 var newOperators = newEncoding.Operators;146 147 cloner.RegisterClonedObject(oldEncoding, newEncoding);148 var operators = oldOperators.Intersect(newOperators, comparer)149 .Select(cloner.Clone)150 .Union(newOperators, comparer).ToList();151 152 newEncoding.ConfigureOperators(operators);153 newEncoding.Operators = operators;154 }155 156 106 protected virtual void MultiEncodingOnEncodingsChanged(object sender, EventArgs e) { 157 107 OnOperatorsChanged(); -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveProblem.cs
r13335 r13336 30 30 namespace HeuristicLab.Optimization { 31 31 [StorableClass] 32 public abstract class SingleObjective BasicProblem<TEncoding> : BasicProblem<TEncoding, SingleObjectiveEvaluator>,33 ISingleObjectiveProblemDefinition , ISingleObjectiveHeuristicOptimizationProblem34 where T Encoding : class, IEncoding{32 public abstract class SingleObjectiveProblem<TSolution> : Problem<TSolution, SingleObjectiveEvaluator<TSolution>>, 33 ISingleObjectiveProblemDefinition<TSolution>, ISingleObjectiveHeuristicOptimizationProblem 34 where TSolution : class, ISolution { 35 35 36 36 protected IValueParameter<DoubleValue> BestKnownQualityParameter { … … 50 50 51 51 [StorableConstructor] 52 protected SingleObjective BasicProblem(bool deserializing) : base(deserializing) { }52 protected SingleObjectiveProblem(bool deserializing) : base(deserializing) { } 53 53 54 protected SingleObjective BasicProblem(SingleObjectiveBasicProblem<TEncoding> original, Cloner cloner)54 protected SingleObjectiveProblem(SingleObjectiveProblem<TSolution> original, Cloner cloner) 55 55 : base(original, cloner) { 56 56 ParameterizeOperators(); 57 57 } 58 58 59 protected SingleObjective BasicProblem()59 protected SingleObjectiveProblem() 60 60 : base() { 61 61 Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true }); … … 63 63 64 64 Operators.Add(Evaluator); 65 Operators.Add(new SingleObjectiveAnalyzer ());66 Operators.Add(new SingleObjectiveImprover ());67 Operators.Add(new SingleObjectiveMoveEvaluator ());68 Operators.Add(new SingleObjectiveMoveGenerator ());69 Operators.Add(new SingleObjectiveMoveMaker ());65 Operators.Add(new SingleObjectiveAnalyzer<TSolution>()); 66 Operators.Add(new SingleObjectiveImprover<TSolution>()); 67 Operators.Add(new SingleObjectiveMoveEvaluator<TSolution>()); 68 Operators.Add(new SingleObjectiveMoveGenerator<TSolution>()); 69 Operators.Add(new SingleObjectiveMoveMaker<TSolution>()); 70 70 71 71 ParameterizeOperators(); … … 78 78 79 79 public abstract bool Maximization { get; } 80 public abstract double Evaluate( Individualindividual, IRandom random);81 public virtual void Analyze( Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { }82 public virtual IEnumerable< Individual> GetNeighbors(Individualindividual, IRandom random) {83 return Enumerable.Empty< Individual>();80 public abstract double Evaluate(TSolution individual, IRandom random); 81 public virtual void Analyze(TSolution[] individuals, double[] qualities, ResultCollection results, IRandom random) { } 82 public virtual IEnumerable<TSolution> GetNeighbors(TSolution individual, IRandom random) { 83 return Enumerable.Empty<TSolution>(); 84 84 } 85 85 86 protected override void OnOperatorsChanged() { 87 base.OnOperatorsChanged(); 88 if (Encoding != null) { 89 PruneMultiObjectiveOperators(Encoding); 90 var multiEncoding = Encoding as MultiEncoding; 91 if (multiEncoding != null) { 92 foreach (var encoding in multiEncoding.Encodings.ToList()) { 93 PruneMultiObjectiveOperators(encoding); 94 } 95 } 96 } 86 public virtual bool IsBetter(double quality, double bestQuality) { 87 return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality); 97 88 } 98 89 99 private void PruneMultiObjectiveOperators(IEncoding encoding) { 100 if (encoding.Operators.Any(x => x is IMultiObjectiveOperator && !(x is ISingleObjectiveOperator))) 101 encoding.Operators = encoding.Operators.Where(x => !(x is IMultiObjectiveOperator) || x is ISingleObjectiveOperator).ToList(); 102 } 90 //TODO 91 //protected override void OnOperatorsChanged() { 92 // base.OnOperatorsChanged(); 93 // if (Encoding != null) { 94 // PruneMultiObjectiveOperators(Encoding); 95 // var multiEncoding = Encoding as MultiEncoding; 96 // if (multiEncoding != null) { 97 // foreach (var encoding in multiEncoding.Encodings.ToList()) { 98 // PruneMultiObjectiveOperators(encoding); 99 // } 100 // } 101 // } 102 //} 103 104 //private void PruneMultiObjectiveOperators(IEncoding<TSolution> encoding) { 105 // if (encoding.Operators.Any(x => x is IMultiObjectiveOperator && !(x is ISingleObjectiveOperator))) 106 // encoding.Operators = encoding.Operators.Where(x => !(x is IMultiObjectiveOperator) || x is ISingleObjectiveOperator).ToList(); 107 //} 103 108 104 109 protected override void OnEvaluatorChanged() { … … 108 113 109 114 private void ParameterizeOperators() { 110 foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator >())115 foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator<TSolution>>()) 111 116 op.EvaluateFunc = Evaluate; 112 foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator >())117 foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator<TSolution>>()) 113 118 op.AnalyzeAction = Analyze; 114 foreach (var op in Operators.OfType<INeighborBasedOperator >())119 foreach (var op in Operators.OfType<INeighborBasedOperator<TSolution>>()) 115 120 op.GetNeighborsFunc = GetNeighbors; 116 121 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/HeuristicLab.Optimization-3.3.csproj
r12975 r13336 121 121 <ItemGroup> 122 122 <Compile Include="Algorithms\BasicAlgorithm.cs" /> 123 <Compile Include="BasicProblems\BasicProblem.cs" /> 123 <Compile Include="BasicProblems\Interfaces\ISingleObjectiveProblem.cs" /> 124 <Compile Include="BasicProblems\Problem.cs" /> 124 125 <Compile Include="BasicProblems\Encoding.cs" /> 125 <Compile Include="BasicProblems\Individuals\Individual.cs" />126 <Compile Include="BasicProblems\Individuals\MultiEncodingIndividual.cs" />127 <Compile Include="BasicProblems\Individuals\SingleEncodingIndividual.cs" />128 126 <Compile Include="BasicProblems\Interfaces\IEncoding.cs" /> 129 127 <Compile Include="BasicProblems\Interfaces\IEncodingOperator.cs" /> … … 138 136 <Compile Include="BasicProblems\Interfaces\ISingleObjectiveMoveOperator.cs" /> 139 137 <Compile Include="BasicProblems\Interfaces\ISingleObjectiveProblemDefinition.cs" /> 140 <Compile Include="BasicProblems\MultiEncoding.cs" /> 141 <Compile Include="BasicProblems\MultiObjectiveBasicProblem.cs" /> 142 <Compile Include="BasicProblems\Operators\MultiEncodingCreator.cs" /> 143 <Compile Include="BasicProblems\Operators\MultiEncodingCrossover.cs" /> 144 <Compile Include="BasicProblems\Operators\MultiEncodingManipulator.cs" /> 145 <Compile Include="BasicProblems\Operators\MultiEncodingOperator.cs" /> 138 <Compile Include="BasicProblems\Interfaces\ISolution.cs" /> 139 <Compile Include="BasicProblems\MultiObjectiveProblem.cs" /> 146 140 <Compile Include="BasicProblems\Operators\MultiObjectiveAnalyzer.cs" /> 147 141 <Compile Include="BasicProblems\Operators\MultiObjectiveEvaluator.cs" /> 142 <Compile Include="BasicProblems\Operators\ScopeUtil.cs" /> 148 143 <Compile Include="BasicProblems\Operators\SingleObjectiveAnalyzer.cs" /> 149 144 <Compile Include="BasicProblems\Operators\SingleObjectiveEvaluator.cs" /> … … 152 147 <Compile Include="BasicProblems\Operators\SingleObjectiveMoveGenerator.cs" /> 153 148 <Compile Include="BasicProblems\Operators\SingleObjectiveMoveMaker.cs" /> 154 <Compile Include="BasicProblems\SingleObjective BasicProblem.cs" />149 <Compile Include="BasicProblems\SingleObjectiveProblem.cs" /> 155 150 <Compile Include="Interfaces\ILocalImprovementAlgorithmOperator.cs" /> 156 151 <Compile Include="Interfaces\IMultiObjectiveOperator.cs" /> … … 328 323 </BootstrapperPackage> 329 324 </ItemGroup> 330 <ItemGroup /> 325 <ItemGroup> 326 <Folder Include="BasicProblems\Individuals\" /> 327 </ItemGroup> 331 328 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 332 329 <!-- To modify your build process, add your task inside one of the targets below and uncomment it. -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/Interfaces/IProblem.cs
r12012 r13336 31 31 IEnumerable<IItem> Operators { get; } 32 32 33 34 33 IEnumerable<IParameterizedItem> ExecutionContextItems { get; } 35 34 event EventHandler OperatorsChanged; 36 35 event EventHandler Reset; 37 36 } 37 38 public interface IProblem<TSolution> : IHeuristicOptimizationProblem where TSolution : class, ISolution { 39 IEncoding<TSolution> Encoding { get; } 40 } 38 41 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/Interfaces/ISolutionCreator.cs
r12012 r13336 27 27 /// </summary> 28 28 public interface ISolutionCreator : IOperator { } 29 30 public interface ISolutionCreator<TSolution> : ISolutionCreator where TSolution : class, ISolution { } 29 31 } -
branches/ProblemRefactoring/HeuristicLab.Problems.Binary/3.3/BinaryProblem.cs
r12000 r13336 34 34 namespace HeuristicLab.Problems.Binary { 35 35 [StorableClass] 36 public abstract class BinaryProblem : SingleObjective BasicProblem<BinaryVectorEncoding> {36 public abstract class BinaryProblem : SingleObjectiveProblem<BinaryVector> { 37 37 public virtual int Length { 38 38 get { return Encoding.Length; } 39 39 set { Encoding.Length = value; } 40 } 41 42 public new BinaryVectorEncoding Encoding { 43 get { return (BinaryVectorEncoding)base.Encoding; } 44 set { base.Encoding = value; } 40 45 } 41 46 … … 60 65 var lengthParameter = new FixedValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(10)); 61 66 Parameters.Add(lengthParameter); 67 Encoding = new BinaryVectorEncoding(); 62 68 Encoding.LengthParameter = lengthParameter; 63 69 RegisterEventHandlers(); 64 70 } 65 71 66 public virtual bool IsBetter(double quality, double bestQuality) { 67 return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality); 68 } 69 70 public abstract double Evaluate(BinaryVector vector, IRandom random); 71 public sealed override double Evaluate(Individual individual, IRandom random) { 72 return Evaluate(individual.BinaryVector(), random); 73 } 74 75 public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { 72 public override void Analyze(BinaryVector[] individuals, double[] qualities, ResultCollection results, IRandom random) { 76 73 base.Analyze(individuals, qualities, results, random); 77 74 var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality); … … 81 78 results.Add(new Result("Best Solution", typeof(BinaryVector))); 82 79 } 83 results["Best Solution"].Value = (IItem)best. BinaryVector().Clone();80 results["Best Solution"].Value = (IItem)best.Clone(); 84 81 } 85 82 -
branches/ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/ExternalEvaluationProblem.cs
r13257 r13336 40 40 // BackwardsCompatibility3.3 41 41 // Rename class to SingleObjectiveExternalEvaluationProblem 42 public class ExternalEvaluationProblem : SingleObjective BasicProblem<IEncoding>, IExternalEvaluationProblem {42 public class ExternalEvaluationProblem : SingleObjectiveProblem<IEncoding>, IExternalEvaluationProblem { 43 43 44 44 public static new Image StaticItemImage { -
branches/ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/MultiObjectiveExternalEvaluationProblem.cs
r13257 r13336 37 37 [Creatable(CreatableAttribute.Categories.ExternalEvaluationProblems, Priority = 200)] 38 38 [StorableClass] 39 public class MultiObjectiveExternalEvaluationProblem : MultiObjective BasicProblem<IEncoding>, IExternalEvaluationProblem {39 public class MultiObjectiveExternalEvaluationProblem : MultiObjectiveProblem<IEncoding>, IExternalEvaluationProblem { 40 40 41 41 public static new Image StaticItemImage { -
branches/ProblemRefactoring/HeuristicLab.Problems.GrammaticalEvolution/3.4/ArtificialAnt/GEArtificialAntProblem.cs
r13243 r13336 38 38 [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 170)] 39 39 [StorableClass] 40 public sealed class GEArtificialAntProblem : SingleObjective BasicProblem<IntegerVectorEncoding>, IStorableContent {40 public sealed class GEArtificialAntProblem : SingleObjectiveProblem<IntegerVectorEncoding>, IStorableContent { 41 41 public string Filename { get; set; } 42 42 -
branches/ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs
r12616 r13336 33 33 [Creatable(CreatableAttribute.Categories.Problems, Priority = 120)] 34 34 [StorableClass] 35 public sealed class MultiObjectiveProgrammableProblem : MultiObjective BasicProblem<IEncoding>, IProgrammableItem {35 public sealed class MultiObjectiveProgrammableProblem : MultiObjectiveProblem<IEncoding>, IProgrammableItem { 36 36 public static new Image StaticItemImage { 37 37 get { return VSImageLibrary.Script; } -
branches/ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs
r12616 r13336 35 35 [Creatable(CreatableAttribute.Categories.Problems, Priority = 100)] 36 36 [StorableClass] 37 public sealed class SingleObjectiveProgrammableProblem : SingleObjective BasicProblem<IEncoding>, IProgrammableItem {37 public sealed class SingleObjectiveProgrammableProblem : SingleObjectiveProblem<IEncoding>, IProgrammableItem { 38 38 public static new Image StaticItemImage { 39 39 get { return VSImageLibrary.Script; }
Note: See TracChangeset
for help on using the changeset viewer.