Changeset 11753
- Timestamp:
- 01/13/15 18:47:19 (10 years ago)
- Location:
- branches/ProgrammableProblem
- Files:
-
- 6 added
- 26 edited
- 4 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/ProgrammableProblem/HeuristicLab.Optimization/3.3/Problems/HeuristicOptimizationProblem.cs
r11171 r11753 78 78 79 79 public U SolutionCreator { 80 get { return SolutionCreatorParameter.Value; }80 get { return (U)SolutionCreatorParameter.Value; } 81 81 protected set { SolutionCreatorParameter.Value = value; } 82 82 } 83 public ValueParameter<U>SolutionCreatorParameter {84 get { return ( ValueParameter<U>)Parameters[SolutionCreateParameterName]; }83 public IValueParameter SolutionCreatorParameter { 84 get { return (IValueParameter)Parameters[SolutionCreateParameterName]; } 85 85 } 86 86 ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator { get { return SolutionCreator; } } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable.Views/3.3/HeuristicLab.Problems.Programmable.Views-3.3.csproj
r11655 r11753 111 111 <DependentUpon>ProblemDefinitionScriptView.cs</DependentUpon> 112 112 </Compile> 113 <Compile Include="MultiObjective ProgrammableProblemView.cs">113 <Compile Include="MultiObjectiveScriptableProblemView.cs"> 114 114 <SubType>UserControl</SubType> 115 115 </Compile> 116 <Compile Include="MultiObjective ProgrammableProblemView.Designer.cs">117 <DependentUpon>MultiObjective ProgrammableProblemView.cs</DependentUpon>116 <Compile Include="MultiObjectiveScriptableProblemView.Designer.cs"> 117 <DependentUpon>MultiObjectiveScriptableProblemView.cs</DependentUpon> 118 118 </Compile> 119 <Compile Include="SingleObjective ProgrammableProblemView.cs">119 <Compile Include="SingleObjectiveScriptableProblemView.cs"> 120 120 <SubType>UserControl</SubType> 121 121 </Compile> 122 <Compile Include="SingleObjective ProgrammableProblemView.Designer.cs">123 <DependentUpon>SingleObjective ProgrammableProblemView.cs</DependentUpon>122 <Compile Include="SingleObjectiveScriptableProblemView.Designer.cs"> 123 <DependentUpon>SingleObjectiveScriptableProblemView.cs</DependentUpon> 124 124 </Compile> 125 125 <Compile Include="Properties\AssemblyInfo.cs" /> -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable.Views/3.3/MultiObjectiveScriptableProblemView.Designer.cs
r11745 r11753 21 21 22 22 namespace HeuristicLab.Problems.Programmable.Views { 23 partial class MultiObjective ProgrammableProblemView {23 partial class MultiObjectiveScriptableProblemView { 24 24 /// <summary> 25 25 /// Required designer variable. -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable.Views/3.3/MultiObjectiveScriptableProblemView.cs
r11741 r11753 28 28 namespace HeuristicLab.Problems.Programmable.Views { 29 29 [View("Multi-Objective Programmable Problem View")] 30 [Content(typeof(MultiObjective ProgrammableProblem), true)]31 public partial class MultiObjective ProgrammableProblemView : ItemView {32 protected ViewHost DefinitionView;30 [Content(typeof(MultiObjectiveScriptableProblem), true)] 31 public partial class MultiObjectiveScriptableProblemView : ItemView { 32 protected ViewHost ScriptView; 33 33 34 public new MultiObjective ProgrammableProblem Content {35 get { return (MultiObjective ProgrammableProblem)base.Content; }34 public new MultiObjectiveScriptableProblem Content { 35 get { return (MultiObjectiveScriptableProblem)base.Content; } 36 36 set { base.Content = value; } 37 37 } 38 38 39 public MultiObjective ProgrammableProblemView() {39 public MultiObjectiveScriptableProblemView() { 40 40 InitializeComponent(); 41 DefinitionView = new ViewHost() { ViewsLabelVisible = false, Dock = DockStyle.Fill };42 Controls.Add( DefinitionView);41 ScriptView = new ViewHost() { ViewsLabelVisible = false, Dock = DockStyle.Fill }; 42 Controls.Add(ScriptView); 43 43 } 44 44 … … 46 46 base.OnContentChanged(); 47 47 if (Content == null) { 48 DefinitionView.Content = null;48 ScriptView.Content = null; 49 49 } else { 50 DefinitionView.Content = Content.ProblemDefinitionParameter.Value;50 ScriptView.Content = Content.ProblemScript; 51 51 Caption = Content.Name; 52 52 } … … 54 54 55 55 protected override void RegisterContentEvents() { 56 Content.ProblemDefinitionParameter.ValueChanged += ProblemDefinitionParameterOnValueChanged;56 //Content.ProblemDefinitionParameter.ValueChanged += ProblemDefinitionParameterOnValueChanged; 57 57 Content.NameChanged += ContentOnNameChanged; 58 58 base.RegisterContentEvents(); … … 61 61 protected override void DeregisterContentEvents() { 62 62 base.DeregisterContentEvents(); 63 Content.ProblemDefinitionParameter.ValueChanged -= ProblemDefinitionParameterOnValueChanged;63 //Content.ProblemDefinitionParameter.ValueChanged -= ProblemDefinitionParameterOnValueChanged; 64 64 Content.NameChanged -= ContentOnNameChanged; 65 65 } 66 66 67 private void ProblemDefinitionParameterOnValueChanged(object sender, EventArgs eventArgs) {68 DefinitionView.Content = Content.ProblemDefinitionParameter.Value;69 }67 //private void ProblemDefinitionParameterOnValueChanged(object sender, EventArgs eventArgs) { 68 // DefinitionView.Content = Content.ProblemDefinitionParameter.Value; 69 //} 70 70 71 71 private void ContentOnNameChanged(object sender, EventArgs eventArgs) { -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable.Views/3.3/ProblemDefinitionScriptView.cs
r11484 r11753 25 25 namespace HeuristicLab.Problems.Programmable.Views { 26 26 [View("ProblemDefinitionScriptView")] 27 [Content(typeof(Problem Script), IsDefaultView = true)]27 [Content(typeof(ProblemDefinitionScript), IsDefaultView = true)] 28 28 public partial class ProblemDefinitionScriptView : ScriptView { 29 29 30 public new Problem Script Content {31 get { return (Problem Script)base.Content; }30 public new ProblemDefinitionScript Content { 31 get { return (ProblemDefinitionScript)base.Content; } 32 32 set { base.Content = value; } 33 33 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable.Views/3.3/SingleObjectiveScriptableProblemView.Designer.cs
r11741 r11753 21 21 22 22 namespace HeuristicLab.Problems.Programmable.Views { 23 partial class SingleObjective ProgrammableProblemView {23 partial class SingleObjectiveScriptableProblemView { 24 24 /// <summary> 25 25 /// Required designer variable. -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable.Views/3.3/SingleObjectiveScriptableProblemView.cs
r11741 r11753 25 25 using HeuristicLab.MainForm; 26 26 using HeuristicLab.MainForm.WindowsForms; 27 using HeuristicLab.Problems.Programmable.Problems; 27 28 28 29 namespace HeuristicLab.Problems.Programmable.Views { 29 [View("Single-Objective Programmable Problem View")]30 [Content(typeof(SingleObjective ProgrammableProblem), true)]31 public partial class SingleObjective ProgrammableProblemView : ItemView {32 protected ViewHost DefinitionView;30 [View("Single-Objective Scriptable Problem View")] 31 [Content(typeof(SingleObjectiveScriptableProblem), true)] 32 public partial class SingleObjectiveScriptableProblemView : ItemView { 33 protected ViewHost ScriptView; 33 34 34 public new SingleObjective ProgrammableProblem Content {35 get { return (SingleObjective ProgrammableProblem)base.Content; }35 public new SingleObjectiveScriptableProblem Content { 36 get { return (SingleObjectiveScriptableProblem)base.Content; } 36 37 set { base.Content = value; } 37 38 } 38 39 39 public SingleObjective ProgrammableProblemView() {40 public SingleObjectiveScriptableProblemView() { 40 41 InitializeComponent(); 41 DefinitionView = new ViewHost() { ViewsLabelVisible = false, Dock = DockStyle.Fill };42 Controls.Add( DefinitionView);42 ScriptView = new ViewHost() { ViewsLabelVisible = false, Dock = DockStyle.Fill }; 43 Controls.Add(ScriptView); 43 44 } 44 45 … … 46 47 base.OnContentChanged(); 47 48 if (Content == null) { 48 DefinitionView.Content = null;49 ScriptView.Content = null; 49 50 } else { 50 DefinitionView.Content = Content.ProblemDefinitionParameter.Value;51 ScriptView.Content = Content.ProblemScript; 51 52 Caption = Content.Name; 52 53 } … … 54 55 55 56 protected override void RegisterContentEvents() { 56 Content.ProblemDefinitionParameter.ValueChanged += ProblemDefinitionParameterOnValueChanged;57 //Content.ProblemDefinitionParameter.ValueChanged += ProblemDefinitionParameterOnValueChanged; 57 58 Content.NameChanged += ContentOnNameChanged; 58 59 base.RegisterContentEvents(); … … 61 62 protected override void DeregisterContentEvents() { 62 63 base.DeregisterContentEvents(); 63 Content.ProblemDefinitionParameter.ValueChanged -= ProblemDefinitionParameterOnValueChanged;64 //Content.ProblemDefinitionParameter.ValueChanged -= ProblemDefinitionParameterOnValueChanged; 64 65 Content.NameChanged -= ContentOnNameChanged; 65 66 } 66 67 67 private void ProblemDefinitionParameterOnValueChanged(object sender, EventArgs eventArgs) {68 DefinitionView.Content = Content.ProblemDefinitionParameter.Value;69 }68 //private void ProblemDefinitionParameterOnValueChanged(object sender, EventArgs eventArgs) { 69 // DefinitionView.Content = Content.ProblemDefinitionParameter.Value; 70 //} 70 71 71 72 private void ContentOnNameChanged(object sender, EventArgs eventArgs) { -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Encoding.cs
r11737 r11753 38 38 39 39 protected HashSet<IOperator> encodingOperators = new HashSet<IOperator>(new TypeEqualityComparer<IOperator>()); 40 [Storable] 40 41 [Storable(Name = "Operators")] 42 private IEnumerable<IOperator> StorableOperators { 43 get { return encodingOperators; } 44 set { encodingOperators = new HashSet<IOperator>(value, new TypeEqualityComparer<IOperator>()); ; } 45 } 46 41 47 public IEnumerable<IOperator> Operators { 42 48 get { return encodingOperators; } 43 private set { encodingOperators = new HashSet<IOperator>(value, new TypeEqualityComparer<IOperator>()); } 49 set { 50 if (!value.OfType<T>().Any()) 51 throw new ArgumentException("The provided operators contain no suitable solution creator"); 52 encodingOperators = new HashSet<IOperator>(value, new TypeEqualityComparer<IOperator>()); 53 54 T newSolutionCreator = (T)encodingOperators.FirstOrDefault(o => o.GetType() == solutionCreator.GetType()); 55 if (newSolutionCreator == null) newSolutionCreator = encodingOperators.OfType<T>().First(); 56 SolutionCreator = newSolutionCreator; 57 OnOperatorsChanged(); 58 } 44 59 } 45 60 … … 90 105 if (handler != null) handler(this, EventArgs.Empty); 91 106 } 107 108 public event EventHandler OperatorsChanged; 109 protected virtual void OnOperatorsChanged() { 110 ConfigureOperators(Operators); 111 var handler = OperatorsChanged; 112 if (handler != null) handler(this, EventArgs.Empty); 113 } 92 114 } 93 115 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/IntegerEncoding.cs
r11746 r11753 174 174 } 175 175 private void DiscoverOperators() { 176 var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, true, false, false); 176 var pluginDescription = ApplicationManager.Manager.GetDeclaringPlugin(typeof(IIntegerVectorOperator)); 177 var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, pluginDescription, true, false, false); 177 178 var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t)); 178 179 var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList(); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/MultiEncoding.cs
r11619 r11753 89 89 90 90 public override void ConfigureOperators(IEnumerable<IOperator> operators) { 91 92 91 } 93 92 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/PermutationEncoding.cs
r11746 r11753 148 148 } 149 149 private void DiscoverOperators() { 150 var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, true, false, false); 150 var pluginDescription = ApplicationManager.Manager.GetDeclaringPlugin(typeof(IPermutationOperator)); 151 var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, pluginDescription, true, false, false); 151 152 var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t)); 152 153 var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList(); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/RealEncoding.cs
r11739 r11753 173 173 } 174 174 private void DiscoverOperators() { 175 var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, true, false, false); 175 var pluginDescription = ApplicationManager.Manager.GetDeclaringPlugin(typeof(IRealVectorOperator)); 176 var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, pluginDescription, true, false, false); 176 177 var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t)); 177 178 var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList(); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/HeuristicLab.Problems.Programmable-3.3.csproj
r11740 r11753 147 147 <Compile Include="Interfaces\IMultiObjectiveProblemDefinition.cs" /> 148 148 <Compile Include="Interfaces\IEncodingOperator.cs" /> 149 <Compile Include="Interfaces\ISingleObjectiveMoveOperator.cs" /> 149 150 <Compile Include="New\ProgrammableProblem.cs" /> 150 151 <Compile Include="New\MultiObjectiveProgrammableProblem.cs" /> 151 152 <Compile Include="New\MultiObjectiveScriptableProblem.cs" /> 153 <Compile Include="New\Scripts\Templates\CompiledMultiObjectiveProblemDefinition.cs" /> 154 <Compile Include="New\Scripts\Templates\CompiledSingleObjectiveProblemDefinition.cs" /> 155 <Compile Include="New\Scripts\Templates\ScriptTemplates.Designer.cs"> 156 <AutoGen>True</AutoGen> 157 <DesignTime>True</DesignTime> 158 <DependentUpon>ScriptTemplates.resx</DependentUpon> 159 </Compile> 152 160 <Compile Include="Operators\MultiEncodingManipulator.cs" /> 153 161 <Compile Include="Operators\MultiEncodingCrossover.cs" /> … … 173 181 <Compile Include="OLD_MultiObjectiveProgrammableProblem.cs" /> 174 182 <Compile Include="OLD_SingleObjectiveProgrammableProblem.cs" /> 183 <EmbeddedResource Include="New\Scripts\Templates\ScriptTemplates.resx"> 184 <Generator>ResXFileCodeGenerator</Generator> 185 <LastGenOutput>ScriptTemplates.Designer.cs</LastGenOutput> 186 </EmbeddedResource> 175 187 <None Include="HeuristicLab.snk" /> 176 188 <None Include="Plugin.cs.frame" /> -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IEncoding.cs
r11737 r11753 28 28 public interface IEncoding : IParameterizedNamedItem { 29 29 ISolutionCreator SolutionCreator { get; set; } 30 31 IEnumerable<IOperator> Operators { get; } 30 IEnumerable<IOperator> Operators { get; set; } 32 31 33 32 Individual GetIndividual(IScope scope); … … 36 35 37 36 event EventHandler SolutionCreatorChanged; 37 event EventHandler OperatorsChanged; 38 38 } 39 39 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IMultiEncodingOperator.cs
r11587 r11753 26 26 void AddEncoding(IEncoding encoding); 27 27 bool RemoveEncoding(IEncoding encoding); 28 void ReplaceEncoding(IEncoding oldEncoding, IEncoding newEncoding); 28 29 } 29 30 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IProblemDefinition.cs
r11739 r11753 25 25 namespace HeuristicLab.Problems.Programmable { 26 26 public interface IProblemDefinition : INamedItem { 27 IEncoding Encoding { get; } 28 IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random); 27 IEncoding Encoding { get; } 29 28 } 30 29 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/ISingleObjectiveProblemDefinition.cs
r11739 r11753 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 using HeuristicLab.Core; 23 24 using HeuristicLab.Optimization; … … 28 29 double Evaluate(Individual individual, IRandom random); 29 30 void Analyze(Individual[] individuals, double[] qualities, ResultCollection results); 31 IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random); 30 32 } 31 33 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/MultiObjectiveProgrammableProblem.cs
r11740 r11753 42 42 : base() { 43 43 44 Operators.Add(Evaluator); 44 45 Operators.Add(new BestScopeSolutionAnalyzer()); 45 Operators.Add(new SingleObjectiveEvaluator());46 46 Operators.Add(new SingleObjectiveAnalyzer()); 47 47 -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/MultiObjectiveScriptableProblem.cs
r11740 r11753 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 28 29 namespace HeuristicLab.Problems.Programmable .Problems{29 namespace HeuristicLab.Problems.Programmable { 30 30 [Item("Scriptable Problem (multi-objective)", "Represents a multi-objective problem that can be scripted.")] 31 31 [Creatable("1 Test")] … … 37 37 } 38 38 39 p rivateMultiObjectiveProblemDefinitionScript ProblemScript {39 public MultiObjectiveProblemDefinitionScript ProblemScript { 40 40 get { return MultiObjectiveProblemScriptParameter.Value; } 41 41 } 42 42 43 p rivateIMultiObjectiveProblemDefinition ProblemDefinition {43 public IMultiObjectiveProblemDefinition ProblemDefinition { 44 44 get { return MultiObjectiveProblemScriptParameter.Value; } 45 45 } … … 48 48 : base(original, cloner) { 49 49 RegisterEvents(); 50 ProblemScript.Compile(); 50 51 } 51 52 public override IDeepCloneable Clone(Cloner cloner) { return new MultiObjectiveScriptableProblem(this, cloner); } … … 64 65 private void AfterDeserialization() { 65 66 RegisterEvents(); 67 ProblemScript.Compile(); 66 68 } 67 69 … … 71 73 72 74 private void OnProblemDefinitionChanged() { 75 Encoding = ProblemDefinition.Encoding; 76 OnOperatorsChanged(); 77 OnReset(); 73 78 } 74 79 … … 84 89 ProblemDefinition.Analyze(individuals, qualities, results); 85 90 } 86 public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {87 return ProblemDefinition.GetNeighbors(individual, random);88 }89 91 } 90 92 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/ProgrammableProblem.cs
r11739 r11753 28 28 using HeuristicLab.Parameters; 29 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.Problems.Programmable.Interfaces; 30 31 31 32 namespace HeuristicLab.Problems.Programmable { … … 46 47 protected set { 47 48 if (value == null) throw new ArgumentNullException("Encoding must not be null."); 48 if (Encoding != null) Parameters.RemoveRange(Encoding.Parameters);49 49 EncodingParameter.Value = value; 50 Parameters.AddRange(Encoding.Parameters);51 50 } 52 51 } 52 //mkommend necessary for reuse of operators if the encoding changes 53 private TEncoding oldEncoding; 53 54 54 55 public virtual IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) { … … 57 58 58 59 protected override IEnumerable<IItem> GetOperators() { 60 if (Encoding == null) return base.GetOperators(); 59 61 return base.GetOperators().Concat(Encoding.Operators); 60 62 } 61 63 public override IEnumerable<IParameterizedItem> ExecutionContextItems { 62 get { return base.ExecutionContextItems.Concat(new[] { Encoding }); } 64 get { 65 if (Encoding == null) return base.ExecutionContextItems; 66 return base.ExecutionContextItems.Concat(new[] { Encoding }); 67 } 63 68 } 64 69 65 p ublicProgrammableProblem()70 protected ProgrammableProblem() 66 71 : base() { 67 72 Parameters.Add(new ValueParameter<TEncoding>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any.")); 73 oldEncoding = Encoding; 68 74 RegisterEvents(); 69 75 } … … 71 77 protected ProgrammableProblem(ProgrammableProblem<TEncoding, TEvaluator> original, Cloner cloner) 72 78 : base(original, cloner) { 79 oldEncoding = cloner.Clone(original.oldEncoding); 73 80 RegisterEvents(); 74 81 } 75 82 76 83 [StorableConstructor] 77 protected ProgrammableProblem(bool deserializing) 78 : base(deserializing) { 84 protected ProgrammableProblem(bool deserializing) : base(deserializing) { } 85 [StorableHook(HookType.AfterDeserialization)] 86 private void AfterDeserialization() { 87 oldEncoding = Encoding; 79 88 RegisterEvents(); 80 89 } … … 85 94 86 95 protected virtual void OnEncodingChanged() { 96 if (oldEncoding != null) AdaptEncodingOperators(oldEncoding, Encoding); 97 oldEncoding = Encoding; 98 87 99 var solutionCreatorType = Encoding.GetType().BaseType.GetGenericArguments(); 88 100 var paramType = typeof(ValueParameter<>).MakeGenericType(solutionCreatorType); … … 91 103 Parameters.Remove(SolutionCreatorParameter); 92 104 Parameters.Add(solutionCreatorParam); 105 106 OnOperatorsChanged(); 107 OnReset(); 93 108 } 94 109 … … 106 121 } 107 122 123 private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) { 124 if (oldEncoding.GetType() != newEncoding.GetType()) return; 125 126 if (oldEncoding.GetType() == typeof(MultiEncoding)) { 127 var oldMultiEncoding = (MultiEncoding)oldEncoding; 128 var newMultiEncoding = (MultiEncoding)newEncoding; 129 if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return; 130 131 var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n }); 132 foreach (var multi in nestedEncodings) 133 AdaptEncodingOperators(multi.oldEnc, multi.newEnc); 134 135 foreach (var op in oldMultiEncoding.Operators.OfType<IMultiEncodingOperator>()) 136 foreach (var multi in nestedEncodings) 137 op.ReplaceEncoding(multi.oldEnc, multi.newEnc); 138 } 139 140 var comparer = new TypeEqualityComparer<IOperator>(); 141 var oldOperators = oldEncoding.Operators; 142 var newOperators = newEncoding.Operators; 143 144 var operators = oldOperators.Intersect(newOperators, comparer); 145 operators = operators.Union(newOperators, comparer); 146 147 newEncoding.ConfigureOperators(operators); 148 newEncoding.Operators = operators; 149 } 150 108 151 } 109 152 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/CompiledProblemDefinition.cs
r11739 r11753 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using HeuristicLab.Common; 25 24 using HeuristicLab.Core; … … 52 51 } 53 52 53 public CompiledProblemDefinition() { } 54 54 protected CompiledProblemDefinition(IEncoding encoding) : this(encoding, "ProblemDefinition") { } 55 55 protected CompiledProblemDefinition(IEncoding encoding, string name) : this(encoding, name, string.Empty) { } … … 57 57 : base(name, description) { 58 58 Encoding = encoding; 59 //TODO call initialize and set refactor ctors60 59 } 61 62 public abstract IEnumerable<Individual> GetNeighbors(Individual vector, IRandom random);63 60 } 64 61 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/MultiObjectiveProblemDefinitionScript.cs
r11739 r11753 24 24 using HeuristicLab.Optimization; 25 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 using HeuristicLab.Problems.Programmable.New.Scripts.Templates; 26 27 27 28 namespace HeuristicLab.Problems.Programmable { … … 32 33 33 34 protected override string CodeTemplate { 34 get { 35 return @"using System; 36 using System.Linq; 37 using System.Collections.Generic; 38 using HeuristicLab.Common; 39 using HeuristicLab.Core; 40 using HeuristicLab.Data; 41 using HeuristicLab.Encodings.PermutationEncoding; 42 using HeuristicLab.Optimization; 43 using HeuristicLab.Problems.Programmable; 35 get { return ScriptTemplates.CompiledMultiObjectiveProblemDefinition; } 36 } 44 37 45 public class CustomProblemDefinition : CompiledProblemDefinition, IMultiObjectiveProblemDefinition { 46 public bool[] Maximization { get { return new [] { false, false }; } } 47 48 public CustomProblemDefinition() { 49 // Define the solution encoding which can also consist of multiple vectors, examples below 50 // Encoding = new BinaryEncoding(""b"", length: 5); 51 // Encoding = new IntegerEncoding(""i"", lenght: 5, min: 2, max: 14, step: 4); 52 // Encoding = new RealEncoding(""r"", length: 5, min: -1.0, max: 1.0); 53 // Encoding = new PermutationEncoding(""P"", length: 5, type: PermutationTypes.Absolute); 54 // Encoding = new MultiEncoding() 55 // .AddBinaryVector(""b"", length: 5) 56 // .AddIntegerVector(""i"", length: 5, min: 2, max: 14, step: 4) 57 // .AddRealVector(""r"", length: 5, min: -1.0, max: 1.0) 58 // .AddPermutation(""P"", length: 5, type: PermutationTypes.Absolute) 59 ; 60 } 61 62 public override void Initialize() { 63 // when the definition is created here you can initialize variables in the variable store 64 } 65 66 public double[] Evaluate(IRandom random, Individual individual) { 67 var qualities = new [] { 0.0, 0.0 }; 68 // use vars.yourVariable to access variables in the variable store i.e. yourVariable 69 // qualities = new [] { individual.RealVector(""r"").Sum(x => x * x), individual.RealVector(""r"").Sum(x => x * x * x) }; 70 return qualities; 71 } 72 73 public void Analyze(Individual[] individuals, double[][] qualities, ResultCollection results) { 74 // write or update results given the range of vectors and resulting qualities 75 // use e.g. vars.yourVariable to access variables in the variable store i.e. yourVariable 76 } 77 78 public override IEnumerable<Individual> GetNeighbors(IRandom random, Individual individual) { 79 // Create new vectors, based on the given one that represent small changes 80 // This method is only called from move-based algorithms (LocalSearch, SimulatedAnnealing, etc.) 81 while (true) { 82 // this is not an infinite loop as only a finite amount of samples will be drawn 83 // it is possible to return a concrete amount of neighbors also 84 var neighbor = (Individual)individual.Clone(); 85 //e.g. make a bit flip in a binary parameter 86 //var bIndex = random.Next(neighbor.BinaryVector(""b"").Length); 87 //neighbor.BinaryVector(""b"")[bIndex] = !neighbor.BinaryVector(""b"")[bIndex]; 88 yield return neighbor; 89 } 90 } 91 92 // implement further classes and methods 93 }"; 94 } 38 private new IMultiObjectiveProblemDefinition CompiledProblemDefinition { 39 get { return (IMultiObjectiveProblemDefinition)base.CompiledProblemDefinition; } 95 40 } 96 41 … … 108 53 } 109 54 110 public new IMultiObjectiveProblemDefinition CompiledProblemDefinition {111 get { return (IMultiObjectiveProblemDefinition)base.CompiledProblemDefinition; }112 }113 114 55 bool[] IMultiObjectiveProblemDefinition.Maximization { 115 get { return CompiledProblemDefinition != null ? CompiledProblemDefinition.Maximization : new bool[0]; }56 get { return CompiledProblemDefinition.Maximization; } 116 57 } 117 58 -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/ProblemDefinitionScript.cs
r11739 r11753 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using System.Linq; 25 24 using System.Reflection; … … 47 46 variableStore = cloner.Clone(original.variableStore); 48 47 } 49 protected ProblemDefinitionScript() :base(){ 48 protected ProblemDefinitionScript() 49 : base() { 50 50 variableStore = new VariableStore(); 51 51 } 52 52 53 53 IEncoding IProblemDefinition.Encoding { 54 get { return CompiledProblemDefinition != null ? CompiledProblemDefinition.Encoding : null; } 55 } 56 IEnumerable<Individual> IProblemDefinition.GetNeighbors(Individual individual, IRandom random) { 57 return CompiledProblemDefinition.GetNeighbors(individual, random); 54 get { return CompiledProblemDefinition.Encoding; } 58 55 } 59 56 … … 62 59 protected IProblemDefinition CompiledProblemDefinition { 63 60 get { 64 lock (locker) { 65 if (compiledProblemDefinition == null) { 66 Compile(); 67 } 68 } 61 //lock (locker) { 62 // if (compiledProblemDefinition == null) { 63 // Compile(); 64 // } 65 //} 66 if (compiledProblemDefinition == null) throw new InvalidOperationException("The problem definition script is not compiled and cannot be used."); 69 67 return compiledProblemDefinition; 70 68 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/SingleObjectiveProblemDefinitionScript.cs
r11739 r11753 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using HeuristicLab.Common; … … 26 25 using HeuristicLab.Optimization; 27 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using HeuristicLab.Problems.Programmable.New.Scripts.Templates; 28 28 29 29 namespace HeuristicLab.Problems.Programmable { … … 34 34 35 35 protected override string CodeTemplate { 36 get { 37 return @"using System; 38 using System.Linq; 39 using System.Collections.Generic; 40 using HeuristicLab.Common; 41 using HeuristicLab.Core; 42 using HeuristicLab.Data; 43 using HeuristicLab.Encodings.PermutationEncoding; 44 using HeuristicLab.Optimization; 45 using HeuristicLab.Problems.Programmable; 36 get { return ScriptTemplates.CompiledSingleObjectiveProblemDefinition; } 37 } 46 38 47 public class CustomProblemDefinition : CompiledProblemDefinition, ISingleObjectiveProblemDefinition { 48 public bool IsMaximizationProblem { get { return false; } } 49 50 public CustomProblemDefinition() { 51 // Define the solution encoding which can also consist of multiple vectors, examples below 52 // Encoding = new BinaryEncoding(""b"", length: 5); 53 // Encoding = new IntegerEncoding(""i"", lenght: 5, min: 2, max: 14, step: 4); 54 // Encoding = new RealEncoding(""r"", length: 5, min: -1.0, max: 1.0); 55 // Encoding = new PermutationEncoding(""P"", length: 5, type: PermutationTypes.Absolute); 56 // Encoding = new MultiEncoding() 57 // .AddBinaryVector(""b"", length: 5) 58 // .AddIntegerVector(""i"", length: 5, min: 2, max: 14, step: 4) 59 // .AddRealVector(""r"", length: 5, min: -1.0, max: 1.0) 60 // .AddPermutation(""P"", length: 5, type: PermutationTypes.Absolute) 61 ; 62 } 63 64 public override void Initialize() { 65 // when the definition is created here you can initialize variables in the variable store 66 } 67 68 public double Evaluate(IRandom random, Individual individual) { 69 var quality = 0.0; 70 // use vars.yourVariable to access variables in the variable store i.e. yourVariable 71 // quality = individual.RealVector(""r"").Sum(x => x * x); 72 return quality; 73 } 74 75 public void Analyze(Individual[] individuals, double[] qualities, ResultCollection results) { 76 // write or update results given the range of vectors and resulting qualities 77 // use e.g. vars.yourVariable to access variables in the variable store i.e. yourVariable 78 } 79 80 public override IEnumerable<Individual> GetNeighbors(IRandom random, Individual individual) { 81 // Create new vectors, based on the given one that represent small changes 82 // This method is only called from move-based algorithms (LocalSearch, SimulatedAnnealing, etc.) 83 while (true) { 84 // this is not an infinite loop as only a finite amount of samples will be drawn 85 // it is possible to return a concrete amount of neighbors also 86 var neighbor = (Individual)individual.Clone(); 87 //e.g. make a bit flip in a binary parameter 88 //var bIndex = random.Next(neighbor.BinaryVector(""b"").Length); 89 //neighbor.BinaryVector(""b"")[bIndex] = !neighbor.BinaryVector(""b"")[bIndex]; 90 yield return neighbor; 91 } 92 } 93 94 // implement further classes and methods 95 }"; 96 } 39 private new ISingleObjectiveProblemDefinition CompiledProblemDefinition { 40 get { return (ISingleObjectiveProblemDefinition)base.CompiledProblemDefinition; } 97 41 } 98 42 … … 100 44 private SingleObjectiveProblemDefinitionScript(bool deserializing) : base(deserializing) { } 101 45 private SingleObjectiveProblemDefinitionScript(SingleObjectiveProblemDefinitionScript original, Cloner cloner) : base(original, cloner) { } 102 public SingleObjectiveProblemDefinitionScript() :base(){ 46 public SingleObjectiveProblemDefinitionScript() 47 : base() { 103 48 Code = CodeTemplate; 104 49 } … … 108 53 } 109 54 110 public new ISingleObjectiveProblemDefinition CompiledProblemDefinition {111 get { return (ISingleObjectiveProblemDefinition)base.CompiledProblemDefinition; }112 }113 114 55 bool ISingleObjectiveProblemDefinition.Maximization { 115 get { return CompiledProblemDefinition != null && CompiledProblemDefinition.Maximization; }56 get { return CompiledProblemDefinition.Maximization; } 116 57 } 117 58 … … 123 64 CompiledProblemDefinition.Analyze(individuals, qualities, results); 124 65 } 66 IEnumerable<Individual> ISingleObjectiveProblemDefinition.GetNeighbors(Individual individual, IRandom random) { 67 return CompiledProblemDefinition.GetNeighbors(individual, random); 68 } 125 69 } 126 70 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/SingleObjectiveProgrammableProblem.cs
r11739 r11753 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Data; 26 27 using HeuristicLab.Optimization; 28 using HeuristicLab.Parameters; 27 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 30 29 31 namespace HeuristicLab.Problems.Programmable { 30 32 [StorableClass] 31 public abstract class SingleObjectiveProgrammableProblem<TEncoding> : ProgrammableProblem<TEncoding, SingleObjectiveEvaluator>, ISingleObjectiveProblemDefinition 33 public abstract class SingleObjectiveProgrammableProblem<TEncoding> : ProgrammableProblem<TEncoding, SingleObjectiveEvaluator>, 34 ISingleObjectiveProblemDefinition, ISingleObjectiveHeuristicOptimizationProblem 32 35 where TEncoding : class, IEncoding { 33 36 [StorableConstructor] … … 41 44 protected SingleObjectiveProgrammableProblem() 42 45 : base() { 46 Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", new BoolValue())); 47 Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem.")); 43 48 49 Operators.Add(Evaluator); 44 50 Operators.Add(new BestScopeSolutionAnalyzer()); 45 Operators.Add(new SingleObjectiveEvaluator());46 51 Operators.Add(new SingleObjectiveAnalyzer()); 52 Operators.Add(new SingleObjectiveImprover()); 53 Operators.Add(new SingleObjectiveMoveEvaluator()); 54 Operators.Add(new SingleObjectiveMoveGenerator()); 55 Operators.Add(new SingleObjectiveMoveMaker()); 47 56 48 57 ParameterizeOperators(); … … 72 81 } 73 82 83 #region ISingleObjectiveHeuristicOptimizationProblem Members 84 IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter { 85 get { return Parameters["Maximization"]; } 86 } 87 IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter { 88 get { return Parameters["BestKnownQuality"]; } 89 } 90 ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator { 91 get { return Evaluator; } 92 } 93 #endregion 74 94 } 75 95 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/SingleObjectiveScriptableProblem.cs
r11739 r11753 21 21 22 22 using System.Collections.Generic; 23 using System.Linq; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 37 38 } 38 39 39 p rivateSingleObjectiveProblemDefinitionScript ProblemScript {40 public SingleObjectiveProblemDefinitionScript ProblemScript { 40 41 get { return SingleObjectiveProblemScriptParameter.Value; } 41 42 } 42 43 43 p rivateISingleObjectiveProblemDefinition ProblemDefinition {44 public ISingleObjectiveProblemDefinition ProblemDefinition { 44 45 get { return SingleObjectiveProblemScriptParameter.Value; } 45 46 } … … 48 49 : base(original, cloner) { 49 50 RegisterEvents(); 51 ProblemScript.Compile(); 50 52 } 51 53 public override IDeepCloneable Clone(Cloner cloner) { return new SingleObjectiveScriptableProblem(this, cloner); } … … 59 61 Parameters.Add(new FixedValueParameter<SingleObjectiveProblemDefinitionScript>("ProblemScript", "Defines the problem.", new SingleObjectiveProblemDefinitionScript() { Name = Name })); 60 62 RegisterEvents(); 63 64 61 65 } 62 66 … … 64 68 private void AfterDeserialization() { 65 69 RegisterEvents(); 70 ProblemScript.Compile(); 66 71 } 67 72 … … 71 76 72 77 private void OnProblemDefinitionChanged() { 78 Encoding = ProblemDefinition.Encoding; 73 79 } 74 80 -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiEncodingOperator.cs
r11737 r11753 20 20 #endregion 21 21 using System; 22 using System.Collections.Generic; 22 23 using System.Linq; 23 24 using HeuristicLab.Common; … … 30 31 namespace HeuristicLab.Problems.Programmable.Operators { 31 32 public abstract class MultiEncodingOperator<T> : Operator, IMultiEncodingOperator where T : class,IOperator { 33 private List<IEncoding> encodings = new List<IEncoding>(); 34 [Storable(Name = "Encodings")] 35 private IEnumerable<IEncoding> StorableEncodings { 36 get { return encodings; } 37 set { encodings = new List<IEncoding>(value); } 38 } 39 32 40 [StorableConstructor] 33 protected MultiEncodingOperator(bool deserializing) : base(deserializing) { } 34 protected MultiEncodingOperator(MultiEncodingOperator<T> original, Cloner cloner) : base(original, cloner) { } 41 protected MultiEncodingOperator(bool deserializing) 42 : base(deserializing) { 43 } 44 45 protected MultiEncodingOperator(MultiEncodingOperator<T> original, Cloner cloner) 46 : base(original, cloner) { 47 encodings = new List<IEncoding>(original.encodings.Select(cloner.Clone)); 48 foreach (var encoding in encodings) 49 encoding.OperatorsChanged += Encoding_OperatorsChanged; 50 } 35 51 36 52 protected MultiEncodingOperator() : base() { } 53 54 [StorableHook(HookType.AfterDeserialization)] 55 private void AfterDeserialization() { 56 foreach (var encoding in encodings) 57 encoding.OperatorsChanged += Encoding_OperatorsChanged; 58 } 37 59 38 60 … … 44 66 public virtual void AddEncoding(IEncoding encoding) { 45 67 if (Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was already added.", encoding.Name)); 68 69 encodings.Add(encoding); 70 encoding.OperatorsChanged += Encoding_OperatorsChanged; 71 46 72 var param = new ConstrainedValueParameter<T>(encoding.Name, new ItemSet<T>(encoding.Operators.OfType<T>())); 47 73 param.Value = param.ValidValues.First(); … … 50 76 51 77 public virtual bool RemoveEncoding(IEncoding encoding) { 78 if (!encodings.Remove(encoding)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name)); 79 encoding.OperatorsChanged -= Encoding_OperatorsChanged; 52 80 return Parameters.Remove(encoding.Name); 81 } 82 83 public virtual void ReplaceEncoding(IEncoding oldEncoding, IEncoding newEncoding) { 84 var oldParameterValue = GetParameter(oldEncoding).Value; 85 RemoveEncoding(oldEncoding); 86 AddEncoding(newEncoding); 87 88 var parameter = GetParameter(newEncoding); 89 var newParameterValue = parameter.ValidValues.FirstOrDefault(op => op.GetType() == oldParameterValue.GetType()); 90 if (newParameterValue != null) parameter.Value = newParameterValue; 53 91 } 54 92 … … 59 97 } 60 98 99 private void Encoding_OperatorsChanged(object sender, EventArgs e) { 100 var encoding = (IEncoding)sender; 101 var param = GetParameter(encoding); 61 102 103 var oldParameterValue = param.Value; 104 param.ValidValues.Clear(); 105 foreach (var op in encoding.Operators.OfType<T>()) 106 param.ValidValues.Add(op); 62 107 108 var newValue = param.ValidValues.FirstOrDefault(op => op.GetType() == oldParameterValue.GetType()); 109 if (newValue == null) newValue = param.ValidValues.First(); 110 param.Value = newValue; 111 } 63 112 } 64 113 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveEvaluator.cs
r11739 r11753 32 32 [Item("Single-objective MoveEvaluator", "Evaluates a parameter vector that results from a move.")] 33 33 [StorableClass] 34 public class SingleObjectiveMoveEvaluator : SingleSuccessorOperator, ISingleObjectiveEvaluationOperator, ISingleObjectiveMoveEvaluator, IStochasticOperator {34 public class SingleObjectiveMoveEvaluator : SingleSuccessorOperator, ISingleObjectiveEvaluationOperator, ISingleObjectiveMoveEvaluator, IStochasticOperator, ISingleObjectiveMoveOperator { 35 35 36 36 public ILookupParameter<IRandom> RandomParameter { -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveGenerator.cs
r11739 r11753 35 35 [Item("Single-objective MoveGenerator", "Calls the GetNeighbors method of the problem definition to obtain the moves.")] 36 36 [StorableClass] 37 public class SingleObjectiveMoveGenerator : SingleSuccessorOperator, INeighborBasedOperator, IMultiMoveGenerator, IStochasticOperator {37 public class SingleObjectiveMoveGenerator : SingleSuccessorOperator, INeighborBasedOperator, IMultiMoveGenerator, IStochasticOperator, ISingleObjectiveMoveOperator { 38 38 public ILookupParameter<IRandom> RandomParameter { 39 39 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveMaker.cs
r11739 r11753 32 32 [Item("Single-objective MoveMaker", "Applies a move.")] 33 33 [StorableClass] 34 public class SingleObjectiveMoveMaker : InstrumentedOperator, IMoveMaker {34 public class SingleObjectiveMoveMaker : InstrumentedOperator, IMoveMaker, ISingleObjectiveMoveOperator { 35 35 public ILookupParameter<IEncoding> EncodingParameter { 36 36 get { return (ILookupParameter<IEncoding>)Parameters["Encoding"]; }
Note: See TracChangeset
for help on using the changeset viewer.