Changeset 13351 for branches/ProblemRefactoring
- Timestamp:
- 11/23/15 22:18:35 (9 years ago)
- Location:
- branches/ProblemRefactoring
- Files:
-
- 10 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Encoding.cs
r13336 r13351 60 60 } 61 61 62 ISolutionCreator IEncoding.SolutionCreator { 63 get { return solutionCreator; } 64 } 65 62 66 [Storable] 63 67 private ISolutionCreator<TSolution> solutionCreator; -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/MultiSolution.cs
r13350 r13351 25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Core; 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 28 28 29 namespace 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); 32 53 } 33 54 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); 39 57 } 40 58 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] { 48 60 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; 52 64 } 53 65 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; 57 68 } 58 69 } 59 70 60 public override TEncoding GetEncoding<TEncoding>(){71 public TEncoding GetEncoding<TEncoding>() where TEncoding : IEncoding { 61 72 TEncoding encoding; 62 73 try { 63 74 encoding = (TEncoding)Encoding.Encodings.SingleOrDefault(e => e is TEncoding); 64 75 } catch (InvalidOperationException) { 65 throw new InvalidOperationException(string.Format("The individualuses multiple {0} .", typeof(TEncoding).GetPrettyName()));76 throw new InvalidOperationException(string.Format("The solution uses multiple {0} .", typeof(TEncoding).GetPrettyName())); 66 77 } 67 if (encoding == null) throw new InvalidOperationException(string.Format("The individualdoes 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())); 68 79 return encoding; 69 80 } 70 81 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; 74 91 } 75 92 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IEncoding.cs
r13336 r13351 25 25 26 26 namespace 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; } 30 29 IEnumerable<IOperator> Operators { get; set; } 31 30 … … 37 36 } 38 37 39 38 public interface IEncoding<TSolution> : IEncoding 39 where TSolution : class, ISolution { 40 new ISolutionCreator<TSolution> SolutionCreator { get; set; } 41 } 40 42 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IMultiEncodingOperator.cs
r13336 r13351 23 23 24 24 namespace 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); 28 28 } 29 29 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/MultiEncoding.cs
r12012 r13351 31 31 [Item("MultiEncoding", "Describes a combined encoding consisting of multiple simpler encodings.")] 32 32 [StorableClass] 33 public sealed class MultiEncoding : Encoding<Multi EncodingCreator> {33 public sealed class MultiEncoding : Encoding<MultiSolution> { 34 34 35 35 private readonly List<IEncoding> encodings; … … 57 57 foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>()) 58 58 AddOperator(@operator); 59 }60 61 public override Individual GetIndividual(IScope scope) {62 return new MultiEncodingIndividual(this, scope);63 59 } 64 60 -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingCreator.cs
r12012 r13351 29 29 [Item("MultiEncodingCreator", "Contains solution creators that together create a multi-encoding.")] 30 30 [StorableClass] 31 public sealed class MultiEncodingCreator : MultiEncodingOperator<ISolutionCreator>, ISolutionCreator {31 public sealed class MultiEncodingCreator : MultiEncodingOperator<ISolutionCreator>, ISolutionCreator<MultiSolution> { 32 32 [StorableConstructor] 33 33 private MultiEncodingCreator(bool deserializing) : base(deserializing) { } 34 35 34 private MultiEncodingCreator(MultiEncodingCreator original, Cloner cloner) : base(original, cloner) { } 36 35 public MultiEncodingCreator() { } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingOperator.cs
r12012 r13351 31 31 namespace HeuristicLab.Optimization { 32 32 [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 { 34 34 private List<IEncoding> encodings = new List<IEncoding>(); 35 35 [Storable(Name = "Encodings")] -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/ScopeUtil.cs
r13337 r13351 46 46 } 47 47 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 48 56 } 49 57 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/HeuristicLab.Optimization-3.3.csproj
r13336 r13351 121 121 <ItemGroup> 122 122 <Compile Include="Algorithms\BasicAlgorithm.cs" /> 123 <Compile Include="BasicProblems\Individuals\MultiSolution.cs" /> 123 124 <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" /> 124 130 <Compile Include="BasicProblems\Problem.cs" /> 125 131 <Compile Include="BasicProblems\Encoding.cs" /> … … 323 329 </BootstrapperPackage> 324 330 </ItemGroup> 325 <ItemGroup> 326 <Folder Include="BasicProblems\Individuals\" /> 327 </ItemGroup> 331 <ItemGroup /> 328 332 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 329 333 <!-- To modify your build process, add your task inside one of the targets below and uncomment it. -
branches/ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblems.cs
r13350 r13351 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Encodings.BinaryVectorEncoding; 27 using HeuristicLab.Optimization; 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 29 … … 49 50 public override IDeepCloneable Clone(Cloner cloner) { 50 51 return new MultiObjectiveBinaryVectorProgrammableProblem(this, cloner); 52 } 53 } 54 55 [Item("Multi Solution Programmable Problem (multi-objective)", "Represents a multi solution multi-objective problem that can be programmed with a script.")] 56 [Creatable(CreatableAttribute.Categories.Problems, Priority = 100)] 57 [StorableClass] 58 public sealed class MultiObjectiveMultiSolutionProgrammableProblem : MultiObjectiveProgrammableProblem<MultiEncoding, MultiSolution> { 59 60 [StorableConstructor] 61 private MultiObjectiveMultiSolutionProgrammableProblem(bool deserializing) : base(deserializing) { } 62 private MultiObjectiveMultiSolutionProgrammableProblem(MultiObjectiveMultiSolutionProgrammableProblem original, Cloner cloner) : base(original, cloner) { } 63 64 public MultiObjectiveMultiSolutionProgrammableProblem() 65 : base() { 66 var codeTemplate = ScriptTemplates.CompiledSingleObjectiveProblemDefinition_Template; 67 codeTemplate = codeTemplate.Replace(ENCODING_NAMESPACE, "HeuristicLab.Encodings.BinaryVectorEncoding"); 68 codeTemplate = codeTemplate.Replace(ENCODING_CLASS, "MultiEncoding"); 69 codeTemplate = codeTemplate.Replace(SOLUTION_CLASS, "MultiSolution"); 70 ProblemScript.Code = codeTemplate; 71 } 72 73 74 public override IDeepCloneable Clone(Cloner cloner) { 75 return new MultiObjectiveMultiSolutionProgrammableProblem(this, cloner); 51 76 } 52 77 } -
branches/ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblems.cs
r13350 r13351 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Encodings.BinaryVectorEncoding; 27 using HeuristicLab.Optimization; 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 29 … … 49 50 public override IDeepCloneable Clone(Cloner cloner) { 50 51 return new SingleObjectiveBinaryVectorProgrammableProblem(this, cloner); 52 } 53 } 54 55 [Item("Multi Solution Programmable Problem (single-objective)", "Represents a multi solution single-objective problem that can be programmed with a script.")] 56 [Creatable(CreatableAttribute.Categories.Problems, Priority = 100)] 57 [StorableClass] 58 public sealed class SingleObjectiveMultiSolutionProgrammableProblem : SingleObjectiveProgrammableProblem<MultiEncoding, MultiSolution> { 59 60 [StorableConstructor] 61 private SingleObjectiveMultiSolutionProgrammableProblem(bool deserializing) : base(deserializing) { } 62 private SingleObjectiveMultiSolutionProgrammableProblem(SingleObjectiveMultiSolutionProgrammableProblem original, Cloner cloner) : base(original, cloner) { } 63 64 public SingleObjectiveMultiSolutionProgrammableProblem() 65 : base() { 66 var codeTemplate = ScriptTemplates.CompiledSingleObjectiveProblemDefinition_Template; 67 codeTemplate = codeTemplate.Replace(ENCODING_NAMESPACE, "HeuristicLab.Encodings.BinaryVectorEncoding"); 68 codeTemplate = codeTemplate.Replace(ENCODING_CLASS, "MultiEncoding"); 69 codeTemplate = codeTemplate.Replace(SOLUTION_CLASS, "MultiSolution"); 70 ProblemScript.Code = codeTemplate; 71 } 72 73 74 public override IDeepCloneable Clone(Cloner cloner) { 75 return new SingleObjectiveMultiSolutionProgrammableProblem(this, cloner); 51 76 } 52 77 }
Note: See TracChangeset
for help on using the changeset viewer.