Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/23/15 22:18:35 (9 years ago)
Author:
abeham
Message:

#2521: Adapted multi-encoding for new infrastructure

TODO: Evaluator, Analyzer, ... need to be copied

Location:
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3
Files:
8 edited
1 moved

Legend:

Unmodified
Added
Removed
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Encoding.cs

    r13336 r13351  
    6060    }
    6161
     62    ISolutionCreator IEncoding.SolutionCreator {
     63      get { return solutionCreator; }
     64    }
     65
    6266    [Storable]
    6367    private ISolutionCreator<TSolution> solutionCreator;
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/MultiSolution.cs

    r13350 r13351  
    2525using HeuristicLab.Common;
    2626using HeuristicLab.Core;
     27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
    2829namespace HeuristicLab.Optimization {
    29   public sealed class MultiEncodingIndividual : Individual {
    30     private new MultiEncoding Encoding {
    31       get { return (MultiEncoding)base.Encoding; }
     30  [Item("MultiSolution", "A solution that consists of other solutions.")]
     31  [StorableClass]
     32  public sealed class MultiSolution : Item, ISolution {
     33
     34    private MultiEncoding Encoding { get; set; }
     35    protected IScope Scope { get; private set; }
     36
     37    private readonly Dictionary<string, ISolution> solutions;
     38
     39    [StorableConstructor]
     40    private MultiSolution(bool deserializing) : base(deserializing) { }
     41
     42    private MultiSolution(MultiSolution original, Cloner cloner)
     43      : base(original, cloner) {
     44      Encoding = cloner.Clone(original.Encoding);
     45      Scope = cloner.Clone(original.Scope);
     46      solutions = original.solutions.ToDictionary(x => x.Key, x => cloner.Clone(x.Value));
     47    }
     48    public MultiSolution(MultiEncoding encoding, IScope scope) {
     49      Encoding = encoding;
     50      Scope = scope;
     51      solutions = encoding.Encodings.Select(e => new { Name = e.Name, Solution = ScopeUtil.GetSolution(scope, e) })
     52                                    .ToDictionary(x => x.Name, x => x.Solution);
    3253    }
    3354
    34     private readonly IEnumerable<Individual> individuals;
    35 
    36     public MultiEncodingIndividual(MultiEncoding encoding, IScope scope)
    37       : base(encoding, scope) {
    38       individuals = encoding.Encodings.Select(e => e.GetIndividual(scope)).ToArray();
     55    public override IDeepCloneable Clone(Cloner cloner) {
     56      return new MultiSolution(this, cloner);
    3957    }
    4058
    41     private MultiEncodingIndividual(MultiEncoding encoding, IScope scope, IEnumerable<Individual> individuals)
    42       : base(encoding, scope) {
    43       this.individuals = individuals;
    44     }
    45 
    46 
    47     public override IItem this[string name] {
     59    public ISolution this[string name] {
    4860      get {
    49         var individual = individuals.SingleOrDefault(i => i.Name == name);
    50         if (individual == null) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
    51         return individual[name];
     61        ISolution result;
     62        if (!solutions.TryGetValue(name, out result)) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
     63        return result;
    5264      }
    5365      set {
    54         var individual = individuals.SingleOrDefault(i => i.Name == name);
    55         if (individual == null) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
    56         individual[name] = value;
     66        if (!solutions.ContainsKey(name)) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
     67        solutions[name] = value;
    5768      }
    5869    }
    5970
    60     public override TEncoding GetEncoding<TEncoding>() {
     71    public TEncoding GetEncoding<TEncoding>() where TEncoding : IEncoding {
    6172      TEncoding encoding;
    6273      try {
    6374        encoding = (TEncoding)Encoding.Encodings.SingleOrDefault(e => e is TEncoding);
    6475      } catch (InvalidOperationException) {
    65         throw new InvalidOperationException(string.Format("The individual uses multiple {0} .", typeof(TEncoding).GetPrettyName()));
     76        throw new InvalidOperationException(string.Format("The solution uses multiple {0} .", typeof(TEncoding).GetPrettyName()));
    6677      }
    67       if (encoding == null) throw new InvalidOperationException(string.Format("The individual does not use a {0}.", typeof(TEncoding).GetPrettyName()));
     78      if (encoding == null) throw new InvalidOperationException(string.Format("The solution does not use a {0}.", typeof(TEncoding).GetPrettyName()));
    6879      return encoding;
    6980    }
    7081
    71     public override Individual CopyToScope(IScope scope) {
    72       var copies = individuals.Select(i => i.CopyToScope(scope)).ToArray();
    73       return new MultiEncodingIndividual(Encoding, scope, copies);
     82    public TSolution GetSolution<TSolution>() where TSolution : class, ISolution {
     83      TSolution solution;
     84      try {
     85        solution = (TSolution)solutions.SingleOrDefault(s => s.Value is TSolution).Value;
     86      } catch (InvalidOperationException) {
     87        throw new InvalidOperationException(string.Format("The solution uses multiple {0} .", typeof(TSolution).GetPrettyName()));
     88      }
     89      if (solution == null) throw new InvalidOperationException(string.Format("The solution does not use a {0}.", typeof(TSolution).GetPrettyName()));
     90      return solution;
    7491    }
    7592  }
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IEncoding.cs

    r13336 r13351  
    2525
    2626namespace HeuristicLab.Optimization {
    27   public interface IEncoding<TSolution> : IParameterizedNamedItem
    28     where TSolution : class, ISolution {
    29     ISolutionCreator<TSolution> SolutionCreator { get; set; }
     27  public interface IEncoding : IParameterizedNamedItem {
     28    ISolutionCreator SolutionCreator { get; }
    3029    IEnumerable<IOperator> Operators { get; set; }
    3130
     
    3736  }
    3837
    39 
     38  public interface IEncoding<TSolution> : IEncoding
     39    where TSolution : class, ISolution {
     40    new ISolutionCreator<TSolution> SolutionCreator { get; set; }
     41  }
    4042}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IMultiEncodingOperator.cs

    r13336 r13351  
    2323
    2424namespace HeuristicLab.Optimization {
    25   public interface IMultiEncodingOperator<TSolution> : IOperator where TSolution : class,ISolution {
    26     void AddEncoding(IEncoding<TSolution> encoding);
    27     bool RemoveEncoding(IEncoding<TSolution> encoding);
     25  public interface IMultiEncodingOperator : IOperator {
     26    void AddEncoding(IEncoding encoding);
     27    bool RemoveEncoding(IEncoding encoding);
    2828  }
    2929}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/MultiEncoding.cs

    r12012 r13351  
    3131  [Item("MultiEncoding", "Describes a combined encoding consisting of multiple simpler encodings.")]
    3232  [StorableClass]
    33   public sealed class MultiEncoding : Encoding<MultiEncodingCreator> {
     33  public sealed class MultiEncoding : Encoding<MultiSolution> {
    3434
    3535    private readonly List<IEncoding> encodings;
     
    5757      foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>())
    5858        AddOperator(@operator);
    59     }
    60 
    61     public override Individual GetIndividual(IScope scope) {
    62       return new MultiEncodingIndividual(this, scope);
    6359    }
    6460
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingCreator.cs

    r12012 r13351  
    2929  [Item("MultiEncodingCreator", "Contains solution creators that together create a multi-encoding.")]
    3030  [StorableClass]
    31   public sealed class MultiEncodingCreator : MultiEncodingOperator<ISolutionCreator>, ISolutionCreator {
     31  public sealed class MultiEncodingCreator : MultiEncodingOperator<ISolutionCreator>, ISolutionCreator<MultiSolution> {
    3232    [StorableConstructor]
    3333    private MultiEncodingCreator(bool deserializing) : base(deserializing) { }
    34 
    3534    private MultiEncodingCreator(MultiEncodingCreator original, Cloner cloner) : base(original, cloner) { }
    3635    public MultiEncodingCreator() { }
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingOperator.cs

    r12012 r13351  
    3131namespace HeuristicLab.Optimization {
    3232  [StorableClass]
    33   public abstract class MultiEncodingOperator<T> : Operator, IMultiEncodingOperator where T : class,IOperator {
     33  public abstract class MultiEncodingOperator<T> : Operator, IMultiEncodingOperator where T : class, IOperator {
    3434    private List<IEncoding> encodings = new List<IEncoding>();
    3535    [Storable(Name = "Encodings")]
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/ScopeUtil.cs

    r13337 r13351  
    4646    }
    4747
     48    public static ISolution GetSolution(IScope scope, IEncoding encoding) {
     49      var name = encoding.Name;
     50      if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name));
     51      var value = scope.Variables[name].Value as ISolution;
     52      if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null.", name));
     53      return value;
     54    }
     55
    4856  }
    4957}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/HeuristicLab.Optimization-3.3.csproj

    r13336 r13351  
    121121  <ItemGroup>
    122122    <Compile Include="Algorithms\BasicAlgorithm.cs" />
     123    <Compile Include="BasicProblems\Individuals\MultiSolution.cs" />
    123124    <Compile Include="BasicProblems\Interfaces\ISingleObjectiveProblem.cs" />
     125    <Compile Include="BasicProblems\MultiEncoding.cs" />
     126    <Compile Include="BasicProblems\Operators\MultiEncodingCreator.cs" />
     127    <Compile Include="BasicProblems\Operators\MultiEncodingCrossover.cs" />
     128    <Compile Include="BasicProblems\Operators\MultiEncodingManipulator.cs" />
     129    <Compile Include="BasicProblems\Operators\MultiEncodingOperator.cs" />
    124130    <Compile Include="BasicProblems\Problem.cs" />
    125131    <Compile Include="BasicProblems\Encoding.cs" />
     
    323329    </BootstrapperPackage>
    324330  </ItemGroup>
    325   <ItemGroup>
    326     <Folder Include="BasicProblems\Individuals\" />
    327   </ItemGroup>
     331  <ItemGroup />
    328332  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    329333  <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Note: See TracChangeset for help on using the changeset viewer.