Changeset 9127 for trunk/sources/HeuristicLab.Algorithms.GradientDescent
- Timestamp:
- 01/08/13 16:55:20 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.GradientDescent/3.3/Lbfgs.cs
r8401 r9127 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Data; 27 using HeuristicLab.Encodings.RealVectorEncoding; 27 28 using HeuristicLab.Operators; 28 29 using HeuristicLab.Optimization; … … 41 42 public sealed class LbfgsAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent { 42 43 public override Type ProblemType { 43 get { return typeof( SingleObjectiveTestFunctionProblem); }44 } 45 46 public new SingleObjectiveTestFunctionProblem Problem {47 get { return ( SingleObjectiveTestFunctionProblem)base.Problem; }44 get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); } 45 } 46 47 public new ISingleObjectiveHeuristicOptimizationProblem Problem { 48 get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; } 48 49 set { base.Problem = value; } 49 50 } … … 76 77 #endregion 77 78 79 [Storable] 80 private LbfgsInitializer initializer; 81 [Storable] 82 private LbfgsMakeStep makeStep; 83 [Storable] 84 private LbfgsUpdateResults updateResults; 85 [Storable] 86 private LbfgsAnalyzer analyzer; 87 [Storable] 88 private LbfgsAnalyzer finalAnalyzer; 89 [Storable] 90 private Placeholder solutionCreator; 91 [Storable] 92 private Placeholder evaluator; 93 78 94 [StorableConstructor] 79 95 private LbfgsAlgorithm(bool deserializing) : base(deserializing) { } 80 96 private LbfgsAlgorithm(LbfgsAlgorithm original, Cloner cloner) 81 97 : base(original, cloner) { 98 initializer = cloner.Clone(original.initializer); 99 makeStep = cloner.Clone(original.makeStep); 100 updateResults = cloner.Clone(original.updateResults); 101 analyzer = cloner.Clone(original.analyzer); 102 finalAnalyzer = cloner.Clone(original.finalAnalyzer); 103 solutionCreator = cloner.Clone(original.solutionCreator); 104 evaluator = cloner.Clone(original.evaluator); 105 RegisterEvents(); 82 106 } 83 107 public LbfgsAlgorithm() … … 85 109 this.name = ItemName; 86 110 this.description = ItemDescription; 87 88 Problem = new SingleObjectiveTestFunctionProblem();89 111 90 112 Parameters.Add(new ValueParameter<IntValue>(MaxIterationsParameterName, "The maximal number of iterations for.", new IntValue(20))); … … 95 117 96 118 var randomCreator = new RandomCreator(); 97 varsolutionCreator = new Placeholder();98 var bfgsInitializer = new LbfgsInitializer();99 varmakeStep = new LbfgsMakeStep();119 solutionCreator = new Placeholder(); 120 initializer = new LbfgsInitializer(); 121 makeStep = new LbfgsMakeStep(); 100 122 var branch = new ConditionalBranch(); 101 varevaluator = new Placeholder();102 varupdateResults = new LbfgsUpdateResults();103 varanalyzer = new LbfgsAnalyzer();104 varfinalAnalyzer = new LbfgsAnalyzer();123 evaluator = new Placeholder(); 124 updateResults = new LbfgsUpdateResults(); 125 analyzer = new LbfgsAnalyzer(); 126 finalAnalyzer = new LbfgsAnalyzer(); 105 127 106 128 OperatorGraph.InitialOperator = randomCreator; … … 112 134 randomCreator.Successor = solutionCreator; 113 135 114 solutionCreator.OperatorParameter.ActualName = Problem.SolutionCreatorParameter.Name; 115 solutionCreator.Successor = bfgsInitializer; 116 117 bfgsInitializer.IterationsParameter.ActualName = MaxIterationsParameterName; 118 bfgsInitializer.PointParameter.ActualName = Problem.SolutionCreator.RealVectorParameter.ActualName; 119 bfgsInitializer.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName; 120 bfgsInitializer.Successor = makeStep; 121 122 makeStep.StateParameter.ActualName = bfgsInitializer.StateParameter.Name; 123 makeStep.PointParameter.ActualName = bfgsInitializer.PointParameter.ActualName; 136 solutionCreator.Name = "Solution Creator (placeholder)"; 137 solutionCreator.Successor = initializer; 138 139 initializer.IterationsParameter.ActualName = MaxIterationsParameterName; 140 initializer.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName; 141 initializer.Successor = makeStep; 142 143 makeStep.StateParameter.ActualName = initializer.StateParameter.Name; 124 144 makeStep.Successor = branch; 125 145 … … 128 148 branch.TrueBranch = finalAnalyzer; 129 149 130 evaluator. OperatorParameter.ActualName = Problem.EvaluatorParameter.Name;150 evaluator.Name = "Evaluator (placeholder)"; 131 151 evaluator.Successor = updateResults; 132 152 133 updateResults.StateParameter.ActualName = bfgsInitializer.StateParameter.Name; 134 updateResults.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.Name; 153 updateResults.StateParameter.ActualName = initializer.StateParameter.Name; 135 154 updateResults.ApproximateGradientsParameter.ActualName = ApproximateGradientsParameterName; 136 155 updateResults.Successor = analyzer; 137 156 138 analyzer.QualityParameter.ActualName = updateResults.QualityParameter.ActualName; 139 analyzer.PointParameter.ActualName = makeStep.PointParameter.ActualName; 140 analyzer.StateParameter.ActualName = bfgsInitializer.StateParameter.Name; 157 analyzer.StateParameter.ActualName = initializer.StateParameter.Name; 141 158 analyzer.Successor = makeStep; 142 159 143 finalAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.Name;144 finalAnalyzer.PointParameter.ActualName = makeStep.PointParameter.ActualName;145 160 finalAnalyzer.PointsTableParameter.ActualName = analyzer.PointsTableParameter.ActualName; 146 161 finalAnalyzer.QualityGradientsTableParameter.ActualName = analyzer.QualityGradientsTableParameter.ActualName; … … 149 164 150 165 [StorableHook(HookType.AfterDeserialization)] 151 private void AfterDeserialization() { } 166 private void AfterDeserialization() { 167 RegisterEvents(); 168 } 152 169 153 170 public override IDeepCloneable Clone(Cloner cloner) { 154 171 return new LbfgsAlgorithm(this, cloner); 155 172 } 173 174 #region events 175 private void RegisterEvents() { 176 if (Problem != null) { 177 RegisterSolutionCreatorEvents(); 178 RegisterEvaluatorEvents(); 179 } 180 } 181 182 protected override void OnProblemChanged() { 183 base.OnProblemChanged(); 184 if (Problem != null) { 185 RegisterEvents(); 186 solutionCreator.OperatorParameter.ActualName = Problem.SolutionCreatorParameter.Name; 187 evaluator.OperatorParameter.ActualName = Problem.EvaluatorParameter.Name; 188 } 189 } 190 191 protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) { 192 base.Problem_SolutionCreatorChanged(sender, e); 193 RegisterSolutionCreatorEvents(); 194 ParameterizeOperators(); 195 } 196 197 protected override void Problem_EvaluatorChanged(object sender, EventArgs e) { 198 base.Problem_EvaluatorChanged(sender, e); 199 RegisterEvaluatorEvents(); 200 ParameterizeOperators(); 201 } 202 203 private void RegisterSolutionCreatorEvents() { 204 var realVectorCreator = Problem.SolutionCreator as RealVectorCreator; 205 // ignore if we have a different kind of problem 206 if (realVectorCreator != null) { 207 realVectorCreator.RealVectorParameter.ActualNameChanged += (sender, args) => ParameterizeOperators(); 208 } 209 } 210 211 private void RegisterEvaluatorEvents() { 212 Problem.Evaluator.QualityParameter.ActualNameChanged += (sender, args) => ParameterizeOperators(); 213 } 214 #endregion 215 216 protected override void OnStarted() { 217 var realVectorCreator = Problem.SolutionCreator as RealVectorCreator; 218 // must catch the case that user loaded an unsupported problem 219 if (realVectorCreator == null) 220 throw new InvalidOperationException("LM-BFGS only works with problems using a real-value encoding."); 221 base.OnStarted(); 222 } 223 224 public override void Prepare() { 225 if (Problem != null) base.Prepare(); 226 } 227 228 private void ParameterizeOperators() { 229 var realVectorCreator = Problem.SolutionCreator as RealVectorCreator; 230 // ignore if we have a different kind of problem 231 if (realVectorCreator != null) { 232 var realVectorParameterName = realVectorCreator.RealVectorParameter.ActualName; 233 initializer.PointParameter.ActualName = realVectorParameterName; 234 makeStep.PointParameter.ActualName = realVectorParameterName; 235 analyzer.PointParameter.ActualName = realVectorParameterName; 236 finalAnalyzer.PointParameter.ActualName = realVectorParameterName; 237 } 238 239 var qualityParameterName = Problem.Evaluator.QualityParameter.ActualName; 240 updateResults.QualityParameter.ActualName = qualityParameterName; 241 analyzer.QualityParameter.ActualName = qualityParameterName; 242 finalAnalyzer.QualityParameter.ActualName = qualityParameterName; 243 } 156 244 } 157 245 }
Note: See TracChangeset
for help on using the changeset viewer.