Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/03/16 00:32:09 (8 years ago)
Author:
abeham
Message:

#2701: working on MemPR implementation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/MemPRContext.cs

    r14420 r14450  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using HeuristicLab.Algorithms.MemPR.Interfaces;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
    2728using HeuristicLab.Data;
    2829using HeuristicLab.Optimization;
    29 using HeuristicLab.Optimization.SolutionModel;
    3030using HeuristicLab.Parameters;
    3131using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3535  [Item("MemPRContext", "Abstract base class for MemPR contexts.")]
    3636  [StorableClass]
    37   public abstract class MemPRContext<TSolution, TContext, TSolutionContext> : ParameterizedNamedItem,
    38       ISingleObjectivePopulationContext<TSolution>, ISolutionModelContext<TSolution>, IStochasticContext,
    39       IMaximizationContext, IEvaluatedSolutionsContext
     37  public abstract class MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext> : ParameterizedNamedItem,
     38    IPopulationBasedHeuristicAlgorithmContext<TProblem, TSolution>, ISolutionModelContext<TSolution>
     39      where TProblem : class, IItem, ISingleObjectiveProblemDefinition
    4040      where TSolution : class, IItem
    41       where TContext : 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> {
    4343
    4444    private IExecutionContext parent;
     
    6060
    6161    [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; }
    6666    }
    6767
     
    9595
    9696    [Storable]
     97    private IValueParameter<TSolution> bestSolution;
     98    public TSolution BestSolution {
     99      get { return bestSolution.Value; }
     100      set { bestSolution.Value = value; }
     101    }
     102
     103    [Storable]
    97104    private IValueParameter<IntValue> hcSteps;
    98105    public int HcSteps {
     
    142149      set { random.Value = value; }
    143150    }
    144 
    145     IEnumerable<IScope> IPopulationContext.Population {
    146       get { return scope.SubScopes; }
    147     }
    148151   
    149152    public IEnumerable<ISingleObjectiveSolutionScope<TSolution>> Population {
     
    183186
    184187    [StorableConstructor]
    185     protected MemPRContext(bool deserializing) : base(deserializing) { }
    186     protected MemPRContext(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)
    187190      : base(original, cloner) {
    188191      scope = cloner.Clone(original.scope);
    189       maximization = cloner.Clone(original.maximization);
     192      problem = cloner.Clone(original.problem);
    190193      initialized = cloner.Clone(original.initialized);
    191194      iterations = cloner.Clone(original.iterations);
    192195      evaluatedSolutions = cloner.Clone(original.evaluatedSolutions);
    193196      bestQuality = cloner.Clone(original.bestQuality);
     197      bestSolution = cloner.Clone(original.bestSolution);
    194198      hcSteps = cloner.Clone(original.hcSteps);
    195199      byBreeding = cloner.Clone(original.byBreeding);
     
    205209      Model = cloner.Clone(original.Model);
    206210    }
    207     public MemPRContext() : this("MemPRContext") { }
    208     public MemPRContext(string name) : base(name) {
     211    public MemPRPopulationContext() : this("MemPRContext") { }
     212    public MemPRPopulationContext(string name) : base(name) {
    209213      scope = new Scope("Global");
    210214
    211       Parameters.Add(maximization = new ValueParameter<BoolValue>("Maximization", new BoolValue(false)));
     215      Parameters.Add(problem = new ValueParameter<TProblem>("Problem"));
    212216      Parameters.Add(initialized = new ValueParameter<BoolValue>("Initialized", new BoolValue(false)));
    213217      Parameters.Add(iterations = new ValueParameter<IntValue>("Iterations", new IntValue(0)));
    214218      Parameters.Add(evaluatedSolutions = new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
    215219      Parameters.Add(bestQuality = new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(double.NaN)));
     220      Parameters.Add(bestSolution = new ValueParameter<TSolution>("BestSolution"));
    216221      Parameters.Add(hcSteps = new ValueParameter<IntValue>("HcSteps", new IntValue(0)));
    217222      Parameters.Add(byBreeding = new ValueParameter<IntValue>("ByBreeding", new IntValue(0)));
     
    229234    public abstract TSolutionContext CreateSingleSolutionContext(ISingleObjectiveSolutionScope<TSolution> solution);
    230235
     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
    231241    #region IExecutionContext members
    232242    public IAtomicOperation CreateOperation(IOperator op) {
     
    246256    }
    247257    #endregion
    248 
    249     IEnumerable<ISolutionScope<TSolution>> IPopulationContext<TSolution>.Population {
    250       get { return Population; }
    251     }
    252258  }
    253259
    254260  [Item("SingleSolutionMemPRContext", "Abstract base class for single solution MemPR contexts.")]
    255261  [StorableClass]
    256   public abstract class SingleSolutionMemPRContext<TSolution, TContext, TSolutionContext> : ParameterizedNamedItem,
    257       ISingleObjectiveSolutionContext<TSolution>, IEvaluatedSolutionsContext,
    258       IIterationsManipulationContext, IStochasticContext, IMaximizationContext
     262  public abstract class MemPRSolutionContext<TProblem, TSolution, TContext, TSolutionContext> : ParameterizedNamedItem,
     263    ISingleSolutionHeuristicAlgorithmContext<TProblem, TSolution>
     264      where TProblem : class, IItem, ISingleObjectiveProblemDefinition
    259265      where TSolution : class, IItem
    260       where TContext : MemPRContext<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> {
    262268
    263269    private TContext parent;
     
    276282      get { return Parameters; }
    277283    }
    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; }
    281297    }
    282298
     
    299315    }
    300316
    301     IScope ISolutionContext.Solution {
     317    ISingleObjectiveSolutionScope<TSolution> ISingleSolutionHeuristicAlgorithmContext<TProblem, TSolution>.Solution {
    302318      get { return scope; }
    303319    }
    304320
    305     ISolutionScope<TSolution> ISolutionContext<TSolution>.Solution {
    306       get { return scope; }
    307     }
    308 
    309     ISingleObjectiveSolutionScope<TSolution> ISingleObjectiveSolutionContext<TSolution>.Solution {
    310       get { return scope; }
    311     }
    312 
    313321    [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)
    316324      : base(original, cloner) {
    317325      scope = cloner.Clone(original.scope);
     
    319327      iterations = cloner.Clone(original.iterations);
    320328    }
    321     public SingleSolutionMemPRContext(TContext baseContext, ISingleObjectiveSolutionScope<TSolution> solution) {
     329    public MemPRSolutionContext(TContext baseContext, ISingleObjectiveSolutionScope<TSolution> solution) {
    322330      parent = baseContext;
    323331      scope = solution;
     
    327335    }
    328336
     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
    329342    #region IExecutionContext members
    330343    public IAtomicOperation CreateOperation(IOperator op) {
Note: See TracChangeset for help on using the changeset viewer.