Changeset 4830 for branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/MetaOptimizationProblem.cs
- Timestamp:
- 11/18/10 00:53:15 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/MetaOptimizationProblem.cs
r4525 r4830 36 36 [Creatable("Problems")] 37 37 [StorableClass] 38 public sealed class MetaOptimizationProblem : ParameterizedNamedItem, ISingleObjectiveProblem, IStorableContent { 39 public string Filename { get; set; } 40 41 public override Image ItemImage { 42 get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; } 43 } 38 public sealed class MetaOptimizationProblem : SingleObjectiveProblem<IMetaOptimizationEvaluator, IParameterConfigurationCreator> { 39 private const string AlgorithmParameterName = "Algorithm"; 40 private const string ProblemsParameterName = "Problems"; 41 private const string AlgorithmParameterConfigurationParameterName = "AlgorithmParameterConfiguration"; 42 private const string ProblemParametersConfigurationParameterName = "ProblemParametersConfiguration"; 44 43 45 44 #region Parameter Properties 46 47 public ValueParameter<IntValue> RepetitionsParameter { 48 get { return (ValueParameter<IntValue>)Parameters["Repetitions"]; } 45 public ValueParameter<IAlgorithm> AlgorithmParameter { 46 get { return (ValueParameter<IAlgorithm>)Parameters[AlgorithmParameterName]; } 49 47 } 50 51 public ValueParameter<IParameterSetCreator> SolutionCreatorParameter { 52 get { return (ValueParameter<IParameterSetCreator>)Parameters["SolutionCreator"]; } 48 public ValueParameter<IItemList<ISingleObjectiveProblem>> ProblemsParameter { 49 get { return (ValueParameter<IItemList<ISingleObjectiveProblem>>)Parameters[ProblemsParameterName]; } 53 50 } 54 IParameter IProblem.SolutionCreatorParameter {55 get { return SolutionCreatorParameter; }51 public ValueParameter<IParameterConfiguration> AlgorithmParameterConfigurationParameter { 52 get { return (ValueParameter<IParameterConfiguration>)Parameters[AlgorithmParameterConfigurationParameterName]; } 56 53 } 57 58 public ValueParameter<IMetaOptimizationEvaluator> EvaluatorParameter { 59 get { return (ValueParameter<IMetaOptimizationEvaluator>)Parameters["Evaluator"]; } 60 } 61 IParameter IProblem.EvaluatorParameter { 62 get { return EvaluatorParameter; } 63 } 64 65 public OptionalValueParameter<ParameterSet> BestKnownSolutionParameter { 66 get { return (OptionalValueParameter<ParameterSet>)Parameters["BestKnownSolution"]; } 67 } 68 69 public ValueParameter<BoolValue> MaximizationParameter { 70 get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; } 71 } 72 IParameter ISingleObjectiveProblem.MaximizationParameter { 73 get { return MaximizationParameter; } 74 } 75 76 public OptionalValueParameter<DoubleValue> BestKnownQualityParameter { 77 get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; } 78 } 79 IParameter ISingleObjectiveProblem.BestKnownQualityParameter { 80 get { return BestKnownQualityParameter; } 81 } 82 83 public ValueParameter<IAlgorithm> AlgorithmParameter { 84 get { return (ValueParameter<IAlgorithm>)Parameters["Algorithm"]; } 85 } 86 public ValueParameter<ParameterConfigurationList> ParametersToOptimizeParameter { 87 get { return (ValueParameter<ParameterConfigurationList>)Parameters["ParametersToOptimize"]; } 88 } 54 //public ValueParameter<IItemList<IParameterConfiguration>> ProblemParametersConfigurationParameter { 55 // get { return (ValueParameter<IItemList<IParameterConfiguration>>)Parameters[ProblemParametersConfigurationParameterName]; } 56 //} 89 57 #endregion 90 58 91 59 #region Properties 92 93 public IntValue Repetitions {94 get { return RepetitionsParameter.Value; }95 set { RepetitionsParameter.Value = value; }96 }97 98 public IEnumerable<IOperator> Operators {99 get { return operators; }100 }101 102 IEvaluator IProblem.Evaluator {103 get { return EvaluatorParameter.Value; }104 }105 public IMetaOptimizationEvaluator Evaluator {106 get { return EvaluatorParameter.Value; }107 set { EvaluatorParameter.Value = value; }108 }109 110 ISolutionCreator IProblem.SolutionCreator {111 get { return SolutionCreatorParameter.Value; }112 }113 public IParameterSetCreator SolutionCreator {114 get { return SolutionCreatorParameter.Value; }115 }116 117 ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {118 get { return this.Evaluator; }119 }120 121 public DoubleValue BestKnownQuality {122 get { return BestKnownQualityParameter.Value; }123 set { BestKnownQualityParameter.Value = value; }124 }125 public ParameterSet BestKnownSolution {126 get { return BestKnownSolutionParameter.Value; }127 set { BestKnownSolutionParameter.Value = value; }128 }129 130 60 public IAlgorithm Algorithm { 131 61 get { return AlgorithmParameter.Value; } 132 62 set { AlgorithmParameter.Value = value; } 133 63 } 134 135 public ParameterConfigurationList ParametersToOptimize { 136 get { return ParametersToOptimizeParameter.Value; } 137 set { ParametersToOptimizeParameter.Value = value; } 64 public IItemList<ISingleObjectiveProblem> Problems { 65 get { return ProblemsParameter.Value; } 66 set { ProblemsParameter.Value = value; } 138 67 } 139 140 private BestQualityAnalyzer BestQualityAnalyzer {141 get { return operators.OfType<BestQualityAnalyzer>().FirstOrDefault(); }68 public IParameterConfiguration AlgorithmParameterConfiguration { 69 get { return AlgorithmParameterConfigurationParameter.Value; } 70 set { AlgorithmParameterConfigurationParameter.Value = value; } 142 71 } 72 //public IItemList<IParameterConfiguration> ProblemParametersConfiguration { 73 // get { return ProblemParametersConfigurationParameter.Value; } 74 // set { ProblemParametersConfigurationParameter.Value = value; } 75 //} 143 76 #endregion 144 77 145 [Storable] 146 private List<IOperator> operators; 78 public MetaOptimizationProblem() 79 : base() { 80 Parameters.Add(new ValueParameter<IAlgorithm>(AlgorithmParameterName, "The algorithm which's parameters should be optimized.")); 81 Parameters.Add(new ValueParameter<IItemList<IProblem>>(ProblemsParameterName, "The problems that should be evaluated.", new ItemList<IProblem>())); 82 Parameters.Add(new ValueParameter<IParameterConfiguration>(AlgorithmParameterConfigurationParameterName, "List of algorithm parameters that should be optimized.")); 83 //Parameters.Add(new ValueParameter<IItemList<IParameterConfiguration>>(ProblemParametersConfigurationParameterName, "List of problem parameters that should be optimized.", new ItemList<IParameterConfiguration>())); 84 85 Maximization = new BoolValue(false); 86 SolutionCreator = new RandomParameterConfigurationCreator(); 87 Evaluator = new MetaOptimizationEvaluator(); 88 89 InitializeOperators(); 90 RegisterParameterEvents(); 91 ParameterizeSolutionCreator(); 92 ParameterizeEvaluator(); 93 ParameterizeOperators(); 94 } 147 95 148 96 [StorableConstructor] 149 97 private MetaOptimizationProblem(bool deserializing) : base(deserializing) { } 150 public MetaOptimizationProblem() 151 : base() { 152 153 IParameterSetCreator creator = new RandomParameterSetCreator(); 154 MetaOptimizationEvaluator evaluator = new MetaOptimizationEvaluator(); 155 ParameterConfigurationList parametersToOptimize = new ParameterConfigurationList(); 156 157 Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Traveling Salesman Problem is a minimization problem.", new BoolValue(false))); 158 Parameters.Add(new ValueParameter<IntValue>("Repetitions", "Number of evaluations for one individual.", new IntValue(3))); 159 Parameters.Add(new ValueParameter<IParameterSetCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator)); 160 Parameters.Add(new ValueParameter<IMetaOptimizationEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator)); 161 Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this TSP instance.")); 162 Parameters.Add(new OptionalValueParameter<ParameterSet>("BestKnownSolution", "The best known solution of this TSP instance.")); 163 164 Parameters.Add(new ValueParameter<IAlgorithm>("Algorithm", "The algorithm and problem which's parameters should be optimized.")); 165 Parameters.Add(new ValueParameter<ParameterConfigurationList>("ParametersToOptimize", "List of parameters that should be optimized.", parametersToOptimize)); 166 167 ParameterizeSolutionCreator(); 168 ParameterizeEvaluator(); 169 170 InitializeOperators(); 171 AttachEventHandlers(); 98 private MetaOptimizationProblem(MetaOptimizationProblem original, Cloner cloner) : base(original, cloner) { 99 // todo 100 this.RegisterParameterEvents(); 172 101 } 173 174 #region Cloning175 102 public override IDeepCloneable Clone(Cloner cloner) { 176 MetaOptimizationProblem clone = (MetaOptimizationProblem)base.Clone(cloner); 177 clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList(); 178 // todo 179 clone.AttachEventHandlers(); 180 return clone; 103 return new MetaOptimizationProblem(this, cloner); 181 104 } 182 #endregion183 105 184 106 #region Helpers 185 107 [StorableHook(HookType.AfterDeserialization)] 186 108 private void AfterDeserializationHook() { 187 AttachEventHandlers();109 RegisterParameterEvents(); 188 110 } 189 private void AttachEventHandlers() {111 private void RegisterParameterEvents() { 190 112 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged); 191 113 EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged); … … 194 116 } 195 117 private void InitializeOperators() { 196 operators = new List<IOperator>(); 197 operators.Add(new BestQualityAnalyzer()); 198 ParameterizeAnalyzer(); 199 operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterSetOperator>().Cast<IOperator>()); 200 ParameterizeOperators(); 201 202 //UpdateMoveEvaluators(); 203 //InitializeMoveGenerators(); 118 Operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterConfigurationOperator>().Cast<IOperator>()); 119 Operators.Add(new BestParameterConfigurationAnalyzer()); 204 120 } 205 121 private void ParameterizeSolutionCreator() { 206 SolutionCreator.ParametersToOptimize = this.ParametersToOptimize;122 //SolutionCreator.ParametersToOptimize = this.ParametersToOptimize; 207 123 } 208 124 private void ParameterizeEvaluator() { 125 ((MetaOptimizationEvaluator)Evaluator).ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName; 209 126 } 210 127 private void ParameterizeAnalyzer() { 211 BestQualityAnalyzer.ResultsParameter.ActualName = "Results";128 //BestQualityAnalyzer.ResultsParameter.ActualName = "Results"; 212 129 } 213 130 private void ParameterizeOperators() { 131 214 132 } 215 133 216 private void AddAlgorithmParameters() {217 foreach (IParameter parameter in Algorithm.Parameters) {218 this.ParametersToOptimize.Add(new NumericParameterConfiguration(parameter, "Algorithm"), false);219 }220 }221 private void RemoveAlgorithmParameters() {222 foreach (IParameter parameter in Algorithm.Parameters) {223 IParameterConfiguration parameterConfiguration = this.ParametersToOptimize.Single(p => p.Parameter == parameter);224 if (parameterConfiguration != null) {225 this.ParametersToOptimize.Remove(parameterConfiguration);226 }227 }228 }229 private void ClearAlgorithmParameters() {230 //this.ParametersToOptimize.Clear();231 }232 233 private void AddProblemParameters() {234 foreach (IParameter parameter in Algorithm.Problem.Parameters) {235 this.ParametersToOptimize.Add(new NumericParameterConfiguration(parameter, "Problem"), false);236 }237 }238 private void RemoveProblemParameters() {239 foreach (IParameter parameter in Algorithm.Problem.Parameters) {240 IParameterConfiguration parameterConfiguration = this.ParametersToOptimize.Single(p => p.Parameter == parameter);241 if (parameterConfiguration != null) {242 this.ParametersToOptimize.Remove(parameterConfiguration);243 }244 }245 }246 private void ClearProblemParameters() {247 //this.ParametersToOptimize.Clear();248 }249 134 #endregion 250 135 251 136 #region Events 252 public event EventHandler SolutionCreatorChanged;253 private void OnSolutionCreatorChanged() {254 EventHandler handler = SolutionCreatorChanged;255 if (handler != null) handler(this, EventArgs.Empty);256 }257 public event EventHandler EvaluatorChanged;258 private void OnEvaluatorChanged() {259 EventHandler handler = EvaluatorChanged;260 if (handler != null) handler(this, EventArgs.Empty);261 }262 public event EventHandler OperatorsChanged;263 private void OnOperatorsChanged() {264 EventHandler handler = OperatorsChanged;265 if (handler != null) handler(this, EventArgs.Empty);266 }267 public event EventHandler Reset;268 private void OnReset() {269 EventHandler handler = Reset;270 if (handler != null) handler(this, EventArgs.Empty);271 }272 137 273 138 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) { … … 288 153 } 289 154 void BaseLevelAlgorithmParameter_ValueChanged(object sender, EventArgs e) { 290 ClearAlgorithmParameters();291 155 if (Algorithm != null) { 292 156 Algorithm.ProblemChanged += new EventHandler(BaseLevelAlgorithm_ProblemChanged); 293 A ddAlgorithmParameters(); // TODO: When to Detach?157 AlgorithmParameterConfiguration = ParameterConfiguration.Create(Algorithm); 294 158 } 295 159 BaseLevelAlgorithm_ProblemChanged(sender, e); … … 297 161 298 162 void BaseLevelAlgorithm_ProblemChanged(object sender, EventArgs e) { 299 ClearProblemParameters();300 if (Algorithm.Problem != null) {301 AddProblemParameters();302 }163 //ClearProblemParameters(); 164 //if (Algorithm.Problem != null) { 165 // AddProblemParameters(); 166 //} 303 167 } 304 168 #endregion
Note: See TracChangeset
for help on using the changeset viewer.