Changeset 11885 for branches/ProgrammableProblem
- Timestamp:
- 02/04/15 13:28:37 (10 years ago)
- Location:
- branches/ProgrammableProblem
- Files:
-
- 1 added
- 14 edited
- 3 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable.Views/3.3/ProblemDefinitionScriptView.cs
r11753 r11885 20 20 #endregion 21 21 22 using System; 23 using System.Drawing; 22 24 using HeuristicLab.MainForm; 23 25 using HeuristicLab.Scripting.Views; … … 46 48 variableStoreView.Enabled = Content != null && !Locked && !ReadOnly; 47 49 } 50 51 public override bool Compile() { 52 try { 53 base.Compile(); 54 } catch (ProblemDefinitionScriptException e) { 55 PluginInfrastructure.ErrorHandling.ShowErrorDialog(e); 56 return false; 57 } 58 return true; 59 } 48 60 } 49 61 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/BinaryVectorEncoding.cs
r11879 r11885 32 32 33 33 namespace HeuristicLab.Problems.Programmable { 34 [Item("Binary Encoding", "Describes a binary vector encoding.")]34 [Item("BinaryVectorEncoding", "Describes a binary vector encoding.")] 35 35 [StorableClass] 36 public sealed class Binary Encoding : Encoding<IBinaryVectorCreator> {36 public sealed class BinaryVectorEncoding : Encoding<IBinaryVectorCreator> { 37 37 #region Encoding Parameters 38 38 [Storable] … … 59 59 60 60 [StorableConstructor] 61 private Binary Encoding(bool deserializing) : base(deserializing) { }61 private BinaryVectorEncoding(bool deserializing) : base(deserializing) { } 62 62 [StorableHook(HookType.AfterDeserialization)] 63 63 private void AfterDeserialization() { … … 65 65 DiscoverOperators(); 66 66 } 67 public override IDeepCloneable Clone(Cloner cloner) { return new Binary Encoding(this, cloner); }68 private Binary Encoding(BinaryEncoding original, Cloner cloner)67 public override IDeepCloneable Clone(Cloner cloner) { return new BinaryVectorEncoding(this, cloner); } 68 private BinaryVectorEncoding(BinaryVectorEncoding original, Cloner cloner) 69 69 : base(original, cloner) { 70 70 lengthParameter = cloner.Clone(original.lengthParameter); … … 72 72 } 73 73 74 public Binary Encoding() : this("BinaryVector", 10) { }75 public Binary Encoding(int length) : this("BinaryVector", length) { }76 public Binary Encoding(string name, int length)74 public BinaryVectorEncoding() : this("BinaryVector", 10) { } 75 public BinaryVectorEncoding(int length) : this("BinaryVector", length) { } 76 public BinaryVectorEncoding(string name, int length) 77 77 : base(name) { 78 78 lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length)); … … 98 98 #region Operator Discovery 99 99 private static readonly IEnumerable<Type> encodingSpecificOperatorTypes; 100 static Binary Encoding() {100 static BinaryVectorEncoding() { 101 101 encodingSpecificOperatorTypes = new List<Type>() { 102 102 typeof (IBinaryVectorOperator), -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/IntegerVectorEncoding.cs
r11879 r11885 33 33 34 34 namespace HeuristicLab.Problems.Programmable { 35 [Item("Integer Encoding", "Describes an integer vector encoding.")]35 [Item("IntegerVectorEncoding", "Describes an integer vector encoding.")] 36 36 [StorableClass] 37 public sealed class Integer Encoding : Encoding<IIntegerVectorCreator> {37 public sealed class IntegerVectorEncoding : Encoding<IIntegerVectorCreator> { 38 38 #region Encoding Parameters 39 39 [Storable] … … 79 79 80 80 [StorableConstructor] 81 private Integer Encoding(bool deserializing) : base(deserializing) { }81 private IntegerVectorEncoding(bool deserializing) : base(deserializing) { } 82 82 [StorableHook(HookType.AfterDeserialization)] 83 83 private void AfterDeserialization() { … … 86 86 } 87 87 88 private Integer Encoding(IntegerEncoding original, Cloner cloner)88 private IntegerVectorEncoding(IntegerVectorEncoding original, Cloner cloner) 89 89 : base(original, cloner) { 90 90 lengthParameter = cloner.Clone(original.lengthParameter); … … 92 92 RegisterParameterEvents(); 93 93 } 94 public override IDeepCloneable Clone(Cloner cloner) { return new Integer Encoding(this, cloner); }95 96 97 public Integer Encoding() : this("IntegerVector", 10) { }98 public Integer Encoding(int length) : this("integerVector", length) { }99 public Integer Encoding(string name, int length, int min = int.MinValue, int max = int.MaxValue, int? step = null)94 public override IDeepCloneable Clone(Cloner cloner) { return new IntegerVectorEncoding(this, cloner); } 95 96 97 public IntegerVectorEncoding() : this("IntegerVector", 10) { } 98 public IntegerVectorEncoding(int length) : this("integerVector", length) { } 99 public IntegerVectorEncoding(string name, int length, int min = int.MinValue, int max = int.MaxValue, int? step = null) 100 100 : base(name) { 101 101 if (min >= max) throw new ArgumentException("min must be less than max", "min"); … … 116 116 DiscoverOperators(); 117 117 } 118 public Integer Encoding(string name, int length, IList<int> min, IList<int> max, IList<int> step = null)118 public IntegerVectorEncoding(string name, int length, IList<int> min, IList<int> max, IList<int> step = null) 119 119 : base(name) { 120 120 if (min.Count == 0) throw new ArgumentException("Bounds must be given for the integer parameters."); … … 163 163 #region Operator Discovery 164 164 private static readonly IEnumerable<Type> encodingSpecificOperatorTypes; 165 static Integer Encoding() {165 static IntegerVectorEncoding() { 166 166 encodingSpecificOperatorTypes = new List<Type>() { 167 167 typeof (IIntegerVectorOperator), -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/RealVectorEncoding.cs
r11879 r11885 33 33 34 34 namespace HeuristicLab.Problems.Programmable { 35 [Item("Real Encoding", "Describes a real vector encoding.")]35 [Item("RealVectorEncoding", "Describes a real vector encoding.")] 36 36 [StorableClass] 37 public sealed class Real Encoding : Encoding<IRealVectorCreator> {37 public sealed class RealVectorEncoding : Encoding<IRealVectorCreator> { 38 38 #region Encoding Parameters 39 39 [Storable] … … 78 78 79 79 [StorableConstructor] 80 private Real Encoding(bool deserializing) : base(deserializing) { }80 private RealVectorEncoding(bool deserializing) : base(deserializing) { } 81 81 [StorableHook(HookType.AfterDeserialization)] 82 82 private void AfterDeserialization() { … … 85 85 } 86 86 87 public override IDeepCloneable Clone(Cloner cloner) { return new Real Encoding(this, cloner); }88 private Real Encoding(RealEncoding original, Cloner cloner)87 public override IDeepCloneable Clone(Cloner cloner) { return new RealVectorEncoding(this, cloner); } 88 private RealVectorEncoding(RealVectorEncoding original, Cloner cloner) 89 89 : base(original, cloner) { 90 90 lengthParameter = cloner.Clone(original.lengthParameter); … … 93 93 } 94 94 95 public Real Encoding() : this("RealVector", 10) { }96 public Real Encoding(int length) : this("RealVector", length) { }97 public Real Encoding(string name, int length, double min = double.MinValue, double max = double.MaxValue)95 public RealVectorEncoding() : this("RealVector", 10) { } 96 public RealVectorEncoding(int length) : this("RealVector", length) { } 97 public RealVectorEncoding(string name, int length, double min = double.MinValue, double max = double.MaxValue) 98 98 : base(name) { 99 99 if (min >= max) throw new ArgumentException("min must be less than max", "min"); … … 113 113 } 114 114 115 public Real Encoding(string name, int length, IList<double> min, IList<double> max)115 public RealVectorEncoding(string name, int length, IList<double> min, IList<double> max) 116 116 : base(name) { 117 117 if (min.Count == 0) throw new ArgumentException("Bounds must be given for the real parameters."); … … 156 156 #region Operator Discovery 157 157 private static readonly IEnumerable<Type> encodingSpecificOperatorTypes; 158 static Real Encoding() {158 static RealVectorEncoding() { 159 159 encodingSpecificOperatorTypes = new List<Type>() { 160 160 typeof (IRealVectorOperator), -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/HeuristicLab.Problems.Programmable-3.3.csproj
r11814 r11885 131 131 <Compile Include="Individuals\MultiEncodingIndividual.cs" /> 132 132 <Compile Include="Encodings\PermutationEncoding.cs" /> 133 <Compile Include="Encodings\Real Encoding.cs" />134 <Compile Include="Encodings\Integer Encoding.cs" />135 <Compile Include="Encodings\Binary Encoding.cs" />133 <Compile Include="Encodings\RealVectorEncoding.cs" /> 134 <Compile Include="Encodings\IntegerVectorEncoding.cs" /> 135 <Compile Include="Encodings\BinaryVectorEncoding.cs" /> 136 136 <Compile Include="Encodings\MultiEncoding.cs" /> 137 137 <Compile Include="Encodings\Encoding.cs" /> … … 151 151 <Compile Include="New\MultiObjectiveBasicProblem.cs" /> 152 152 <Compile Include="New\MultiObjectiveProgrammableProblem.cs" /> 153 <Compile Include="New\Scripts\ProblemDefinitionScriptException.cs" /> 153 154 <Compile Include="New\Scripts\Templates\CompiledMultiObjectiveProblemDefinition.cs" /> 154 155 <Compile Include="New\Scripts\Templates\CompiledSingleObjectiveProblemDefinition.cs" /> -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Individuals/Individual.cs
r11813 r11885 43 43 44 44 public Individual Copy() { 45 return Copy (new Scope());45 return CopyToScope(new Scope()); 46 46 } 47 internal abstract Individual Copy(IScope scope); 47 48 public abstract Individual CopyToScope(IScope scope); 48 49 49 50 protected static IItem ExtractScopeValue(string name, IScope scope) { … … 64 65 public static class IndividualExtensionMethods { 65 66 public static BinaryVector BinaryVector(this Individual individual) { 66 var encoding = individual.GetEncoding<Binary Encoding>();67 var encoding = individual.GetEncoding<BinaryVectorEncoding>(); 67 68 return individual.BinaryVector(encoding.Name); 68 69 } … … 72 73 73 74 public static IntegerVector IntegerVector(this Individual individual) { 74 var encoding = individual.GetEncoding<Integer Encoding>();75 var encoding = individual.GetEncoding<IntegerVectorEncoding>(); 75 76 return individual.IntegerVector(encoding.Name); 76 77 } … … 88 89 89 90 public static RealVector RealVector(this Individual individual) { 90 var encoding = individual.GetEncoding<Real Encoding>();91 var encoding = individual.GetEncoding<RealVectorEncoding>(); 91 92 return individual.RealVector(encoding.Name); 92 93 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Individuals/MultiEncodingIndividual.cs
r11813 r11885 62 62 try { 63 63 encoding = (TEncoding)Encoding.Encodings.SingleOrDefault(e => e is TEncoding); 64 } 65 catch (InvalidOperationException) { 64 } catch (InvalidOperationException) { 66 65 throw new InvalidOperationException(string.Format("The individual uses multiple {0} .", typeof(TEncoding).GetPrettyName())); 67 66 } … … 70 69 } 71 70 72 internal override Individual Copy(IScope scope) {73 var copies = individuals.Select(i => i.Copy (scope)).ToArray();71 public override Individual CopyToScope(IScope scope) { 72 var copies = individuals.Select(i => i.CopyToScope(scope)).ToArray(); 74 73 return new MultiEncodingIndividual(Encoding, scope, copies); 75 74 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Individuals/SingleEncodingIndividual.cs
r11813 r11885 49 49 } 50 50 51 internal override Individual Copy(IScope scope) {51 public override Individual CopyToScope(IScope scope) { 52 52 SetScopeValue(Name, scope, (IItem)this[Name].Clone()); 53 53 return new SingleEncodingIndividual(Encoding, scope); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IMultiEncodingOperator.cs
r11797 r11885 26 26 void AddEncoding(IEncoding encoding); 27 27 bool RemoveEncoding(IEncoding encoding); 28 void ReplaceEncoding(IEncoding oldEncoding, IEncoding newEncoding);29 28 } 30 29 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/BasicProblem.cs
r11880 r11885 126 126 foreach (var multi in nestedEncodings) 127 127 AdaptEncodingOperators(multi.oldEnc, multi.newEnc); 128 129 foreach (var op in oldMultiEncoding.Operators.OfType<IMultiEncodingOperator>())130 foreach (var multi in nestedEncodings)131 op.ReplaceEncoding(multi.oldEnc, multi.newEnc);132 128 } 133 129 … … 137 133 var newOperators = newEncoding.Operators; 138 134 139 140 var operators = oldOperators.Intersect(newOperators, comparer).Select(op => cloner.Clone(op)); 141 operators = operators.Union(newOperators, comparer).ToList(); 135 cloner.RegisterClonedObject(oldEncoding, newEncoding); 136 var operators = oldOperators.Intersect(newOperators, comparer) 137 .Select(cloner.Clone) 138 .Union(newOperators, comparer).ToList(); 142 139 143 140 newEncoding.ConfigureOperators(operators); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/OneMaxNew.cs
r11880 r11885 32 32 [Creatable("1 Test")] 33 33 [StorableClass] 34 public class OneMaxNew : SingleObjectiveBasicProblem<Binary Encoding> {34 public class OneMaxNew : SingleObjectiveBasicProblem<BinaryVectorEncoding> { 35 35 public override bool Maximization { 36 36 get { return true; } … … 39 39 public OneMaxNew() 40 40 : base() { 41 Encoding = new Binary Encoding("BinaryVector", 10);41 Encoding = new BinaryVectorEncoding("BinaryVector", 10); 42 42 var bestScopeSolutionAnalyzer = Operators.OfType<BestScopeSolutionAnalyzer>().FirstOrDefault(); 43 43 if (bestScopeSolutionAnalyzer != null) Operators.Remove(bestScopeSolutionAnalyzer); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/ProblemDefinitionScript.cs
r11880 r11885 71 71 var assembly = base.Compile(); 72 72 var types = assembly.GetTypes(); 73 if (!types.Any(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x))) 74 throw new ProblemDefinitionScriptException("The compiled code doesn't contain a problem definition." + Environment.NewLine + "The problem definition must be a subclass of CompiledProblemDefinition."); 75 if (types.Count(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)) > 1) 76 throw new ProblemDefinitionScriptException("The compiled code contains multiple problem definitions." + Environment.NewLine + "Only one subclass of CompiledProblemDefinition is allowed."); 77 78 CompiledProblemDefinition inst; 73 79 try { 74 var inst = (CompiledProblemDefinition)Activator.CreateInstance(types.First(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x))); 80 inst = (CompiledProblemDefinition)Activator.CreateInstance(types.Single(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x))); 81 } catch (Exception e) { 82 compiledProblemDefinition = null; 83 throw new ProblemDefinitionScriptException("Instantiating the problem definition failed." + Environment.NewLine + "Check your default constructor.", e); 84 } 85 86 try { 75 87 inst.vars = new Variables(VariableStore); 76 88 inst.Initialize(); 89 } catch (Exception e) { 90 compiledProblemDefinition = null; 91 throw new ProblemDefinitionScriptException("Initializing the problem definition failed." + Environment.NewLine + "Check your Initialize() method.", e); 92 } 93 94 try { 77 95 CompiledProblemDefinition = inst; 78 96 } catch (Exception e) { 79 97 compiledProblemDefinition = null; 80 throw ;98 throw new ProblemDefinitionScriptException("Using the problem definition in the problem failed." + Environment.NewLine + "Examine this error message carefully (often there is an issue with the defined encoding).", e); 81 99 } 100 82 101 return assembly; 83 102 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/SingleObjectiveProgrammableProblem.cs
r11880 r11885 51 51 try { 52 52 ProblemScript.Compile(); 53 } catch ( InvalidOperationException) {54 //Compilation error53 } catch (ProblemDefinitionScriptException e) { 54 PluginInfrastructure.ErrorHandling.ShowErrorDialog(e); 55 55 } 56 56 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiEncodingOperator.cs
r11797 r11885 81 81 } 82 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;91 }92 93 83 protected IConstrainedValueParameter<T> GetParameter(IEncoding encoding) { 94 84 if (!Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name)); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveImprover.cs
r11880 r11885 110 110 LocalEvaluatedSolutionsParameter.ActualValue = new IntValue(count); 111 111 QualityParameter.ActualValue = new DoubleValue(quality); 112 individual.Copy (ExecutionContext.Scope);112 individual.CopyToScope(ExecutionContext.Scope); 113 113 return base.Apply(); 114 114 } -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveGenerator.cs
r11753 r11885 74 74 for (int i = 0; i < moveScopes.Length; i++) { 75 75 moveScopes[i] = new Scope(i.ToString(CultureInfo.InvariantCulture.NumberFormat)); 76 nbhood[i].Copy (moveScopes[i]);76 nbhood[i].CopyToScope(moveScopes[i]); 77 77 } 78 78 ExecutionContext.Scope.SubScopes.AddRange(moveScopes); -
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveMaker.cs
r11753 r11885 63 63 var encoding = EncodingParameter.ActualValue; 64 64 var individual = encoding.GetIndividual(ExecutionContext.Scope); 65 individual.Copy (ExecutionContext.Scope.Parent.Parent);65 individual.CopyToScope(ExecutionContext.Scope.Parent.Parent); 66 66 67 67 if (QualityParameter.ActualValue == null) QualityParameter.ActualValue = new DoubleValue(MoveQualityParameter.ActualValue.Value);
Note: See TracChangeset
for help on using the changeset viewer.