Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/18/12 16:55:08 (13 years ago)
Author:
abeham
Message:

#1614

  • worked on GQAP
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/GRASPWithPathRelinking.cs

    r7345 r7363  
    2929using HeuristicLab.Parameters;
    3030using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     31using HeuristicLab.PluginInfrastructure;
     32using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common;
    3133using HeuristicLab.Random;
    3234
    3335namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms {
    3436  /// <summary>
    35   /// A genetic algorithm.
     37  /// The algorithm combines the Greedy Randomized Adaptive Search Procedure (GRASP) with Path Relinking and is described in Mateus, G., Resende, M., and Silva, R. 2011. GRASP with path-relinking for the generalized quadratic assignment problem. Journal of Heuristics 17, Springer Netherlands, pp. 527-565.
    3638  /// </summary>
    3739  [Item("GRASP+PR", "The algorithm combines the Greedy Randomized Adaptive Search Procedure (GRASP) with Path Relinking and is described in Mateus, G., Resende, M., and Silva, R. 2011. GRASP with path-relinking for the generalized quadratic assignment problem. Journal of Heuristics 17, Springer Netherlands, pp. 527-565.")]
     
    5961      get { return (IValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
    6062    }
    61     private IValueParameter<IntValue> EliteSetSizeParameter {
    62       get { return (IValueParameter<IntValue>)Parameters["EliteSetSize"]; }
    63     }
    64     private IValueParameter<IntValue> LocalImprovementMaximumIterationsParameter {
    65       get { return (IValueParameter<IntValue>)Parameters["LocalImprovementMaximumIterations"]; }
     63    private IFixedValueParameter<IntValue> EliteSetSizeParameter {
     64      get { return (IFixedValueParameter<IntValue>)Parameters["EliteSetSize"]; }
     65    }
     66    private IFixedValueParameter<IntValue> LocalImprovementMaximumIterationsParameter {
     67      get { return (IFixedValueParameter<IntValue>)Parameters["LocalImprovementMaximumIterations"]; }
    6668    }
    6769    private ConstrainedValueParameter<ILocalImprovementOperator> LocalImprovementParameter {
     
    7476      get { return (ConstrainedValueParameter<ICrossover>)Parameters["PathRelinking"]; }
    7577    }
    76     public ConstrainedValueParameter<IReplacer> EliteSetReplacerParameter {
    77       get { return (ConstrainedValueParameter<IReplacer>)Parameters["EliteSetReplacer"]; }
     78    public ConstrainedValueParameter<IMerger> EliteSetReplacerParameter {
     79      get { return (ConstrainedValueParameter<IMerger>)Parameters["EliteSetMerger"]; }
    7880    }
    7981    #endregion
     
    9496    public IntValue EliteSetSize {
    9597      get { return EliteSetSizeParameter.Value; }
    96       set { EliteSetSizeParameter.Value = value; }
     98      set { EliteSetSizeParameter.Value.Value = value.Value; }
    9799    }
    98100    public IntValue LocalImprovementMaximumIterations {
    99101      get { return LocalImprovementMaximumIterationsParameter.Value; }
    100       set { LocalImprovementMaximumIterationsParameter.Value = value; }
     102      set { LocalImprovementMaximumIterationsParameter.Value.Value = value.Value; }
    101103    }
    102104
     
    110112    private GRASPWithPathRelinking(GRASPWithPathRelinking original, Cloner cloner)
    111113      : base(original, cloner) {
    112       // TODO: clone your private fields here
    113114      RegisterEventHandlers();
    114115    }
     
    118119      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator."));
    119120      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each iteration.", new MultiAnalyzer()));
    120       Parameters.Add(new ValueParameter<IntValue>("EliteSetSize", "The elite set stores the best found solutions.", new IntValue(10)));
    121       Parameters.Add(new ValueParameter<IntValue>("LocalImprovementMaximumIterations", "The maximum number of iterations performed by the local improvement operator.", new IntValue(100)));
    122       Parameters.Add(new ConstrainedValueParameter<ILocalImprovementOperator>("LocalImprovement", "Performs a local search on the solution.", null));
     121      Parameters.Add(new FixedValueParameter<IntValue>("EliteSetSize", "The elite set stores the best found solutions.", new IntValue(10)));
     122      Parameters.Add(new FixedValueParameter<IntValue>("LocalImprovementMaximumIterations", "The maximum number of iterations performed by the local improvement operator.", new IntValue(100)));
     123      Parameters.Add(new ConstrainedValueParameter<ILocalImprovementOperator>("LocalImprovement", "Performs a local search on the solution."));
    123124      Parameters.Add(new FixedValueParameter<IntValue>("MinimumEliteSetSize", "(ρ) The minimum amount of elites for performing path relinking.", new IntValue(1)));
    124125      Parameters.Add(new ConstrainedValueParameter<ICrossover>("PathRelinking", "The operator that performs the path relinking."));
    125       Parameters.Add(new ConstrainedValueParameter<IReplacer>("EliteSetReplacer", "The operator that replaces solutions in the elite set."));
     126      Parameters.Add(new ConstrainedValueParameter<IMerger>("EliteSetMerger", "The operator that merges new solutions into the elite set."));
    126127
    127128      RandomCreator randomCreator = new RandomCreator();
     
    151152    #region Events
    152153    protected override void OnProblemChanged() {
    153       // TODO: Initialize and parameterize operators
    154154      InitializeOperators();
    155155      base.OnProblemChanged();
     
    157157
    158158    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
    159       // TODO: Parameterize operators
     159      Parameterize();
    160160      base.Problem_SolutionCreatorChanged(sender, e);
    161161    }
    162162    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
    163       // TODO: Parameterize operators
     163      Parameterize();
    164164      base.Problem_EvaluatorChanged(sender, e);
    165165    }
    166166    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
    167       // TODO: Parameterize operators
    168167      InitializeOperators();
    169168      base.Problem_OperatorsChanged(sender, e);
     
    176175      if (Problem != null) {
    177176      }
     177      RegisterEventHandlers();
    178178    }
    179179
     
    190190          Analyzer.Operators.Add(analyzer);
    191191        }
    192 
    193         string oldLocalImprovement = String.Empty;
    194         if (LocalImprovementParameter.Value != null)
    195           oldLocalImprovement = LocalImprovementParameter.Value.Name;
     192        InitializeFromInstallation(LocalImprovementParameter, x => x.ProblemType.IsAssignableFrom(Problem.GetType()));
     193        InitializeFromProblem(PathRelinkingParameter);
     194        InitializeFromProblem(EliteSetReplacerParameter);
     195      } else {
    196196        LocalImprovementParameter.ValidValues.Clear();
    197         foreach (ILocalImprovementOperator localImprovement in Problem.Operators.OfType<ILocalImprovementOperator>()) {
    198           LocalImprovementParameter.ValidValues.Add(localImprovement);
    199         }
    200         var newLocalImprovement = LocalImprovementParameter.ValidValues.FirstOrDefault(x => x.Name == oldLocalImprovement);
    201         if (newLocalImprovement != null) LocalImprovementParameter.Value = newLocalImprovement;
    202 
    203         string oldPathRelinking = String.Empty;
    204         if (PathRelinkingParameter.Value != null)
    205           oldPathRelinking = PathRelinkingParameter.Value.Name;
    206197        PathRelinkingParameter.ValidValues.Clear();
    207         foreach (ICrossover pathRelinking in Problem.Operators.OfType<ICrossover>()) {
    208           PathRelinkingParameter.ValidValues.Add(pathRelinking);
    209         }
    210         var newPathRelinking = PathRelinkingParameter.ValidValues.FirstOrDefault(x => x.Name == oldPathRelinking);
    211         if (newPathRelinking != null) PathRelinkingParameter.Value = newPathRelinking;
    212 
    213         string oldReplacer = String.Empty;
    214         if (EliteSetReplacerParameter.Value != null)
    215           oldReplacer = EliteSetReplacerParameter.Value.Name;
    216198        EliteSetReplacerParameter.ValidValues.Clear();
    217         foreach (IReplacer replacer in Problem.Operators.OfType<IReplacer>()) {
    218           EliteSetReplacerParameter.ValidValues.Add(replacer);
    219         }
    220         var newReplacer = EliteSetReplacerParameter.ValidValues.FirstOrDefault(x => x.Name == oldReplacer);
    221         if (newReplacer != null) EliteSetReplacerParameter.Value = newReplacer;
    222199      }
    223200
     
    234211      }
    235212    }
     213
     214    private void InitializeFromProblem<T>(ConstrainedValueParameter<T> parameter) where T : class, INamedItem {
     215      InitializeFromProblem(parameter, x => true);
     216    }
     217    private void InitializeFromProblem<T>(ConstrainedValueParameter<T> parameter, Func<T, bool> condition) where T : class, INamedItem {
     218      if (parameter == null) throw new ArgumentNullException("parameter");
     219      if (condition == null) throw new ArgumentNullException("condition");
     220      string oldName = String.Empty;
     221      if (parameter.Value != null) oldName = parameter.Value.Name;
     222      parameter.ValidValues.Clear();
     223      foreach (var item in Problem.Operators.OfType<T>().Where(condition)) {
     224        parameter.ValidValues.Add(item);
     225      }
     226      var newItem = parameter.ValidValues.FirstOrDefault(x => x.Name == oldName);
     227      if (newItem != null) parameter.Value = newItem;
     228    }
     229    private void InitializeFromInstallation<T>(ConstrainedValueParameter<T> parameter) where T : class, INamedItem {
     230      InitializeFromInstallation(parameter, x => true);
     231    }
     232    private void InitializeFromInstallation<T>(ConstrainedValueParameter<T> parameter, Func<T, bool> condition) where T : class, INamedItem {
     233      if (parameter == null) throw new ArgumentNullException("parameter");
     234      if (condition == null) throw new ArgumentNullException("condition");
     235      string oldName = String.Empty;
     236      if (parameter.Value != null) oldName = parameter.Value.Name;
     237      parameter.ValidValues.Clear();
     238      foreach (var item in ApplicationManager.Manager.GetInstances<T>().Where(condition)) {
     239        parameter.ValidValues.Add(item);
     240      }
     241      var newItem = parameter.ValidValues.FirstOrDefault(x => x.Name == oldName);
     242      if (newItem != null) parameter.Value = newItem;
     243    }
    236244    #endregion
    237245  }
Note: See TracChangeset for help on using the changeset viewer.