Changeset 7516


Ignore:
Timestamp:
02/24/12 11:39:50 (7 years ago)
Author:
mkommend
Message:

#1691: Updated TabuSearch to use default operators.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.TabuSearch/3.3/TabuSearch.cs

    r7259 r7516  
    348348    private void UpdateMoveGenerator() {
    349349      IMoveGenerator oldMoveGenerator = MoveGenerator;
     350      IMoveGenerator defaultMoveGenerator = Problem.Operators.OfType<IMoveGenerator>().FirstOrDefault();
    350351      MoveGeneratorParameter.ValidValues.Clear();
    351352      if (Problem != null) {
     
    356357      if (oldMoveGenerator != null && MoveGeneratorParameter.ValidValues.Any(x => x.GetType() == oldMoveGenerator.GetType()))
    357358        MoveGenerator = MoveGeneratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveGenerator.GetType());
     359      if (MoveGenerator == null && defaultMoveGenerator != null)
     360        MoveGenerator = defaultMoveGenerator;
     361
    358362      if (MoveGenerator == null) {
    359363        ClearMoveParameters();
     
    363367      IMoveMaker oldMoveMaker = MoveMaker;
    364368      ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator;
    365       ITabuChecker oldTabuMoveEvaluator = TabuChecker;
     369      ITabuChecker oldTabuChecker = TabuChecker;
    366370      ITabuMaker oldTabuMoveMaker = TabuMaker;
    367371      ClearMoveParameters();
     
    372376            moveTypes.Remove(type);
    373377        }
    374         foreach (Type type in moveTypes) {
    375           var operators = Problem.Operators.Where(x => type.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name);
    376           foreach (IMoveMaker moveMaker in operators.OfType<IMoveMaker>())
    377             MoveMakerParameter.ValidValues.Add(moveMaker);
    378           foreach (ISingleObjectiveMoveEvaluator moveEvaluator in operators.OfType<ISingleObjectiveMoveEvaluator>())
    379             MoveEvaluatorParameter.ValidValues.Add(moveEvaluator);
    380           foreach (ITabuChecker tabuMoveEvaluator in operators.OfType<ITabuChecker>())
    381             TabuCheckerParameter.ValidValues.Add(tabuMoveEvaluator);
    382           foreach (ITabuMaker tabuMoveMaker in operators.OfType<ITabuMaker>())
    383             TabuMakerParameter.ValidValues.Add(tabuMoveMaker);
    384         }
     378
     379        var operators = Problem.Operators.Where(op => moveTypes.Any(m => m.IsInstanceOfType(op))).ToList();
     380        IMoveMaker defaultMoveMaker = operators.OfType<IMoveMaker>().FirstOrDefault();
     381        ISingleObjectiveMoveEvaluator defaultMoveEvaluator = operators.OfType<ISingleObjectiveMoveEvaluator>().FirstOrDefault();
     382        ITabuChecker defaultTabuChecker = operators.OfType<ITabuChecker>().FirstOrDefault();
     383        ITabuMaker defaultTabuMaker = operators.OfType<ITabuMaker>().FirstOrDefault();
     384
     385        foreach (IMoveMaker moveMaker in operators.OfType<IMoveMaker>())
     386          MoveMakerParameter.ValidValues.Add(moveMaker);
     387        foreach (ISingleObjectiveMoveEvaluator moveEvaluator in operators.OfType<ISingleObjectiveMoveEvaluator>())
     388          MoveEvaluatorParameter.ValidValues.Add(moveEvaluator);
     389        foreach (ITabuChecker tabuMoveEvaluator in operators.OfType<ITabuChecker>())
     390          TabuCheckerParameter.ValidValues.Add(tabuMoveEvaluator);
     391        foreach (ITabuMaker tabuMoveMaker in operators.OfType<ITabuMaker>())
     392          TabuMakerParameter.ValidValues.Add(tabuMoveMaker);
     393
    385394        if (oldMoveMaker != null) {
    386395          IMoveMaker mm = MoveMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType());
    387396          if (mm != null) MoveMaker = mm;
    388         }
     397          else oldMoveMaker = null;
     398        }
     399        if (oldMoveMaker == null && defaultMoveMaker != null)
     400          MoveMaker = defaultMoveMaker;
     401
    389402        if (oldMoveEvaluator != null) {
    390403          ISingleObjectiveMoveEvaluator me = MoveEvaluatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType());
    391404          if (me != null) MoveEvaluator = me;
    392         }
     405          else oldMoveEvaluator = null;
     406        }
     407        if (oldMoveEvaluator == null && defaultMoveEvaluator != null)
     408          MoveEvaluator = defaultMoveEvaluator;
     409
    393410        if (oldTabuMoveMaker != null) {
    394411          ITabuMaker tmm = TabuMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldTabuMoveMaker.GetType());
    395412          if (tmm != null) TabuMaker = tmm;
    396         }
    397         if (oldTabuMoveEvaluator != null) {
    398           ITabuChecker tme = TabuCheckerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldTabuMoveEvaluator.GetType());
     413          else oldTabuMoveMaker = null;
     414        }
     415        if (oldTabuMoveMaker == null && defaultTabuMaker != null)
     416          TabuMaker = defaultTabuMaker;
     417
     418        if (oldTabuChecker != null) {
     419          ITabuChecker tme = TabuCheckerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldTabuChecker.GetType());
    399420          if (tme != null) TabuChecker = tme;
    400         }
     421          else oldTabuChecker = null;
     422        }
     423        if (oldTabuChecker == null && defaultTabuChecker != null)
     424          TabuChecker = defaultTabuChecker;
    401425      }
    402426    }
Note: See TracChangeset for help on using the changeset viewer.