Changeset 11885


Ignore:
Timestamp:
02/04/15 13:28:37 (7 years ago)
Author:
abeham
Message:

#2174:

  • Some refactorings and bug fixes
  • Renamed (Binary|Integer|Real)Encoding to (Binary|Integer|Real)VectorEncoding
  • Improved error messages when compiling programmable problems
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  
    2020#endregion
    2121
     22using System;
     23using System.Drawing;
    2224using HeuristicLab.MainForm;
    2325using HeuristicLab.Scripting.Views;
     
    4648      variableStoreView.Enabled = Content != null && !Locked && !ReadOnly;
    4749    }
     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    }
    4860  }
    4961}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/BinaryVectorEncoding.cs

    r11879 r11885  
    3232
    3333namespace HeuristicLab.Problems.Programmable {
    34   [Item("BinaryEncoding", "Describes a binary vector encoding.")]
     34  [Item("BinaryVectorEncoding", "Describes a binary vector encoding.")]
    3535  [StorableClass]
    36   public sealed class BinaryEncoding : Encoding<IBinaryVectorCreator> {
     36  public sealed class BinaryVectorEncoding : Encoding<IBinaryVectorCreator> {
    3737    #region Encoding Parameters
    3838    [Storable]
     
    5959
    6060    [StorableConstructor]
    61     private BinaryEncoding(bool deserializing) : base(deserializing) { }
     61    private BinaryVectorEncoding(bool deserializing) : base(deserializing) { }
    6262    [StorableHook(HookType.AfterDeserialization)]
    6363    private void AfterDeserialization() {
     
    6565      DiscoverOperators();
    6666    }
    67     public override IDeepCloneable Clone(Cloner cloner) { return new BinaryEncoding(this, cloner); }
    68     private BinaryEncoding(BinaryEncoding original, Cloner cloner)
     67    public override IDeepCloneable Clone(Cloner cloner) { return new BinaryVectorEncoding(this, cloner); }
     68    private BinaryVectorEncoding(BinaryVectorEncoding original, Cloner cloner)
    6969      : base(original, cloner) {
    7070      lengthParameter = cloner.Clone(original.lengthParameter);
     
    7272    }
    7373
    74     public BinaryEncoding() : this("BinaryVector", 10) { }
    75     public BinaryEncoding(int length) : this("BinaryVector", length) { }
    76     public BinaryEncoding(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)
    7777      : base(name) {
    7878      lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
     
    9898    #region Operator Discovery
    9999    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
    100     static BinaryEncoding() {
     100    static BinaryVectorEncoding() {
    101101      encodingSpecificOperatorTypes = new List<Type>() {
    102102        typeof (IBinaryVectorOperator),
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/IntegerVectorEncoding.cs

    r11879 r11885  
    3333
    3434namespace HeuristicLab.Problems.Programmable {
    35   [Item("IntegerEncoding", "Describes an integer vector encoding.")]
     35  [Item("IntegerVectorEncoding", "Describes an integer vector encoding.")]
    3636  [StorableClass]
    37   public sealed class IntegerEncoding : Encoding<IIntegerVectorCreator> {
     37  public sealed class IntegerVectorEncoding : Encoding<IIntegerVectorCreator> {
    3838    #region Encoding Parameters
    3939    [Storable]
     
    7979
    8080    [StorableConstructor]
    81     private IntegerEncoding(bool deserializing) : base(deserializing) { }
     81    private IntegerVectorEncoding(bool deserializing) : base(deserializing) { }
    8282    [StorableHook(HookType.AfterDeserialization)]
    8383    private void AfterDeserialization() {
     
    8686    }
    8787
    88     private IntegerEncoding(IntegerEncoding original, Cloner cloner)
     88    private IntegerVectorEncoding(IntegerVectorEncoding original, Cloner cloner)
    8989      : base(original, cloner) {
    9090      lengthParameter = cloner.Clone(original.lengthParameter);
     
    9292      RegisterParameterEvents();
    9393    }
    94     public override IDeepCloneable Clone(Cloner cloner) { return new IntegerEncoding(this, cloner); }
    95 
    96 
    97     public IntegerEncoding() : this("IntegerVector", 10) { }
    98     public IntegerEncoding(int length) : this("integerVector", length) { }
    99     public IntegerEncoding(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)
    100100      : base(name) {
    101101      if (min >= max) throw new ArgumentException("min must be less than max", "min");
     
    116116      DiscoverOperators();
    117117    }
    118     public IntegerEncoding(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)
    119119      : base(name) {
    120120      if (min.Count == 0) throw new ArgumentException("Bounds must be given for the integer parameters.");
     
    163163    #region Operator Discovery
    164164    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
    165     static IntegerEncoding() {
     165    static IntegerVectorEncoding() {
    166166      encodingSpecificOperatorTypes = new List<Type>() {
    167167        typeof (IIntegerVectorOperator),
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/RealVectorEncoding.cs

    r11879 r11885  
    3333
    3434namespace HeuristicLab.Problems.Programmable {
    35   [Item("RealEncoding", "Describes a real vector encoding.")]
     35  [Item("RealVectorEncoding", "Describes a real vector encoding.")]
    3636  [StorableClass]
    37   public sealed class RealEncoding : Encoding<IRealVectorCreator> {
     37  public sealed class RealVectorEncoding : Encoding<IRealVectorCreator> {
    3838    #region Encoding Parameters
    3939    [Storable]
     
    7878
    7979    [StorableConstructor]
    80     private RealEncoding(bool deserializing) : base(deserializing) { }
     80    private RealVectorEncoding(bool deserializing) : base(deserializing) { }
    8181    [StorableHook(HookType.AfterDeserialization)]
    8282    private void AfterDeserialization() {
     
    8585    }
    8686
    87     public override IDeepCloneable Clone(Cloner cloner) { return new RealEncoding(this, cloner); }
    88     private RealEncoding(RealEncoding original, Cloner cloner)
     87    public override IDeepCloneable Clone(Cloner cloner) { return new RealVectorEncoding(this, cloner); }
     88    private RealVectorEncoding(RealVectorEncoding original, Cloner cloner)
    8989      : base(original, cloner) {
    9090      lengthParameter = cloner.Clone(original.lengthParameter);
     
    9393    }
    9494
    95     public RealEncoding() : this("RealVector", 10) { }
    96     public RealEncoding(int length) : this("RealVector", length) { }
    97     public RealEncoding(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)
    9898      : base(name) {
    9999      if (min >= max) throw new ArgumentException("min must be less than max", "min");
     
    113113    }
    114114
    115     public RealEncoding(string name, int length, IList<double> min, IList<double> max)
     115    public RealVectorEncoding(string name, int length, IList<double> min, IList<double> max)
    116116      : base(name) {
    117117      if (min.Count == 0) throw new ArgumentException("Bounds must be given for the real parameters.");
     
    156156    #region Operator Discovery
    157157    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
    158     static RealEncoding() {
     158    static RealVectorEncoding() {
    159159      encodingSpecificOperatorTypes = new List<Type>() {
    160160          typeof (IRealVectorOperator),
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/HeuristicLab.Problems.Programmable-3.3.csproj

    r11814 r11885  
    131131    <Compile Include="Individuals\MultiEncodingIndividual.cs" />
    132132    <Compile Include="Encodings\PermutationEncoding.cs" />
    133     <Compile Include="Encodings\RealEncoding.cs" />
    134     <Compile Include="Encodings\IntegerEncoding.cs" />
    135     <Compile Include="Encodings\BinaryEncoding.cs" />
     133    <Compile Include="Encodings\RealVectorEncoding.cs" />
     134    <Compile Include="Encodings\IntegerVectorEncoding.cs" />
     135    <Compile Include="Encodings\BinaryVectorEncoding.cs" />
    136136    <Compile Include="Encodings\MultiEncoding.cs" />
    137137    <Compile Include="Encodings\Encoding.cs" />
     
    151151    <Compile Include="New\MultiObjectiveBasicProblem.cs" />
    152152    <Compile Include="New\MultiObjectiveProgrammableProblem.cs" />
     153    <Compile Include="New\Scripts\ProblemDefinitionScriptException.cs" />
    153154    <Compile Include="New\Scripts\Templates\CompiledMultiObjectiveProblemDefinition.cs" />
    154155    <Compile Include="New\Scripts\Templates\CompiledSingleObjectiveProblemDefinition.cs" />
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Individuals/Individual.cs

    r11813 r11885  
    4343
    4444    public Individual Copy() {
    45       return Copy(new Scope());
     45      return CopyToScope(new Scope());
    4646    }
    47     internal abstract Individual Copy(IScope scope);
     47
     48    public abstract Individual CopyToScope(IScope scope);
    4849
    4950    protected static IItem ExtractScopeValue(string name, IScope scope) {
     
    6465  public static class IndividualExtensionMethods {
    6566    public static BinaryVector BinaryVector(this Individual individual) {
    66       var encoding = individual.GetEncoding<BinaryEncoding>();
     67      var encoding = individual.GetEncoding<BinaryVectorEncoding>();
    6768      return individual.BinaryVector(encoding.Name);
    6869    }
     
    7273
    7374    public static IntegerVector IntegerVector(this Individual individual) {
    74       var encoding = individual.GetEncoding<IntegerEncoding>();
     75      var encoding = individual.GetEncoding<IntegerVectorEncoding>();
    7576      return individual.IntegerVector(encoding.Name);
    7677    }
     
    8889
    8990    public static RealVector RealVector(this Individual individual) {
    90       var encoding = individual.GetEncoding<RealEncoding>();
     91      var encoding = individual.GetEncoding<RealVectorEncoding>();
    9192      return individual.RealVector(encoding.Name);
    9293    }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Individuals/MultiEncodingIndividual.cs

    r11813 r11885  
    6262      try {
    6363        encoding = (TEncoding)Encoding.Encodings.SingleOrDefault(e => e is TEncoding);
    64       }
    65       catch (InvalidOperationException) {
     64      } catch (InvalidOperationException) {
    6665        throw new InvalidOperationException(string.Format("The individual uses multiple {0} .", typeof(TEncoding).GetPrettyName()));
    6766      }
     
    7069    }
    7170
    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();
    7473      return new MultiEncodingIndividual(Encoding, scope, copies);
    7574    }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Individuals/SingleEncodingIndividual.cs

    r11813 r11885  
    4949    }
    5050
    51     internal override Individual Copy(IScope scope) {
     51    public override Individual CopyToScope(IScope scope) {
    5252      SetScopeValue(Name, scope, (IItem)this[Name].Clone());
    5353      return new SingleEncodingIndividual(Encoding, scope);
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IMultiEncodingOperator.cs

    r11797 r11885  
    2626    void AddEncoding(IEncoding encoding);
    2727    bool RemoveEncoding(IEncoding encoding);
    28     void ReplaceEncoding(IEncoding oldEncoding, IEncoding newEncoding);
    2928  }
    3029}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/BasicProblem.cs

    r11880 r11885  
    126126        foreach (var multi in nestedEncodings)
    127127          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);
    132128      }
    133129
     
    137133      var newOperators = newEncoding.Operators;
    138134
    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();
    142139
    143140      newEncoding.ConfigureOperators(operators);
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/OneMaxNew.cs

    r11880 r11885  
    3232  [Creatable("1 Test")]
    3333  [StorableClass]
    34   public class OneMaxNew : SingleObjectiveBasicProblem<BinaryEncoding> {
     34  public class OneMaxNew : SingleObjectiveBasicProblem<BinaryVectorEncoding> {
    3535    public override bool Maximization {
    3636      get { return true; }
     
    3939    public OneMaxNew()
    4040      : base() {
    41       Encoding = new BinaryEncoding("BinaryVector", 10);
     41      Encoding = new BinaryVectorEncoding("BinaryVector", 10);
    4242      var bestScopeSolutionAnalyzer = Operators.OfType<BestScopeSolutionAnalyzer>().FirstOrDefault();
    4343      if (bestScopeSolutionAnalyzer != null) Operators.Remove(bestScopeSolutionAnalyzer);
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/ProblemDefinitionScript.cs

    r11880 r11885  
    7171      var assembly = base.Compile();
    7272      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;
    7379      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 {
    7587        inst.vars = new Variables(VariableStore);
    7688        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 {
    7795        CompiledProblemDefinition = inst;
    7896      } catch (Exception e) {
    7997        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);
    8199      }
     100
    82101      return assembly;
    83102    }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/SingleObjectiveProgrammableProblem.cs

    r11880 r11885  
    5151      try {
    5252        ProblemScript.Compile();
    53       } catch (InvalidOperationException) {
    54         //Compilation error
     53      } catch (ProblemDefinitionScriptException e) {
     54        PluginInfrastructure.ErrorHandling.ShowErrorDialog(e);
    5555      }
    5656    }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiEncodingOperator.cs

    r11797 r11885  
    8181    }
    8282
    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 
    9383    protected IConstrainedValueParameter<T> GetParameter(IEncoding encoding) {
    9484      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  
    110110      LocalEvaluatedSolutionsParameter.ActualValue = new IntValue(count);
    111111      QualityParameter.ActualValue = new DoubleValue(quality);
    112       individual.Copy(ExecutionContext.Scope);
     112      individual.CopyToScope(ExecutionContext.Scope);
    113113      return base.Apply();
    114114    }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveGenerator.cs

    r11753 r11885  
    7474      for (int i = 0; i < moveScopes.Length; i++) {
    7575        moveScopes[i] = new Scope(i.ToString(CultureInfo.InvariantCulture.NumberFormat));
    76         nbhood[i].Copy(moveScopes[i]);
     76        nbhood[i].CopyToScope(moveScopes[i]);
    7777      }
    7878      ExecutionContext.Scope.SubScopes.AddRange(moveScopes);
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveMaker.cs

    r11753 r11885  
    6363      var encoding = EncodingParameter.ActualValue;
    6464      var individual = encoding.GetIndividual(ExecutionContext.Scope);
    65       individual.Copy(ExecutionContext.Scope.Parent.Parent);
     65      individual.CopyToScope(ExecutionContext.Scope.Parent.Parent);
    6666
    6767      if (QualityParameter.ActualValue == null) QualityParameter.ActualValue = new DoubleValue(MoveQualityParameter.ActualValue.Value);
Note: See TracChangeset for help on using the changeset viewer.