Free cookie consent management tool by TermsFeed Policy Generator

Changeset 4098


Ignore:
Timestamp:
07/25/10 01:04:14 (13 years ago)
Author:
abeham
Message:

#1090

  • Fixed initialization of operators and made operator list storable
Location:
trunk/sources
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntProblem.cs

    r4068 r4098  
    4242  [StorableClass]
    4343  public sealed class ArtificialAntProblem : ParameterizedNamedItem, ISingleObjectiveProblem {
     44
    4445    public override Image ItemImage {
    4546      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
     
    180181      get { return BestKnownQualityParameter.Value; }
    181182    }
    182     private List<IOperator> operators;
    183183    public IEnumerable<IOperator> Operators {
    184184      get { return operators; }
     
    190190    #endregion
    191191
     192    [Storable]
     193    private List<IOperator> operators;
     194
     195    [StorableConstructor]
     196    private ArtificialAntProblem(bool deserializing) : base() { }
    192197    public ArtificialAntProblem()
    193198      : base() {
     
    212217      ParameterizeSolutionCreator();
    213218      ParameterizeEvaluator();
    214       Initialize();
    215     }
    216 
    217     [StorableConstructor]
    218     private ArtificialAntProblem(bool deserializing) : base() { }
     219      InitializeOperators();
     220      AttachEventHandlers();
     221    }
    219222
    220223    public override IDeepCloneable Clone(Cloner cloner) {
    221224      ArtificialAntProblem clone = (ArtificialAntProblem)base.Clone(cloner);
    222       clone.Initialize();
     225      clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
     226      clone.AttachEventHandlers();
    223227      return clone;
    224228    }
     
    274278    #region Helpers
    275279    [StorableHook(HookType.AfterDeserialization)]
    276     private void Initialize() {
    277       InitializeOperators();
     280    private void AttachEventHandlers() {
     281      // Start BackwardsCompatibility3.3 (remove with 3.4)
     282      if (operators == null) InitializeOperators();
     283      // End BackwardsCompatibility3.3
    278284      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
    279285      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.FeatureSelection/3.3/FeatureSelectionProblem.cs

    r4082 r4098  
    8181      get { return EvaluatorParameter.Value; }
    8282    }
    83     private List<IOperator> operators;
    8483    public override IEnumerable<IOperator> Operators {
    8584      get { return operators; }
     
    8786    #endregion
    8887
     88    [Storable]
     89    private List<IOperator> operators;
     90
     91    [StorableConstructor]
     92    private FeatureSelectionProblem(bool deserializing) : base() { }
    8993    public FeatureSelectionProblem()
    9094      : base() {
     
    101105      ParameterizeEvaluator();
    102106
    103       Initialize();
    104     }
    105 
    106     [StorableConstructor]
    107     private FeatureSelectionProblem(bool deserializing) : base() { }
     107      InitializeOperators();
     108      AttachEventHandlers();
     109    }
    108110
    109111    [StorableHook(HookType.AfterDeserialization)]
     
    115117    public override IDeepCloneable Clone(Cloner cloner) {
    116118      FeatureSelectionProblem clone = (FeatureSelectionProblem)base.Clone(cloner);
    117       clone.Initialize();
     119      clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
     120      clone.AttachEventHandlers();
    118121      return clone;
    119122    }
     
    165168
    166169    #region Helpers
    167     private void Initialize() {
    168       InitializeOperators();
     170    private void AttachEventHandlers() {
     171      // Start BackwardsCompatibility3.3 (remove with 3.4)
     172      if (operators == null) InitializeOperators();
     173      // End BackwardsCompatibility3.3
    169174      RegisterParameterEvents();
    170175      RegisterParameterValueEvents();
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.MultiVariate/3.3/MultiVariateDataAnalysisProblem.cs

    r4068 r4098  
    5050    #endregion
    5151
     52    [StorableConstructor]
     53    private MultiVariateDataAnalysisProblem(bool deserializing) : base() { }
    5254    public MultiVariateDataAnalysisProblem()
    5355      : base() {
     
    5658      RegisterParameterValueEvents();
    5759    }
    58 
    59     [StorableConstructor]
    60     private MultiVariateDataAnalysisProblem(bool deserializing) : base() { }
    6160
    6261    [StorableHook(HookType.AfterDeserialization)]
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionProblem.cs

    r4068 r4098  
    147147      get { return BestKnownQualityParameter.Value; }
    148148    }
    149     private List<IOperator> operators;
    150149    public override IEnumerable<IOperator> Operators {
    151150      get { return operators; }
     
    180179    #endregion
    181180
     181    [Storable]
     182    private List<IOperator> operators;
     183
     184    [StorableConstructor]
     185    private SymbolicRegressionProblem(bool deserializing) : base() { }
    182186    public SymbolicRegressionProblem()
    183187      : base() {
     
    208212      UpdateGrammar();
    209213      UpdateEstimationLimits();
    210       Initialize();
    211     }
    212 
    213     [StorableConstructor]
    214     private SymbolicRegressionProblem(bool deserializing) : base() { }
    215 
    216     [StorableHook(HookType.AfterDeserialization)]
    217     private void AfterDeserializationHook() {
    218       RegisterParameterEvents();
    219       RegisterParameterValueEvents();
     214      InitializeOperators();
     215      AttachEventHandlers();
    220216    }
    221217
    222218    public override IDeepCloneable Clone(Cloner cloner) {
    223219      SymbolicRegressionProblem clone = (SymbolicRegressionProblem)base.Clone(cloner);
    224       clone.Initialize();
     220      clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
     221      clone.AttachEventHandlers();
    225222      return clone;
    226223    }
     
    305302
    306303    #region Helpers
    307     private void Initialize() {
    308       InitializeOperators();
     304    [StorableHook(HookType.AfterDeserialization)]
     305    private void AttachEventHandlers() {
     306      // Start BackwardsCompatibility3.3 (remove with 3.4)
     307      if (operators == null) InitializeOperators();
     308      // End BackwardsCompatibility3.3
    309309      RegisterParameterEvents();
    310310      RegisterParameterValueEvents();
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/SupportVectorMachine/ParameterAdjustmentProblem/SupportVectorMachineParameterAdjustmentProblem.cs

    r4068 r4098  
    107107      set { BestKnownQualityParameter.Value = value; }
    108108    }
    109     private List<IOperator> operators;
    110109    public override IEnumerable<IOperator> Operators {
    111110      get { return operators; }
     
    120119    }
    121120
     121    [Storable]
     122    private List<IOperator> operators;
    122123    [Storable]
    123124    private StdDevStrategyVectorCreator strategyVectorCreator;
     
    160161      ParameterizeEvaluator();
    161162
    162       Initialize();
     163      InitializeOperators();
     164      AttachEventHandlers();
    163165      UpdateStrategyVectorBounds();
    164166    }
     
    166168    public override IDeepCloneable Clone(Cloner cloner) {
    167169      SupportVectorMachineParameterAdjustmentProblem clone = (SupportVectorMachineParameterAdjustmentProblem)base.Clone(cloner);
     170      clone.operators = operators.Where(x => IsNotFieldReferenced(x)).Select(x => (IOperator)cloner.Clone(x)).ToList();
    168171      clone.strategyVectorCreator = (StdDevStrategyVectorCreator)cloner.Clone(strategyVectorCreator);
     172      clone.operators.Add(clone.strategyVectorCreator);
    169173      clone.strategyVectorCrossover = (StdDevStrategyVectorCrossover)cloner.Clone(strategyVectorCrossover);
     174      clone.operators.Add(strategyVectorCrossover);
    170175      clone.strategyVectorManipulator = (StdDevStrategyVectorManipulator)cloner.Clone(strategyVectorManipulator);
    171       clone.Initialize();
     176      clone.operators.Add(strategyVectorManipulator);
     177      clone.AttachEventHandlers();
    172178      return clone;
     179    }
     180
     181    private bool IsNotFieldReferenced(IOperator x) {
     182      return !(x == strategyVectorCreator
     183        || x == strategyVectorCrossover
     184        || x == strategyVectorManipulator);
    173185    }
    174186
     
    204216    #region Helpers
    205217    [StorableHook(HookType.AfterDeserialization)]
    206     private void Initialize() {
    207       InitializeOperators();
     218    private void AttachEventHandlers() {
     219      // Start BackwardsCompatibility3.3 (remove with 3.4)
     220      if (operators == null) InitializeOperators();
     221      // End BackwardsCompatibility3.3
    208222      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
    209223      SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/ExternalEvaluationProblem.cs

    r4068 r4098  
    192192
    193193    #region Helpers
    194     private void InitializeOperators() {
    195       ItemList<IOperator> operators = OperatorsParameter.Value;
    196       operators.Add(new BestScopeSolutionAnalyzer());
    197       ParameterizeAnalyzers();
    198     }
    199194    [StorableHook(HookType.AfterDeserialization)]
    200195    private void AttachEventHandlers() {
     
    206201      OperatorsParameter.Value.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_ItemsRemoved);
    207202      OperatorsParameter.Value.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_CollectionReset);
     203    }
     204    private void InitializeOperators() {
     205      ItemList<IOperator> operators = OperatorsParameter.Value;
     206      operators.Add(new BestScopeSolutionAnalyzer());
     207      ParameterizeAnalyzers();
    208208    }
    209209    private void ParameterizeAnalyzers() {
  • trunk/sources/HeuristicLab.Problems.Knapsack/3.3/KnapsackProblem.cs

    r3797 r4098  
    126126      set { BestKnownSolutionParameter.Value = value; }
    127127    }
    128     private List<IOperator> operators;
    129128    public IEnumerable<IOperator> Operators {
    130129      get { return operators.Cast<IOperator>(); }
     
    135134    #endregion
    136135
    137     private void InitializeRandomKnapsackInstance() {
    138       System.Random rand = new System.Random();
    139 
    140       int itemCount = rand.Next(10, 100);
    141       Weights = new IntArray(itemCount);
    142       Values = new IntArray(itemCount);
    143 
    144       double totalWeight = 0;
    145 
    146       for (int i = 0; i < itemCount; i++ ) {
    147         int value = rand.Next(1, 10);
    148         int weight = rand.Next(1, 10);
    149 
    150         Values[i] = value;
    151         Weights[i] = weight;
    152         totalWeight += weight;
    153       }
    154 
    155       int capacity = (int)Math.Round(0.7 * totalWeight);
    156       KnapsackCapacity = new IntValue(capacity);
    157     }
    158 
     136    [Storable]
     137    private List<IOperator> operators;
     138
     139    [StorableConstructor]
     140    private KnapsackProblem(bool deserializing) : base() { }
    159141    public KnapsackProblem()
    160142      : base() {
     
    179161      ParameterizeEvaluator();
    180162
    181       Initialize();
    182     }
    183 
    184     [StorableConstructor]
    185     private KnapsackProblem(bool deserializing) : base() { }
     163      InitializeOperators();
     164      AttachEventHandlers();
     165    }
    186166
    187167    public override IDeepCloneable Clone(Cloner cloner) {
    188168      KnapsackProblem clone = (KnapsackProblem)base.Clone(cloner);
    189       clone.Initialize();
     169      clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
     170      clone.AttachEventHandlers();
    190171      return clone;
    191172    }
     
    274255    #region Helpers
    275256    [StorableHook(HookType.AfterDeserialization)]
    276     private void Initialize() {
    277       InitializeOperators();
     257    private void AttachEventHandlers() {
     258      // Start BackwardsCompatibility3.3 (remove with 3.4)
     259      if (operators == null) InitializeOperators();
     260      // End BackwardsCompatibility3.3
    278261      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
    279262      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
     
    350333    }
    351334    #endregion
     335
     336    private void InitializeRandomKnapsackInstance() {
     337      System.Random rand = new System.Random();
     338
     339      int itemCount = rand.Next(10, 100);
     340      Weights = new IntArray(itemCount);
     341      Values = new IntArray(itemCount);
     342
     343      double totalWeight = 0;
     344
     345      for (int i = 0; i < itemCount; i++) {
     346        int value = rand.Next(1, 10);
     347        int weight = rand.Next(1, 10);
     348
     349        Values[i] = value;
     350        Weights[i] = weight;
     351        totalWeight += weight;
     352      }
     353
     354      int capacity = (int)Math.Round(0.7 * totalWeight);
     355      KnapsackCapacity = new IntValue(capacity);
     356    }
    352357  }
    353358}
  • trunk/sources/HeuristicLab.Problems.OneMax/3.3/OnemaxProblem.cs

    r3797 r4098  
    9797      get { return BestKnownQualityParameter.Value; }
    9898    }
    99     private List<IOperator> operators;
    10099    public IEnumerable<IOperator> Operators {
    101100      get { return operators.Cast<IOperator>(); }
     
    106105    #endregion
    107106
     107    [Storable]
     108    private List<IOperator> operators;
     109
     110    [StorableConstructor]
     111    private OneMaxProblem(bool deserializing) : base() { }
    108112    public OneMaxProblem()
    109113      : base() {
     
    122126      ParameterizeEvaluator();
    123127
    124       Initialize();
    125     }
    126 
    127     [StorableConstructor]
    128     private OneMaxProblem(bool deserializing) : base() { }
     128      InitializeOperators();
     129      AttachEventHandlers();
     130    }
    129131
    130132    public override IDeepCloneable Clone(Cloner cloner) {
    131133      OneMaxProblem clone = (OneMaxProblem)base.Clone(cloner);
    132       clone.Initialize();
     134      clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
     135      clone.AttachEventHandlers();
    133136      return clone;
    134137    }
     
    195198    #region Helpers
    196199    [StorableHook(HookType.AfterDeserialization)]
    197     private void Initialize() {
    198       InitializeOperators();
     200    private void AttachEventHandlers() {
     201      // Start BackwardsCompatibility3.3 (remove with 3.4)
     202      if (operators == null) InitializeOperators();
     203      // End BackwardsCompatibility3.3
    199204      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
    200205      SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged);
  • trunk/sources/HeuristicLab.Problems.TestFunctions/3.3/Analyzers/BestSingleObjectiveTestFunctionSolutionAnalyzer.cs

    r4086 r4098  
    9090    [StorableHook(HookType.AfterDeserialization)]
    9191    private void CompatibilityMethod() {
     92      // BackwardsCompatibility3.3
    9293      // Bounds are introduced in 3.3.0.3894
    9394      if (!Parameters.ContainsKey("Bounds"))
  • trunk/sources/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs

    r4068 r4098  
    119119      set { BestKnownQualityParameter.Value = value; }
    120120    }
    121     private List<IOperator> operators;
    122121    public IEnumerable<IOperator> Operators {
    123122      get { return operators; }
     
    127126    }
    128127    #endregion
     128
     129    [Storable]
     130    private List<IOperator> operators;
    129131
    130132    [StorableConstructor]
     
    154156      ParameterizeEvaluator();
    155157
    156       Initialize();
     158      InitializeOperators();
     159      AttachEventHandlers();
    157160      UpdateStrategyVectorBounds();
    158161    }
     
    160163    public override IDeepCloneable Clone(Cloner cloner) {
    161164      SingleObjectiveTestFunctionProblem clone = (SingleObjectiveTestFunctionProblem)base.Clone(cloner);
     165      clone.operators = operators.Where(x => IsNotFieldReferenced(x)).Select(x => (IOperator)cloner.Clone(x)).ToList();
    162166      clone.strategyVectorCreator = (StdDevStrategyVectorCreator)cloner.Clone(strategyVectorCreator);
     167      clone.operators.Add(clone.strategyVectorCreator);
    163168      clone.strategyVectorCrossover = (StdDevStrategyVectorCrossover)cloner.Clone(strategyVectorCrossover);
     169      clone.operators.Add(strategyVectorCrossover);
    164170      clone.strategyVectorManipulator = (StdDevStrategyVectorManipulator)cloner.Clone(strategyVectorManipulator);
    165       clone.Initialize();
     171      clone.operators.Add(strategyVectorManipulator);
     172      clone.AttachEventHandlers();
    166173      return clone;
     174    }
     175
     176    private bool IsNotFieldReferenced(IOperator x) {
     177      return !(x == strategyVectorCreator
     178        || x == strategyVectorCrossover
     179        || x == strategyVectorManipulator);
    167180    }
    168181
     
    284297    #region Helpers
    285298    [StorableHook(HookType.AfterDeserialization)]
    286     private void Initialize() {
    287       InitializeOperators();
     299    private void AttachEventHandlers() {
     300      // Start BackwardsCompatibility3.3 (remove with 3.4)
     301      if (operators == null) InitializeOperators();
     302      // End BackwardsCompatibility3.3
    288303      ProblemSizeParameter.ValueChanged += new EventHandler(ProblemSizeParameter_ValueChanged);
    289304      ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
  • trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/TravelingSalesmanProblem.cs

    r4047 r4098  
    120120      set { BestKnownSolutionParameter.Value = value; }
    121121    }
    122     private List<IOperator> operators;
    123122    public IEnumerable<IOperator> Operators {
    124123      get { return operators; }
     
    129128    #endregion
    130129
     130    [Storable]
     131    private List<IOperator> operators;
     132
     133    [StorableConstructor]
     134    private TravelingSalesmanProblem(bool deserializing) : base() { }
    131135    public TravelingSalesmanProblem()
    132136      : base() {
     
    155159      ParameterizeEvaluator();
    156160
    157       Initialize();
    158     }
    159     [StorableConstructor]
    160     private TravelingSalesmanProblem(bool deserializing) : base() { }
     161      InitializeOperators();
     162      AttachEventHandlers();
     163    }
    161164
    162165    public override IDeepCloneable Clone(Cloner cloner) {
    163166      TravelingSalesmanProblem clone = (TravelingSalesmanProblem)base.Clone(cloner);
     167      clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
    164168      clone.DistanceMatrixParameter.Value = DistanceMatrixParameter.Value;
    165       clone.Initialize();
     169      clone.AttachEventHandlers();
    166170      return clone;
    167171    }
     172
     173    #region Events
     174    public event EventHandler SolutionCreatorChanged;
     175    private void OnSolutionCreatorChanged() {
     176      EventHandler handler = SolutionCreatorChanged;
     177      if (handler != null) handler(this, EventArgs.Empty);
     178    }
     179    public event EventHandler EvaluatorChanged;
     180    private void OnEvaluatorChanged() {
     181      EventHandler handler = EvaluatorChanged;
     182      if (handler != null) handler(this, EventArgs.Empty);
     183    }
     184    public event EventHandler OperatorsChanged;
     185    private void OnOperatorsChanged() {
     186      EventHandler handler = OperatorsChanged;
     187      if (handler != null) handler(this, EventArgs.Empty);
     188    }
     189    public event EventHandler Reset;
     190    private void OnReset() {
     191      EventHandler handler = Reset;
     192      if (handler != null) handler(this, EventArgs.Empty);
     193    }
     194
     195    private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
     196      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
     197      Coordinates.Reset += new EventHandler(Coordinates_Reset);
     198      ParameterizeSolutionCreator();
     199      ClearDistanceMatrix();
     200    }
     201    private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
     202      ClearDistanceMatrix();
     203    }
     204    private void Coordinates_Reset(object sender, EventArgs e) {
     205      ParameterizeSolutionCreator();
     206      ClearDistanceMatrix();
     207    }
     208    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
     209      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
     210      ParameterizeSolutionCreator();
     211      ParameterizeEvaluator();
     212      ParameterizeAnalyzer();
     213      ParameterizeOperators();
     214      OnSolutionCreatorChanged();
     215    }
     216    private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
     217      ParameterizeEvaluator();
     218      ParameterizeAnalyzer();
     219      ParameterizeOperators();
     220    }
     221    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
     222      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
     223      ParameterizeEvaluator();
     224      UpdateMoveEvaluators();
     225      ParameterizeAnalyzer();
     226      ClearDistanceMatrix();
     227      OnEvaluatorChanged();
     228    }
     229    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
     230      ParameterizeAnalyzer();
     231    }
     232    private void MoveGenerator_InversionMoveParameter_ActualNameChanged(object sender, EventArgs e) {
     233      string name = ((ILookupParameter<InversionMove>)sender).ActualName;
     234      foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {
     235        op.InversionMoveParameter.ActualName = name;
     236      }
     237    }
     238    private void MoveGenerator_TranslocationMoveParameter_ActualNameChanged(object sender, EventArgs e) {
     239      string name = ((ILookupParameter<TranslocationMove>)sender).ActualName;
     240      foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
     241        op.TranslocationMoveParameter.ActualName = name;
     242      }
     243    }
     244    #endregion
     245
     246    #region Helpers
     247    [StorableHook(HookType.AfterDeserialization)]
     248    private void AttachEventHandlers() {
     249      // Start BackwardsCompatibility3.3 (remove with 3.4)
     250      if (operators == null) InitializeOperators();
     251      // End BackwardsCompatibility3.3
     252      CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
     253      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
     254      Coordinates.Reset += new EventHandler(Coordinates_Reset);
     255      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
     256      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
     257      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
     258      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
     259    }
     260
     261    private void InitializeOperators() {
     262      operators = new List<IOperator>();
     263      operators.Add(new BestTSPSolutionAnalyzer());
     264      ParameterizeAnalyzer();
     265      operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>().Cast<IOperator>());
     266      ParameterizeOperators();
     267      UpdateMoveEvaluators();
     268      InitializeMoveGenerators();
     269    }
     270    private void InitializeMoveGenerators() {
     271      foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {
     272        if (op is IMoveGenerator) {
     273          op.InversionMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_InversionMoveParameter_ActualNameChanged);
     274        }
     275      }
     276      foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
     277        if (op is IMoveGenerator) {
     278          op.TranslocationMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_TranslocationMoveParameter_ActualNameChanged);
     279        }
     280      }
     281    }
     282    private void UpdateMoveEvaluators() {
     283      operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
     284      foreach (ITSPPathMoveEvaluator op in ApplicationManager.Manager.GetInstances<ITSPPathMoveEvaluator>())
     285        if (op.EvaluatorType == Evaluator.GetType()) {
     286          operators.Add(op);
     287        }
     288      ParameterizeOperators();
     289      OnOperatorsChanged();
     290    }
     291    private void ParameterizeSolutionCreator() {
     292      SolutionCreator.LengthParameter.Value = new IntValue(Coordinates.Rows);
     293      SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.RelativeUndirected);
     294    }
     295    private void ParameterizeEvaluator() {
     296      if (Evaluator is ITSPPathEvaluator)
     297        ((ITSPPathEvaluator)Evaluator).PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     298      if (Evaluator is ITSPCoordinatesPathEvaluator) {
     299        ITSPCoordinatesPathEvaluator evaluator = (ITSPCoordinatesPathEvaluator)Evaluator;
     300        evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
     301        evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
     302        evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
     303      }
     304    }
     305    private void ParameterizeAnalyzer() {
     306      BestTSPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
     307      BestTSPSolutionAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
     308      BestTSPSolutionAnalyzer.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     309      BestTSPSolutionAnalyzer.ResultsParameter.ActualName = "Results";
     310      BestTSPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
     311      BestTSPSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
     312      BestTSPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
     313    }
     314    private void ParameterizeOperators() {
     315      foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) {
     316        op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     317        op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     318      }
     319      foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) {
     320        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     321      }
     322      foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) {
     323        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     324      }
     325      foreach (ITSPPathMoveEvaluator op in Operators.OfType<ITSPPathMoveEvaluator>()) {
     326        op.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
     327        op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
     328        op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
     329        op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
     330        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
     331      }
     332      string inversionMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationInversionMoveOperator>().First().InversionMoveParameter.ActualName;
     333      foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>())
     334        op.InversionMoveParameter.ActualName = inversionMove;
     335      string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName;
     336      foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>())
     337        op.TranslocationMoveParameter.ActualName = translocationMove;
     338    }
     339
     340    private void ClearDistanceMatrix() {
     341      DistanceMatrixParameter.Value = null;
     342    }
     343    #endregion
    168344
    169345    public void ImportFromTSPLIB(string tspFileName, string optimalTourFileName) {
     
    197373      BestKnownQuality = new DoubleValue(bestKnownQuality);
    198374    }
    199 
    200     #region Events
    201     public event EventHandler SolutionCreatorChanged;
    202     private void OnSolutionCreatorChanged() {
    203       EventHandler handler = SolutionCreatorChanged;
    204       if (handler != null) handler(this, EventArgs.Empty);
    205     }
    206     public event EventHandler EvaluatorChanged;
    207     private void OnEvaluatorChanged() {
    208       EventHandler handler = EvaluatorChanged;
    209       if (handler != null) handler(this, EventArgs.Empty);
    210     }
    211     public event EventHandler OperatorsChanged;
    212     private void OnOperatorsChanged() {
    213       EventHandler handler = OperatorsChanged;
    214       if (handler != null) handler(this, EventArgs.Empty);
    215     }
    216     public event EventHandler Reset;
    217     private void OnReset() {
    218       EventHandler handler = Reset;
    219       if (handler != null) handler(this, EventArgs.Empty);
    220     }
    221 
    222     private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
    223       Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
    224       Coordinates.Reset += new EventHandler(Coordinates_Reset);
    225       ParameterizeSolutionCreator();
    226       ClearDistanceMatrix();
    227     }
    228     private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
    229       ClearDistanceMatrix();
    230     }
    231     private void Coordinates_Reset(object sender, EventArgs e) {
    232       ParameterizeSolutionCreator();
    233       ClearDistanceMatrix();
    234     }
    235     private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
    236       SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
    237       ParameterizeSolutionCreator();
    238       ParameterizeEvaluator();
    239       ParameterizeAnalyzer();
    240       ParameterizeOperators();
    241       OnSolutionCreatorChanged();
    242     }
    243     private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
    244       ParameterizeEvaluator();
    245       ParameterizeAnalyzer();
    246       ParameterizeOperators();
    247     }
    248     private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
    249       Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    250       ParameterizeEvaluator();
    251       UpdateMoveEvaluators();
    252       ParameterizeAnalyzer();
    253       ClearDistanceMatrix();
    254       OnEvaluatorChanged();
    255     }
    256     private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
    257       ParameterizeAnalyzer();
    258     }
    259     private void MoveGenerator_InversionMoveParameter_ActualNameChanged(object sender, EventArgs e) {
    260       string name = ((ILookupParameter<InversionMove>)sender).ActualName;
    261       foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {
    262         op.InversionMoveParameter.ActualName = name;
    263       }
    264     }
    265     private void MoveGenerator_TranslocationMoveParameter_ActualNameChanged(object sender, EventArgs e) {
    266       string name = ((ILookupParameter<TranslocationMove>)sender).ActualName;
    267       foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
    268         op.TranslocationMoveParameter.ActualName = name;
    269       }
    270     }
    271     #endregion
    272 
    273     #region Helpers
    274     [StorableHook(HookType.AfterDeserialization)]
    275     private void Initialize() {
    276       InitializeOperators();
    277       CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
    278       Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
    279       Coordinates.Reset += new EventHandler(Coordinates_Reset);
    280       SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
    281       SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
    282       EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
    283       Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    284     }
    285 
    286     private void InitializeOperators() {
    287       operators = new List<IOperator>();
    288       operators.Add(new BestTSPSolutionAnalyzer());
    289       ParameterizeAnalyzer();
    290       operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>().Cast<IOperator>());
    291       ParameterizeOperators();
    292       UpdateMoveEvaluators();
    293       InitializeMoveGenerators();
    294     }
    295     private void InitializeMoveGenerators() {
    296       foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {
    297         if (op is IMoveGenerator) {
    298           op.InversionMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_InversionMoveParameter_ActualNameChanged);
    299         }
    300       }
    301       foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {
    302         if (op is IMoveGenerator) {
    303           op.TranslocationMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_TranslocationMoveParameter_ActualNameChanged);
    304         }
    305       }
    306     }
    307     private void UpdateMoveEvaluators() {
    308       operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);
    309       foreach (ITSPPathMoveEvaluator op in ApplicationManager.Manager.GetInstances<ITSPPathMoveEvaluator>())
    310         if (op.EvaluatorType == Evaluator.GetType()) {
    311           operators.Add(op);
    312         }
    313       ParameterizeOperators();
    314       OnOperatorsChanged();
    315     }
    316     private void ParameterizeSolutionCreator() {
    317       SolutionCreator.LengthParameter.Value = new IntValue(Coordinates.Rows);
    318       SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.RelativeUndirected);
    319     }
    320     private void ParameterizeEvaluator() {
    321       if (Evaluator is ITSPPathEvaluator)
    322         ((ITSPPathEvaluator)Evaluator).PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
    323       if (Evaluator is ITSPCoordinatesPathEvaluator) {
    324         ITSPCoordinatesPathEvaluator evaluator = (ITSPCoordinatesPathEvaluator)Evaluator;
    325         evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
    326         evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
    327         evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
    328       }
    329     }
    330     private void ParameterizeAnalyzer() {
    331       BestTSPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
    332       BestTSPSolutionAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
    333       BestTSPSolutionAnalyzer.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
    334       BestTSPSolutionAnalyzer.ResultsParameter.ActualName = "Results";
    335       BestTSPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
    336       BestTSPSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
    337       BestTSPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
    338     }
    339     private void ParameterizeOperators() {
    340       foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) {
    341         op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
    342         op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
    343       }
    344       foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) {
    345         op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
    346       }
    347       foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) {
    348         op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
    349       }
    350       foreach (ITSPPathMoveEvaluator op in Operators.OfType<ITSPPathMoveEvaluator>()) {
    351         op.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
    352         op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
    353         op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
    354         op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
    355         op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
    356       }
    357       string inversionMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationInversionMoveOperator>().First().InversionMoveParameter.ActualName;
    358       foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>())
    359         op.InversionMoveParameter.ActualName = inversionMove;
    360       string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName;
    361       foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>())
    362         op.TranslocationMoveParameter.ActualName = translocationMove;
    363     }
    364 
    365     private void ClearDistanceMatrix() {
    366       DistanceMatrixParameter.Value = null;
    367     }
    368     #endregion
    369375  }
    370376}
  • trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/VehicleRoutingProblem.cs

    r4068 r4098  
    170170      get { return EvaluatorParameter.Value; }
    171171    }
    172     private List<IOperator> operators;
    173172    public IEnumerable<IOperator> Operators {
    174173      get { return operators; }
     
    179178    #endregion
    180179
     180    [Storable]
     181    private List<IOperator> operators;
     182
     183    [StorableConstructor]
     184    private VehicleRoutingProblem(bool deserializing) : base() { }
    181185    public VehicleRoutingProblem()
    182186      : base() {
     
    209213      ParameterizeEvaluator();
    210214
    211       Initialize();
    212     }
    213     [StorableConstructor]
    214     private VehicleRoutingProblem(bool deserializing) : base() { }
     215      InitializeOperators();
     216      AttachEventHandlers();
     217    }
    215218
    216219    public override IDeepCloneable Clone(Cloner cloner) {
    217220      VehicleRoutingProblem clone = (VehicleRoutingProblem)base.Clone(cloner);
     221      clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
    218222      clone.DistanceMatrixParameter.Value = DistanceMatrixParameter.Value;
    219       clone.Initialize();
     223      clone.AttachEventHandlers();
    220224      return clone;
    221     }
    222 
    223     private static double CalculateDistance(int start, int end, DoubleMatrix coordinates) {
    224       double distance = 0.0;
    225 
    226       distance =
    227           Math.Sqrt(
    228             Math.Pow(coordinates[start, 0] - coordinates[end, 0], 2) +
    229             Math.Pow(coordinates[start, 1] - coordinates[end, 1], 2));
    230 
    231       return distance;
    232     }
    233 
    234     private static DoubleMatrix CreateDistanceMatrix(DoubleMatrix coordinates) {
    235       DoubleMatrix distanceMatrix = new DoubleMatrix(coordinates.Rows, coordinates.Rows);
    236 
    237       for (int i = 0; i < distanceMatrix.Rows; i++) {
    238         for (int j = i; j < distanceMatrix.Columns; j++) {
    239           double distance = CalculateDistance(i, j, coordinates);
    240 
    241           distanceMatrix[i, j] = distance;
    242           distanceMatrix[j, i] = distance;
    243         }
    244       }
    245 
    246       return distanceMatrix;
    247     }
    248 
    249     public static double GetDistance(int start, int end,
    250       DoubleMatrix coordinates, ILookupParameter<DoubleMatrix> distanceMatrix, BoolValue useDistanceMatrix) {
    251       double distance = 0.0;
    252 
    253       if (useDistanceMatrix.Value) {
    254         if (distanceMatrix.ActualValue == null) {
    255           distanceMatrix.ActualValue = CreateDistanceMatrix(coordinates);
    256         }
    257 
    258         distance = distanceMatrix.ActualValue[start, end];
    259       } else {
    260         distance = CalculateDistance(start, end, coordinates);
    261       }
    262 
    263       return distance;
    264     }
    265 
    266     public static double GetDistance(int start, int end,
    267       DoubleMatrix coordinates, DoubleMatrix distanceMatrix, BoolValue useDistanceMatrix) {
    268       double distance = 0.0;
    269 
    270       if (useDistanceMatrix.Value) {
    271         distance = distanceMatrix[start, end];
    272       } else {
    273         distance = CalculateDistance(start, end, coordinates);
    274       }
    275 
    276       return distance;
    277     }
    278 
    279     public void ImportFromSolomon(string solomonFileName) {
    280       SolomonParser parser = new SolomonParser(solomonFileName);
    281       parser.Parse();
    282 
    283       this.Name = parser.ProblemName;
    284 
    285       Coordinates = new DoubleMatrix(parser.Coordinates);
    286       Vehicles.Value = parser.Vehicles;
    287       Capacity.Value = parser.Capacity;
    288       Demand = new DoubleArray(parser.Demands);
    289       ReadyTime = new DoubleArray(parser.Readytimes);
    290       DueTime = new DoubleArray(parser.Duetimes);
    291       ServiceTime = new DoubleArray(parser.Servicetimes);
    292 
    293       OnReset();
    294225    }
    295226
     
    364295    #region Helpers
    365296    [StorableHook(HookType.AfterDeserialization)]
    366     private void Initialize() {
    367       InitializeOperators();
     297    private void AttachEventHandlers() {
     298      // Start BackwardsCompatibility3.3 (remove with 3.4)
     299      if (operators == null) InitializeOperators();
     300      // End BackwardsCompatibility3.3
    368301      CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
    369302      Vehicles.ValueChanged += new EventHandler(VehiclesValue_ValueChanged);
     
    471404    }
    472405    #endregion
     406
     407    private static double CalculateDistance(int start, int end, DoubleMatrix coordinates) {
     408      double distance = 0.0;
     409
     410      distance =
     411          Math.Sqrt(
     412            Math.Pow(coordinates[start, 0] - coordinates[end, 0], 2) +
     413            Math.Pow(coordinates[start, 1] - coordinates[end, 1], 2));
     414
     415      return distance;
     416    }
     417
     418    private static DoubleMatrix CreateDistanceMatrix(DoubleMatrix coordinates) {
     419      DoubleMatrix distanceMatrix = new DoubleMatrix(coordinates.Rows, coordinates.Rows);
     420
     421      for (int i = 0; i < distanceMatrix.Rows; i++) {
     422        for (int j = i; j < distanceMatrix.Columns; j++) {
     423          double distance = CalculateDistance(i, j, coordinates);
     424
     425          distanceMatrix[i, j] = distance;
     426          distanceMatrix[j, i] = distance;
     427        }
     428      }
     429
     430      return distanceMatrix;
     431    }
     432
     433    public static double GetDistance(int start, int end,
     434      DoubleMatrix coordinates, ILookupParameter<DoubleMatrix> distanceMatrix, BoolValue useDistanceMatrix) {
     435      double distance = 0.0;
     436
     437      if (useDistanceMatrix.Value) {
     438        if (distanceMatrix.ActualValue == null) {
     439          distanceMatrix.ActualValue = CreateDistanceMatrix(coordinates);
     440        }
     441
     442        distance = distanceMatrix.ActualValue[start, end];
     443      } else {
     444        distance = CalculateDistance(start, end, coordinates);
     445      }
     446
     447      return distance;
     448    }
     449
     450    public static double GetDistance(int start, int end,
     451      DoubleMatrix coordinates, DoubleMatrix distanceMatrix, BoolValue useDistanceMatrix) {
     452      double distance = 0.0;
     453
     454      if (useDistanceMatrix.Value) {
     455        distance = distanceMatrix[start, end];
     456      } else {
     457        distance = CalculateDistance(start, end, coordinates);
     458      }
     459
     460      return distance;
     461    }
     462
     463    public void ImportFromSolomon(string solomonFileName) {
     464      SolomonParser parser = new SolomonParser(solomonFileName);
     465      parser.Parse();
     466
     467      this.Name = parser.ProblemName;
     468
     469      Coordinates = new DoubleMatrix(parser.Coordinates);
     470      Vehicles.Value = parser.Vehicles;
     471      Capacity.Value = parser.Capacity;
     472      Demand = new DoubleArray(parser.Demands);
     473      ReadyTime = new DoubleArray(parser.Readytimes);
     474      DueTime = new DoubleArray(parser.Duetimes);
     475      ServiceTime = new DoubleArray(parser.Servicetimes);
     476
     477      OnReset();
     478    }
    473479  }
    474480}
Note: See TracChangeset for help on using the changeset viewer.