Changeset 11753


Ignore:
Timestamp:
01/13/15 18:47:19 (5 years ago)
Author:
mkommend
Message:

#2174: First working version of refactored programmable problem.

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  
    7878
    7979    public U SolutionCreator {
    80       get { return SolutionCreatorParameter.Value; }
     80      get { return (U)SolutionCreatorParameter.Value; }
    8181      protected set { SolutionCreatorParameter.Value = value; }
    8282    }
    83     public ValueParameter<U> SolutionCreatorParameter {
    84       get { return (ValueParameter<U>)Parameters[SolutionCreateParameterName]; }
     83    public IValueParameter SolutionCreatorParameter {
     84      get { return (IValueParameter)Parameters[SolutionCreateParameterName]; }
    8585    }
    8686    ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator { get { return SolutionCreator; } }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable.Views/3.3/HeuristicLab.Problems.Programmable.Views-3.3.csproj

    r11655 r11753  
    111111      <DependentUpon>ProblemDefinitionScriptView.cs</DependentUpon>
    112112    </Compile>
    113     <Compile Include="MultiObjectiveProgrammableProblemView.cs">
     113    <Compile Include="MultiObjectiveScriptableProblemView.cs">
    114114      <SubType>UserControl</SubType>
    115115    </Compile>
    116     <Compile Include="MultiObjectiveProgrammableProblemView.Designer.cs">
    117       <DependentUpon>MultiObjectiveProgrammableProblemView.cs</DependentUpon>
     116    <Compile Include="MultiObjectiveScriptableProblemView.Designer.cs">
     117      <DependentUpon>MultiObjectiveScriptableProblemView.cs</DependentUpon>
    118118    </Compile>
    119     <Compile Include="SingleObjectiveProgrammableProblemView.cs">
     119    <Compile Include="SingleObjectiveScriptableProblemView.cs">
    120120      <SubType>UserControl</SubType>
    121121    </Compile>
    122     <Compile Include="SingleObjectiveProgrammableProblemView.Designer.cs">
    123       <DependentUpon>SingleObjectiveProgrammableProblemView.cs</DependentUpon>
     122    <Compile Include="SingleObjectiveScriptableProblemView.Designer.cs">
     123      <DependentUpon>SingleObjectiveScriptableProblemView.cs</DependentUpon>
    124124    </Compile>
    125125    <Compile Include="Properties\AssemblyInfo.cs" />
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable.Views/3.3/MultiObjectiveScriptableProblemView.Designer.cs

    r11745 r11753  
    2121
    2222namespace HeuristicLab.Problems.Programmable.Views {
    23   partial class MultiObjectiveProgrammableProblemView {
     23  partial class MultiObjectiveScriptableProblemView {
    2424    /// <summary>
    2525    /// Required designer variable.
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable.Views/3.3/MultiObjectiveScriptableProblemView.cs

    r11741 r11753  
    2828namespace HeuristicLab.Problems.Programmable.Views {
    2929  [View("Multi-Objective Programmable Problem View")]
    30   [Content(typeof(MultiObjectiveProgrammableProblem), true)]
    31   public partial class MultiObjectiveProgrammableProblemView : ItemView {
    32     protected ViewHost DefinitionView;
     30  [Content(typeof(MultiObjectiveScriptableProblem), true)]
     31  public partial class MultiObjectiveScriptableProblemView : ItemView {
     32    protected ViewHost ScriptView;
    3333
    34     public new MultiObjectiveProgrammableProblem Content {
    35       get { return (MultiObjectiveProgrammableProblem)base.Content; }
     34    public new MultiObjectiveScriptableProblem Content {
     35      get { return (MultiObjectiveScriptableProblem)base.Content; }
    3636      set { base.Content = value; }
    3737    }
    3838
    39     public MultiObjectiveProgrammableProblemView() {
     39    public MultiObjectiveScriptableProblemView() {
    4040      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);
    4343    }
    4444
     
    4646      base.OnContentChanged();
    4747      if (Content == null) {
    48         DefinitionView.Content = null;
     48        ScriptView.Content = null;
    4949      } else {
    50         DefinitionView.Content = Content.ProblemDefinitionParameter.Value;
     50        ScriptView.Content = Content.ProblemScript;
    5151        Caption = Content.Name;
    5252      }
     
    5454
    5555    protected override void RegisterContentEvents() {
    56       Content.ProblemDefinitionParameter.ValueChanged += ProblemDefinitionParameterOnValueChanged;
     56      //Content.ProblemDefinitionParameter.ValueChanged += ProblemDefinitionParameterOnValueChanged;
    5757      Content.NameChanged += ContentOnNameChanged;
    5858      base.RegisterContentEvents();
     
    6161    protected override void DeregisterContentEvents() {
    6262      base.DeregisterContentEvents();
    63       Content.ProblemDefinitionParameter.ValueChanged -= ProblemDefinitionParameterOnValueChanged;
     63      //Content.ProblemDefinitionParameter.ValueChanged -= ProblemDefinitionParameterOnValueChanged;
    6464      Content.NameChanged -= ContentOnNameChanged;
    6565    }
    6666
    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    //}
    7070
    7171    private void ContentOnNameChanged(object sender, EventArgs eventArgs) {
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable.Views/3.3/ProblemDefinitionScriptView.cs

    r11484 r11753  
    2525namespace HeuristicLab.Problems.Programmable.Views {
    2626  [View("ProblemDefinitionScriptView")]
    27   [Content(typeof(ProblemScript), IsDefaultView = true)]
     27  [Content(typeof(ProblemDefinitionScript), IsDefaultView = true)]
    2828  public partial class ProblemDefinitionScriptView : ScriptView {
    2929
    30     public new ProblemScript Content {
    31       get { return (ProblemScript)base.Content; }
     30    public new ProblemDefinitionScript Content {
     31      get { return (ProblemDefinitionScript)base.Content; }
    3232      set { base.Content = value; }
    3333    }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable.Views/3.3/SingleObjectiveScriptableProblemView.Designer.cs

    r11741 r11753  
    2121
    2222namespace HeuristicLab.Problems.Programmable.Views {
    23   partial class SingleObjectiveProgrammableProblemView {
     23  partial class SingleObjectiveScriptableProblemView {
    2424    /// <summary>
    2525    /// Required designer variable.
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable.Views/3.3/SingleObjectiveScriptableProblemView.cs

    r11741 r11753  
    2525using HeuristicLab.MainForm;
    2626using HeuristicLab.MainForm.WindowsForms;
     27using HeuristicLab.Problems.Programmable.Problems;
    2728
    2829namespace HeuristicLab.Problems.Programmable.Views {
    29   [View("Single-Objective Programmable Problem View")]
    30   [Content(typeof(SingleObjectiveProgrammableProblem), true)]
    31   public partial class SingleObjectiveProgrammableProblemView : 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;
    3334
    34     public new SingleObjectiveProgrammableProblem Content {
    35       get { return (SingleObjectiveProgrammableProblem)base.Content; }
     35    public new SingleObjectiveScriptableProblem Content {
     36      get { return (SingleObjectiveScriptableProblem)base.Content; }
    3637      set { base.Content = value; }
    3738    }
    3839
    39     public SingleObjectiveProgrammableProblemView() {
     40    public SingleObjectiveScriptableProblemView() {
    4041      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);
    4344    }
    4445
     
    4647      base.OnContentChanged();
    4748      if (Content == null) {
    48         DefinitionView.Content = null;
     49        ScriptView.Content = null;
    4950      } else {
    50         DefinitionView.Content = Content.ProblemDefinitionParameter.Value;
     51        ScriptView.Content = Content.ProblemScript;
    5152        Caption = Content.Name;
    5253      }
     
    5455
    5556    protected override void RegisterContentEvents() {
    56       Content.ProblemDefinitionParameter.ValueChanged += ProblemDefinitionParameterOnValueChanged;
     57      //Content.ProblemDefinitionParameter.ValueChanged += ProblemDefinitionParameterOnValueChanged;
    5758      Content.NameChanged += ContentOnNameChanged;
    5859      base.RegisterContentEvents();
     
    6162    protected override void DeregisterContentEvents() {
    6263      base.DeregisterContentEvents();
    63       Content.ProblemDefinitionParameter.ValueChanged -= ProblemDefinitionParameterOnValueChanged;
     64      //Content.ProblemDefinitionParameter.ValueChanged -= ProblemDefinitionParameterOnValueChanged;
    6465      Content.NameChanged -= ContentOnNameChanged;
    6566    }
    6667
    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    //}
    7071
    7172    private void ContentOnNameChanged(object sender, EventArgs eventArgs) {
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Encoding.cs

    r11737 r11753  
    3838
    3939    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
    4147    public IEnumerable<IOperator> Operators {
    4248      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      }
    4459    }
    4560
     
    90105      if (handler != null) handler(this, EventArgs.Empty);
    91106    }
     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    }
    92114  }
    93115}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/IntegerEncoding.cs

    r11746 r11753  
    174174    }
    175175    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);
    177178      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
    178179      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/MultiEncoding.cs

    r11619 r11753  
    8989
    9090    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
    91 
    9291    }
    9392  }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/PermutationEncoding.cs

    r11746 r11753  
    148148    }
    149149    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);
    151152      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
    152153      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/RealEncoding.cs

    r11739 r11753  
    173173    }
    174174    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);
    176177      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
    177178      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/HeuristicLab.Problems.Programmable-3.3.csproj

    r11740 r11753  
    147147    <Compile Include="Interfaces\IMultiObjectiveProblemDefinition.cs" />
    148148    <Compile Include="Interfaces\IEncodingOperator.cs" />
     149    <Compile Include="Interfaces\ISingleObjectiveMoveOperator.cs" />
    149150    <Compile Include="New\ProgrammableProblem.cs" />
    150151    <Compile Include="New\MultiObjectiveProgrammableProblem.cs" />
    151152    <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>
    152160    <Compile Include="Operators\MultiEncodingManipulator.cs" />
    153161    <Compile Include="Operators\MultiEncodingCrossover.cs" />
     
    173181    <Compile Include="OLD_MultiObjectiveProgrammableProblem.cs" />
    174182    <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>
    175187    <None Include="HeuristicLab.snk" />
    176188    <None Include="Plugin.cs.frame" />
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IEncoding.cs

    r11737 r11753  
    2828  public interface IEncoding : IParameterizedNamedItem {
    2929    ISolutionCreator SolutionCreator { get; set; }
    30 
    31     IEnumerable<IOperator> Operators { get; }
     30    IEnumerable<IOperator> Operators { get; set; }
    3231
    3332    Individual GetIndividual(IScope scope);
     
    3635
    3736    event EventHandler SolutionCreatorChanged;
     37    event EventHandler OperatorsChanged;
    3838  }
    3939}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IMultiEncodingOperator.cs

    r11587 r11753  
    2626    void AddEncoding(IEncoding encoding);
    2727    bool RemoveEncoding(IEncoding encoding);
     28    void ReplaceEncoding(IEncoding oldEncoding, IEncoding newEncoding);
    2829  }
    2930}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/IProblemDefinition.cs

    r11739 r11753  
    2525namespace HeuristicLab.Problems.Programmable {
    2626  public interface IProblemDefinition : INamedItem {
    27     IEncoding Encoding { get; }
    28     IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random);
     27    IEncoding Encoding { get; }   
    2928  }
    3029}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Interfaces/ISingleObjectiveProblemDefinition.cs

    r11739 r11753  
    2020#endregion
    2121
     22using System.Collections.Generic;
    2223using HeuristicLab.Core;
    2324using HeuristicLab.Optimization;
     
    2829    double Evaluate(Individual individual, IRandom random);
    2930    void Analyze(Individual[] individuals, double[] qualities, ResultCollection results);
     31    IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random);
    3032  }
    3133}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/MultiObjectiveProgrammableProblem.cs

    r11740 r11753  
    4242      : base() {
    4343
     44      Operators.Add(Evaluator);
    4445      Operators.Add(new BestScopeSolutionAnalyzer());
    45       Operators.Add(new SingleObjectiveEvaluator());
    4646      Operators.Add(new SingleObjectiveAnalyzer());
    4747
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/MultiObjectiveScriptableProblem.cs

    r11740 r11753  
    2727using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2828
    29 namespace HeuristicLab.Problems.Programmable.Problems {
     29namespace HeuristicLab.Problems.Programmable {
    3030  [Item("Scriptable Problem (multi-objective)", "Represents a multi-objective problem that can be scripted.")]
    3131  [Creatable("1 Test")]
     
    3737    }
    3838
    39     private MultiObjectiveProblemDefinitionScript ProblemScript {
     39    public MultiObjectiveProblemDefinitionScript ProblemScript {
    4040      get { return MultiObjectiveProblemScriptParameter.Value; }
    4141    }
    4242
    43     private IMultiObjectiveProblemDefinition ProblemDefinition {
     43    public IMultiObjectiveProblemDefinition ProblemDefinition {
    4444      get { return MultiObjectiveProblemScriptParameter.Value; }
    4545    }
     
    4848      : base(original, cloner) {
    4949      RegisterEvents();
     50      ProblemScript.Compile();
    5051    }
    5152    public override IDeepCloneable Clone(Cloner cloner) { return new MultiObjectiveScriptableProblem(this, cloner); }
     
    6465    private void AfterDeserialization() {
    6566      RegisterEvents();
     67      ProblemScript.Compile();
    6668    }
    6769
     
    7173
    7274    private void OnProblemDefinitionChanged() {
     75      Encoding = ProblemDefinition.Encoding;
     76      OnOperatorsChanged();
     77      OnReset();
    7378    }
    7479
     
    8489      ProblemDefinition.Analyze(individuals, qualities, results);
    8590    }
    86     public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
    87       return ProblemDefinition.GetNeighbors(individual, random);
    88     }
    8991  }
    9092}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/ProgrammableProblem.cs

    r11739 r11753  
    2828using HeuristicLab.Parameters;
    2929using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     30using HeuristicLab.Problems.Programmable.Interfaces;
    3031
    3132namespace HeuristicLab.Problems.Programmable {
     
    4647      protected set {
    4748        if (value == null) throw new ArgumentNullException("Encoding must not be null.");
    48         if (Encoding != null) Parameters.RemoveRange(Encoding.Parameters);
    4949        EncodingParameter.Value = value;
    50         Parameters.AddRange(Encoding.Parameters);
    5150      }
    5251    }
     52    //mkommend necessary for reuse of operators if the encoding changes
     53    private TEncoding oldEncoding;
    5354
    5455    public virtual IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
     
    5758
    5859    protected override IEnumerable<IItem> GetOperators() {
     60      if (Encoding == null) return base.GetOperators();
    5961      return base.GetOperators().Concat(Encoding.Operators);
    6062    }
    6163    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      }
    6368    }
    6469
    65     public ProgrammableProblem()
     70    protected ProgrammableProblem()
    6671      : base() {
    6772      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;
    6874      RegisterEvents();
    6975    }
     
    7177    protected ProgrammableProblem(ProgrammableProblem<TEncoding, TEvaluator> original, Cloner cloner)
    7278      : base(original, cloner) {
     79      oldEncoding = cloner.Clone(original.oldEncoding);
    7380      RegisterEvents();
    7481    }
    7582
    7683    [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;
    7988      RegisterEvents();
    8089    }
     
    8594
    8695    protected virtual void OnEncodingChanged() {
     96      if (oldEncoding != null) AdaptEncodingOperators(oldEncoding, Encoding);
     97      oldEncoding = Encoding;
     98
    8799      var solutionCreatorType = Encoding.GetType().BaseType.GetGenericArguments();
    88100      var paramType = typeof(ValueParameter<>).MakeGenericType(solutionCreatorType);
     
    91103      Parameters.Remove(SolutionCreatorParameter);
    92104      Parameters.Add(solutionCreatorParam);
     105
     106      OnOperatorsChanged();
     107      OnReset();
    93108    }
    94109
     
    106121    }
    107122
     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
    108151  }
    109152}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/CompiledProblemDefinition.cs

    r11739 r11753  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using HeuristicLab.Common;
    2524using HeuristicLab.Core;
     
    5251    }
    5352
     53    public CompiledProblemDefinition() { }
    5454    protected CompiledProblemDefinition(IEncoding encoding) : this(encoding, "ProblemDefinition") { }
    5555    protected CompiledProblemDefinition(IEncoding encoding, string name) : this(encoding, name, string.Empty) { }
     
    5757      : base(name, description) {
    5858      Encoding = encoding;
    59       //TODO call initialize and set refactor ctors
    6059    }
    61 
    62     public abstract IEnumerable<Individual> GetNeighbors(Individual vector, IRandom random);
    6360  }
    6461}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/MultiObjectiveProblemDefinitionScript.cs

    r11739 r11753  
    2424using HeuristicLab.Optimization;
    2525using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     26using HeuristicLab.Problems.Programmable.New.Scripts.Templates;
    2627
    2728namespace HeuristicLab.Problems.Programmable {
     
    3233
    3334    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    }
    4437
    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; }
    9540    }
    9641
     
    10853    }
    10954
    110     public new IMultiObjectiveProblemDefinition CompiledProblemDefinition {
    111       get { return (IMultiObjectiveProblemDefinition)base.CompiledProblemDefinition; }
    112     }
    113 
    11455    bool[] IMultiObjectiveProblemDefinition.Maximization {
    115       get { return CompiledProblemDefinition != null ? CompiledProblemDefinition.Maximization : new bool[0]; }
     56      get { return CompiledProblemDefinition.Maximization; }
    11657    }
    11758
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/ProblemDefinitionScript.cs

    r11739 r11753  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Linq;
    2524using System.Reflection;
     
    4746      variableStore = cloner.Clone(original.variableStore);
    4847    }
    49     protected ProblemDefinitionScript() :base(){
     48    protected ProblemDefinitionScript()
     49      : base() {
    5050      variableStore = new VariableStore();
    5151    }
    5252
    5353    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; }
    5855    }
    5956
     
    6259    protected IProblemDefinition CompiledProblemDefinition {
    6360      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.");
    6967        return compiledProblemDefinition;
    7068      }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/SingleObjectiveProblemDefinitionScript.cs

    r11739 r11753  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using HeuristicLab.Common;
     
    2625using HeuristicLab.Optimization;
    2726using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     27using HeuristicLab.Problems.Programmable.New.Scripts.Templates;
    2828
    2929namespace HeuristicLab.Problems.Programmable {
     
    3434
    3535    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    }
    4638
    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; }
    9741    }
    9842
     
    10044    private SingleObjectiveProblemDefinitionScript(bool deserializing) : base(deserializing) { }
    10145    private SingleObjectiveProblemDefinitionScript(SingleObjectiveProblemDefinitionScript original, Cloner cloner) : base(original, cloner) { }
    102     public SingleObjectiveProblemDefinitionScript() :base(){
     46    public SingleObjectiveProblemDefinitionScript()
     47      : base() {
    10348      Code = CodeTemplate;
    10449    }
     
    10853    }
    10954
    110     public new ISingleObjectiveProblemDefinition CompiledProblemDefinition {
    111       get { return (ISingleObjectiveProblemDefinition)base.CompiledProblemDefinition; }
    112     }
    113 
    11455    bool ISingleObjectiveProblemDefinition.Maximization {
    115       get { return CompiledProblemDefinition != null && CompiledProblemDefinition.Maximization; }
     56      get { return CompiledProblemDefinition.Maximization; }
    11657    }
    11758
     
    12364      CompiledProblemDefinition.Analyze(individuals, qualities, results);
    12465    }
     66    IEnumerable<Individual> ISingleObjectiveProblemDefinition.GetNeighbors(Individual individual, IRandom random) {
     67      return CompiledProblemDefinition.GetNeighbors(individual, random);
     68    }
    12569  }
    12670}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/SingleObjectiveProgrammableProblem.cs

    r11739 r11753  
    2424using HeuristicLab.Common;
    2525using HeuristicLab.Core;
     26using HeuristicLab.Data;
    2627using HeuristicLab.Optimization;
     28using HeuristicLab.Parameters;
    2729using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2830
    2931namespace HeuristicLab.Problems.Programmable {
    3032  [StorableClass]
    31   public abstract class SingleObjectiveProgrammableProblem<TEncoding> : ProgrammableProblem<TEncoding, SingleObjectiveEvaluator>, ISingleObjectiveProblemDefinition
     33  public abstract class SingleObjectiveProgrammableProblem<TEncoding> : ProgrammableProblem<TEncoding, SingleObjectiveEvaluator>,
     34    ISingleObjectiveProblemDefinition, ISingleObjectiveHeuristicOptimizationProblem
    3235  where TEncoding : class, IEncoding {
    3336    [StorableConstructor]
     
    4144    protected SingleObjectiveProgrammableProblem()
    4245      : 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."));
    4348
     49      Operators.Add(Evaluator);
    4450      Operators.Add(new BestScopeSolutionAnalyzer());
    45       Operators.Add(new SingleObjectiveEvaluator());
    4651      Operators.Add(new SingleObjectiveAnalyzer());
     52      Operators.Add(new SingleObjectiveImprover());
     53      Operators.Add(new SingleObjectiveMoveEvaluator());
     54      Operators.Add(new SingleObjectiveMoveGenerator());
     55      Operators.Add(new SingleObjectiveMoveMaker());
    4756
    4857      ParameterizeOperators();
     
    7281    }
    7382
     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
    7494  }
    7595}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/SingleObjectiveScriptableProblem.cs

    r11739 r11753  
    2121
    2222using System.Collections.Generic;
     23using System.Linq;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
     
    3738    }
    3839
    39     private SingleObjectiveProblemDefinitionScript ProblemScript {
     40    public SingleObjectiveProblemDefinitionScript ProblemScript {
    4041      get { return SingleObjectiveProblemScriptParameter.Value; }
    4142    }
    4243
    43     private ISingleObjectiveProblemDefinition ProblemDefinition {
     44    public ISingleObjectiveProblemDefinition ProblemDefinition {
    4445      get { return SingleObjectiveProblemScriptParameter.Value; }
    4546    }
     
    4849      : base(original, cloner) {
    4950      RegisterEvents();
     51      ProblemScript.Compile();
    5052    }
    5153    public override IDeepCloneable Clone(Cloner cloner) { return new SingleObjectiveScriptableProblem(this, cloner); }
     
    5961      Parameters.Add(new FixedValueParameter<SingleObjectiveProblemDefinitionScript>("ProblemScript", "Defines the problem.", new SingleObjectiveProblemDefinitionScript() { Name = Name }));
    6062      RegisterEvents();
     63
     64
    6165    }
    6266
     
    6468    private void AfterDeserialization() {
    6569      RegisterEvents();
     70      ProblemScript.Compile();
    6671    }
    6772
     
    7176
    7277    private void OnProblemDefinitionChanged() {
     78      Encoding = ProblemDefinition.Encoding;
    7379    }
    7480
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiEncodingOperator.cs

    r11737 r11753  
    2020#endregion
    2121using System;
     22using System.Collections.Generic;
    2223using System.Linq;
    2324using HeuristicLab.Common;
     
    3031namespace HeuristicLab.Problems.Programmable.Operators {
    3132  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
    3240    [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    }
    3551
    3652    protected MultiEncodingOperator() : base() { }
     53
     54    [StorableHook(HookType.AfterDeserialization)]
     55    private void AfterDeserialization() {
     56      foreach (var encoding in encodings)
     57        encoding.OperatorsChanged += Encoding_OperatorsChanged;
     58    }
    3759
    3860
     
    4466    public virtual void AddEncoding(IEncoding encoding) {
    4567      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
    4672      var param = new ConstrainedValueParameter<T>(encoding.Name, new ItemSet<T>(encoding.Operators.OfType<T>()));
    4773      param.Value = param.ValidValues.First();
     
    5076
    5177    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;
    5280      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;
    5391    }
    5492
     
    5997    }
    6098
     99    private void Encoding_OperatorsChanged(object sender, EventArgs e) {
     100      var encoding = (IEncoding)sender;
     101      var param = GetParameter(encoding);
    61102
     103      var oldParameterValue = param.Value;
     104      param.ValidValues.Clear();
     105      foreach (var op in encoding.Operators.OfType<T>())
     106        param.ValidValues.Add(op);
    62107
     108      var newValue = param.ValidValues.FirstOrDefault(op => op.GetType() == oldParameterValue.GetType());
     109      if (newValue == null) newValue = param.ValidValues.First();
     110      param.Value = newValue;
     111    }
    63112  }
    64113}
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveEvaluator.cs

    r11739 r11753  
    3232  [Item("Single-objective MoveEvaluator", "Evaluates a parameter vector that results from a move.")]
    3333  [StorableClass]
    34   public class SingleObjectiveMoveEvaluator : SingleSuccessorOperator, ISingleObjectiveEvaluationOperator, ISingleObjectiveMoveEvaluator, IStochasticOperator {
     34  public class SingleObjectiveMoveEvaluator : SingleSuccessorOperator, ISingleObjectiveEvaluationOperator, ISingleObjectiveMoveEvaluator, IStochasticOperator, ISingleObjectiveMoveOperator {
    3535
    3636    public ILookupParameter<IRandom> RandomParameter {
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveGenerator.cs

    r11739 r11753  
    3535  [Item("Single-objective MoveGenerator", "Calls the GetNeighbors method of the problem definition to obtain the moves.")]
    3636  [StorableClass]
    37   public class SingleObjectiveMoveGenerator : SingleSuccessorOperator, INeighborBasedOperator, IMultiMoveGenerator, IStochasticOperator {
     37  public class SingleObjectiveMoveGenerator : SingleSuccessorOperator, INeighborBasedOperator, IMultiMoveGenerator, IStochasticOperator, ISingleObjectiveMoveOperator {
    3838    public ILookupParameter<IRandom> RandomParameter {
    3939      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
  • branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/SingleObjectiveMoveMaker.cs

    r11739 r11753  
    3232  [Item("Single-objective MoveMaker", "Applies a move.")]
    3333  [StorableClass]
    34   public class SingleObjectiveMoveMaker : InstrumentedOperator, IMoveMaker {
     34  public class SingleObjectiveMoveMaker : InstrumentedOperator, IMoveMaker, ISingleObjectiveMoveOperator {
    3535    public ILookupParameter<IEncoding> EncodingParameter {
    3636      get { return (ILookupParameter<IEncoding>)Parameters["Encoding"]; }
Note: See TracChangeset for help on using the changeset viewer.