- Timestamp:
- 12/03/16 00:32:09 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/MemPRContext.cs
r14420 r14450 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HeuristicLab.Algorithms.MemPR.Interfaces; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 28 using HeuristicLab.Data; 28 29 using HeuristicLab.Optimization; 29 using HeuristicLab.Optimization.SolutionModel;30 30 using HeuristicLab.Parameters; 31 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 35 35 [Item("MemPRContext", "Abstract base class for MemPR contexts.")] 36 36 [StorableClass] 37 public abstract class MemPR Context<TSolution, TContext, TSolutionContext> : ParameterizedNamedItem,38 ISingleObjectivePopulationContext<TSolution>, ISolutionModelContext<TSolution>, IStochasticContext,39 IMaximizationContext, IEvaluatedSolutionsContext37 public abstract class MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext> : ParameterizedNamedItem, 38 IPopulationBasedHeuristicAlgorithmContext<TProblem, TSolution>, ISolutionModelContext<TSolution> 39 where TProblem : class, IItem, ISingleObjectiveProblemDefinition 40 40 where TSolution : class, IItem 41 where T Context : MemPRContext<TSolution, TContext, TSolutionContext>42 where TSolutionContext : SingleSolutionMemPRContext<TSolution, TContext, TSolutionContext> {41 where TPopulationContext : MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext> 42 where TSolutionContext : MemPRSolutionContext<TProblem, TSolution, TPopulationContext, TSolutionContext> { 43 43 44 44 private IExecutionContext parent; … … 60 60 61 61 [Storable] 62 private IValueParameter< BoolValue> maximization;63 public bool Maximization{64 get { return maximization.Value.Value; }65 set { maximization.Value.Value = value; }62 private IValueParameter<TProblem> problem; 63 public TProblem Problem { 64 get { return problem.Value; } 65 set { problem.Value = value; } 66 66 } 67 67 … … 95 95 96 96 [Storable] 97 private IValueParameter<TSolution> bestSolution; 98 public TSolution BestSolution { 99 get { return bestSolution.Value; } 100 set { bestSolution.Value = value; } 101 } 102 103 [Storable] 97 104 private IValueParameter<IntValue> hcSteps; 98 105 public int HcSteps { … … 142 149 set { random.Value = value; } 143 150 } 144 145 IEnumerable<IScope> IPopulationContext.Population {146 get { return scope.SubScopes; }147 }148 151 149 152 public IEnumerable<ISingleObjectiveSolutionScope<TSolution>> Population { … … 183 186 184 187 [StorableConstructor] 185 protected MemPR Context(bool deserializing) : base(deserializing) { }186 protected MemPR Context(MemPRContext<TSolution, TContext, TSolutionContext> original, Cloner cloner)188 protected MemPRPopulationContext(bool deserializing) : base(deserializing) { } 189 protected MemPRPopulationContext(MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext> original, Cloner cloner) 187 190 : base(original, cloner) { 188 191 scope = cloner.Clone(original.scope); 189 maximization = cloner.Clone(original.maximization);192 problem = cloner.Clone(original.problem); 190 193 initialized = cloner.Clone(original.initialized); 191 194 iterations = cloner.Clone(original.iterations); 192 195 evaluatedSolutions = cloner.Clone(original.evaluatedSolutions); 193 196 bestQuality = cloner.Clone(original.bestQuality); 197 bestSolution = cloner.Clone(original.bestSolution); 194 198 hcSteps = cloner.Clone(original.hcSteps); 195 199 byBreeding = cloner.Clone(original.byBreeding); … … 205 209 Model = cloner.Clone(original.Model); 206 210 } 207 public MemPR Context() : this("MemPRContext") { }208 public MemPR Context(string name) : base(name) {211 public MemPRPopulationContext() : this("MemPRContext") { } 212 public MemPRPopulationContext(string name) : base(name) { 209 213 scope = new Scope("Global"); 210 214 211 Parameters.Add( maximization = new ValueParameter<BoolValue>("Maximization", new BoolValue(false)));215 Parameters.Add(problem = new ValueParameter<TProblem>("Problem")); 212 216 Parameters.Add(initialized = new ValueParameter<BoolValue>("Initialized", new BoolValue(false))); 213 217 Parameters.Add(iterations = new ValueParameter<IntValue>("Iterations", new IntValue(0))); 214 218 Parameters.Add(evaluatedSolutions = new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0))); 215 219 Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(double.NaN))); 220 Parameters.Add(bestSolution = new ValueParameter<TSolution>("BestSolution")); 216 221 Parameters.Add(hcSteps = new ValueParameter<IntValue>("HcSteps", new IntValue(0))); 217 222 Parameters.Add(byBreeding = new ValueParameter<IntValue>("ByBreeding", new IntValue(0))); … … 229 234 public abstract TSolutionContext CreateSingleSolutionContext(ISingleObjectiveSolutionScope<TSolution> solution); 230 235 236 public void IncrementEvaluatedSolutions(int byEvaluations) { 237 if (byEvaluations < 0) throw new ArgumentException("Can only increment and not decrement evaluated solutions."); 238 EvaluatedSolutions += byEvaluations; 239 } 240 231 241 #region IExecutionContext members 232 242 public IAtomicOperation CreateOperation(IOperator op) { … … 246 256 } 247 257 #endregion 248 249 IEnumerable<ISolutionScope<TSolution>> IPopulationContext<TSolution>.Population {250 get { return Population; }251 }252 258 } 253 259 254 260 [Item("SingleSolutionMemPRContext", "Abstract base class for single solution MemPR contexts.")] 255 261 [StorableClass] 256 public abstract class SingleSolutionMemPRContext<TSolution, TContext, TSolutionContext> : ParameterizedNamedItem,257 ISingleObjectiveSolutionContext<TSolution>, IEvaluatedSolutionsContext,258 IIterationsManipulationContext, IStochasticContext, IMaximizationContext262 public abstract class MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> : ParameterizedNamedItem, 263 ISingleSolutionHeuristicAlgorithmContext<TProblem, TSolution> 264 where TProblem : class, IItem, ISingleObjectiveProblemDefinition 259 265 where TSolution : class, IItem 260 where TContext : MemPR Context<TSolution, TContext, TSolutionContext>261 where TSolutionContext : SingleSolutionMemPRContext<TSolution, TContext, TSolutionContext> {266 where TContext : MemPRPopulationContext<TProblem, TSolution, TContext, TSolutionContext> 267 where TSolutionContext : MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> { 262 268 263 269 private TContext parent; … … 276 282 get { return Parameters; } 277 283 } 278 279 public bool Maximization { 280 get { return parent.Maximization; } 284 285 public TProblem Problem { 286 get { return parent.Problem; } 287 } 288 289 public double BestQuality { 290 get { return parent.BestQuality; } 291 set { parent.BestQuality = value; } 292 } 293 294 public TSolution BestSolution { 295 get { return parent.BestSolution; } 296 set { parent.BestSolution = value; } 281 297 } 282 298 … … 299 315 } 300 316 301 IS cope ISolutionContext.Solution {317 ISingleObjectiveSolutionScope<TSolution> ISingleSolutionHeuristicAlgorithmContext<TProblem, TSolution>.Solution { 302 318 get { return scope; } 303 319 } 304 320 305 ISolutionScope<TSolution> ISolutionContext<TSolution>.Solution {306 get { return scope; }307 }308 309 ISingleObjectiveSolutionScope<TSolution> ISingleObjectiveSolutionContext<TSolution>.Solution {310 get { return scope; }311 }312 313 321 [StorableConstructor] 314 protected SingleSolutionMemPRContext(bool deserializing) : base(deserializing) { }315 protected SingleSolutionMemPRContext(SingleSolutionMemPRContext<TSolution, TContext, TSolutionContext> original, Cloner cloner)322 protected MemPRSolutionContext(bool deserializing) : base(deserializing) { } 323 protected MemPRSolutionContext(MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> original, Cloner cloner) 316 324 : base(original, cloner) { 317 325 scope = cloner.Clone(original.scope); … … 319 327 iterations = cloner.Clone(original.iterations); 320 328 } 321 public SingleSolutionMemPRContext(TContext baseContext, ISingleObjectiveSolutionScope<TSolution> solution) {329 public MemPRSolutionContext(TContext baseContext, ISingleObjectiveSolutionScope<TSolution> solution) { 322 330 parent = baseContext; 323 331 scope = solution; … … 327 335 } 328 336 337 public void IncrementEvaluatedSolutions(int byEvaluations) { 338 if (byEvaluations < 0) throw new ArgumentException("Can only increment and not decrement evaluated solutions."); 339 EvaluatedSolutions += byEvaluations; 340 } 341 329 342 #region IExecutionContext members 330 343 public IAtomicOperation CreateOperation(IOperator op) {
Note: See TracChangeset
for help on using the changeset viewer.