Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/09/11 11:17:48 (14 years ago)
Author:
svonolfe
Message:

Implemented wiring (#1425)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/VNS/HeuristicLab.Algorithms.LocalSearch/3.3/LocalSearchImprovement.cs

    r5622 r5642  
    4444    private LocalSearchMainLoop loop;
    4545   
    46     private /*Constrained*/ValueParameter<IMoveGenerator> MoveGeneratorParameter {
    47       get { return (/*Constrained*/ValueParameter<IMoveGenerator>)Parameters["MoveGenerator"]; }
    48     }
    49     private /*Constrained*/ValueParameter<IMoveMaker> MoveMakerParameter {
    50       get { return (/*Constrained*/ValueParameter<IMoveMaker>)Parameters["MoveMaker"]; }
    51     }
    52     private /*Constrained*/ValueParameter<ISingleObjectiveMoveEvaluator> MoveEvaluatorParameter {
    53       get { return (/*Constrained*/ValueParameter<ISingleObjectiveMoveEvaluator>)Parameters["MoveEvaluator"]; }
     46    private ConstrainedValueParameter<IMoveGenerator> MoveGeneratorParameter {
     47      get { return (ConstrainedValueParameter<IMoveGenerator>)Parameters["MoveGenerator"]; }
     48    }
     49    private ConstrainedValueParameter<IMoveMaker> MoveMakerParameter {
     50      get { return (ConstrainedValueParameter<IMoveMaker>)Parameters["MoveMaker"]; }
     51    }
     52    private ConstrainedValueParameter<ISingleObjectiveMoveEvaluator> MoveEvaluatorParameter {
     53      get { return (ConstrainedValueParameter<ISingleObjectiveMoveEvaluator>)Parameters["MoveEvaluator"]; }
    5454    }
    5555    private ValueParameter<IntValue> MaximumIterationsParameter {
     
    8080
    8181    [StorableConstructor]
    82     protected LocalSearchImprovement(bool deserializing) : base(deserializing) { }
     82    protected LocalSearchImprovement(bool deserializing) : base(deserializing) {}
     83    [StorableHook(HookType.AfterDeserialization)]
     84    private void AfterDeserialization() {
     85      Initialize();
     86    }
    8387    protected LocalSearchImprovement(LocalSearchImprovement original, Cloner cloner)
    8488      : base(original, cloner) {
    8589        this.loop = cloner.Clone(original.loop);
     90        Initialize();
    8691    }
    8792    public override IDeepCloneable Clone(Cloner cloner) {
     
    9297        loop = new LocalSearchMainLoop(new BoolValue(true));
    9398
    94         Parameters.Add(new /*Constrained*/ValueParameter<IMoveGenerator>("MoveGenerator", "The operator used to generate moves to the neighborhood of the current solution."));
    95         Parameters.Add(new /*Constrained*/ValueParameter<IMoveMaker>("MoveMaker", "The operator used to perform a move."));
    96         Parameters.Add(new /*Constrained*/ValueParameter<ISingleObjectiveMoveEvaluator>("MoveEvaluator", "The operator used to evaluate a move."));
     99        Parameters.Add(new ConstrainedValueParameter<IMoveGenerator>("MoveGenerator", "The operator used to generate moves to the neighborhood of the current solution."));
     100        Parameters.Add(new ConstrainedValueParameter<IMoveMaker>("MoveMaker", "The operator used to perform a move."));
     101        Parameters.Add(new ConstrainedValueParameter<ISingleObjectiveMoveEvaluator>("MoveEvaluator", "The operator used to evaluate a move."));
    97102        Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(1000)));
    98103        Parameters.Add(new ValueParameter<IntValue>("SampleSize", "Number of moves that MultiMoveGenerators should create. This is ignored for Exhaustive- and SingleMoveGenerators.", new IntValue(100)));
    99104        Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated moves."));
    100105        Parameters.Add(new LookupParameter<IOperator>("Analyzer", "The operator used to analyze the solution."));
     106
     107        Initialize();
     108    }
     109
     110    private void Initialize() {
     111      MoveGeneratorParameter.ValueChanged += new EventHandler(MoveGeneratorParameter_ValueChanged);
    101112    }
    102113
    103114    public void Parameterize(IProblem problem) {
    104115      UpdateMoveOperators(problem);
     116      ChooseMoveOperators();
     117
     118      ParameterizeMoveGenerators(problem as ISingleObjectiveProblem);
     119      ParameterizeMoveEvaluators(problem as ISingleObjectiveProblem);
     120      ParameterizeMoveMakers(problem as ISingleObjectiveProblem);
     121    }
     122
     123    void MoveGeneratorParameter_ValueChanged(object sender, EventArgs e) {
     124      ChooseMoveOperators();
    105125    }
    106126
    107127    private void UpdateMoveOperators(IProblem problem) {
    108      /* IMoveGenerator oldMoveGenerator = MoveGenerator;
    109       MoveGeneratorParameter.ValidValues.Clear();
     128      IMoveGenerator oldMoveGenerator = MoveGenerator;
     129      IMoveMaker oldMoveMaker = MoveMaker;
     130      ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator;
     131
     132      ClearMoveParameters();
     133
    110134      if (problem != null) {
    111135        foreach (IMoveGenerator generator in problem.Operators.OfType<IMoveGenerator>().OrderBy(x => x.Name))
     
    127151      }
    128152
    129       IMoveMaker oldMoveMaker = MoveMaker;
    130153      if (oldMoveMaker != null) {
    131154        IMoveMaker mm = MoveMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType());
     
    133156      }
    134157
    135       ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator;
    136158      if (oldMoveEvaluator != null) {
    137159        ISingleObjectiveMoveEvaluator me = MoveEvaluatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType());
    138160        if (me != null) MoveEvaluator = me;
    139       }*/
     161      }
     162    }
     163
     164    private void ChooseMoveOperators() {
     165      IMoveMaker oldMoveMaker = MoveMaker;
     166      ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator;
     167
     168      if (MoveGenerator != null) {
     169        List<Type> moveTypes = MoveGenerator.GetType().GetInterfaces().Where(x => typeof(IMoveOperator).IsAssignableFrom(x)).ToList();
     170        foreach (Type type in moveTypes.ToList()) {
     171          if (moveTypes.Any(t => t != type && type.IsAssignableFrom(t)))
     172            moveTypes.Remove(type);
     173        }
     174        List<IMoveMaker> validMoveMakers = new List<IMoveMaker>();
     175        List<ISingleObjectiveMoveEvaluator> validMoveEvaluators = new List<ISingleObjectiveMoveEvaluator>();
     176
     177        foreach (Type type in moveTypes) {
     178          var moveMakers = MoveMakerParameter.ValidValues.Where(x => type.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name);
     179          foreach (IMoveMaker moveMaker in moveMakers)
     180            validMoveMakers.Add(moveMaker);
     181
     182          var moveEvaluators = MoveEvaluatorParameter.ValidValues.Where(x => type.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name);
     183          foreach (ISingleObjectiveMoveEvaluator moveEvaluator in moveEvaluators)
     184            validMoveEvaluators.Add(moveEvaluator);
     185        }
     186        if (oldMoveMaker != null) {
     187          IMoveMaker mm = validMoveMakers.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType());
     188          if (mm != null) MoveMaker = mm;
     189          else MoveMaker = validMoveMakers.FirstOrDefault();
     190        }
     191
     192        if (oldMoveEvaluator != null) {
     193          ISingleObjectiveMoveEvaluator me = validMoveEvaluators.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType());
     194          if (me != null) MoveEvaluator = me;
     195          else MoveEvaluator = validMoveEvaluators.FirstOrDefault();
     196        }
     197      }
    140198    }
    141199
    142200    private void ClearMoveParameters() {
    143       /*MoveMakerParameter.ValidValues.Clear();
    144       MoveEvaluatorParameter.ValidValues.Clear();*/
     201      MoveGeneratorParameter.ValidValues.Clear();
     202      MoveMakerParameter.ValidValues.Clear();
     203      MoveEvaluatorParameter.ValidValues.Clear();
     204    }
     205
     206    private void ParameterizeMoveGenerators(ISingleObjectiveProblem problem) {
     207      if (problem != null) {
     208        foreach (IMultiMoveGenerator generator in problem.Operators.OfType<IMultiMoveGenerator>())
     209          generator.SampleSizeParameter.ActualName = SampleSizeParameter.Name;
     210      }
     211    }
     212    private void ParameterizeMoveEvaluators(ISingleObjectiveProblem problem) {
     213      foreach (ISingleObjectiveMoveEvaluator op in problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) {
     214        op.QualityParameter.ActualName = problem.Evaluator.QualityParameter.ActualName;
     215      }
     216    }
     217    private void ParameterizeMoveMakers(ISingleObjectiveProblem problem) {
     218      foreach (IMoveMaker op in problem.Operators.OfType<IMoveMaker>()) {
     219        op.QualityParameter.ActualName = problem.Evaluator.QualityParameter.ActualName;
     220        if (MoveEvaluator != null)
     221          op.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName;
     222      }
    145223    }
    146224
Note: See TracChangeset for help on using the changeset viewer.