Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/07/08 12:00:05 (16 years ago)
Author:
gkronber
Message:

improved BestSolutionStorer (can have a sub-operator graph that is executed for the best solution) (ticket #194)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Logging/BestSolutionStorer.cs

    r363 r364  
    2828
    2929namespace HeuristicLab.Logging {
    30   public class BestSolutionStorer : OperatorBase {
     30  public class BestSolutionStorer : DelegatingOperator {
    3131    public override string Description {
    3232      get { return @"Keeps a variable in the global scope that contains the scope representing the best of run solution."; }
     
    3737      AddVariableInfo(new VariableInfo("Quality", "Quality value of a solution", typeof(DoubleData), VariableKind.In));
    3838      AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In));
    39       AddVariableInfo(new VariableInfo("BestSolution", "The best solution of the run", typeof(IScope), VariableKind.New | VariableKind.Out));
     39      AddVariableInfo(new VariableInfo("BestSolution", "The best solution of the run", typeof(IScope), VariableKind.New | VariableKind.In | VariableKind.Out));
    4040    }
    4141
    4242    public override IOperation Apply(IScope scope) {
    43       double[] qualities = new double[scope.SubScopes.Count];
     43      if(scope.GetVariable(Guid.ToString() + "-Active") == null) {
     44        double[] qualities = new double[scope.SubScopes.Count];
     45        bool maximization = scope.GetVariableValue<BoolData>("Maximization", true).Data;
     46        for(int i = 0; i < scope.SubScopes.Count; i++)
     47          qualities[i] = scope.SubScopes[i].GetVariableValue<DoubleData>("Quality", false).Data;
    4448
    45       for (int i = 0; i < scope.SubScopes.Count; i++)
    46         qualities[i] = scope.SubScopes[i].GetVariableValue<DoubleData>("Quality", false).Data;
     49        double smallest = qualities[0]; int smallestIndex = 0;
     50        double biggest = qualities[0]; int biggestIndex = 0;
     51        for(int i = 1; i < qualities.Length; i++) {
     52          if(qualities[i] < smallest) {
     53            smallest = qualities[i];
     54            smallestIndex = i;
     55          }
     56          if(qualities[i] > biggest) {
     57            biggest = qualities[i];
     58            biggestIndex = i;
     59          }
     60        }
    4761
    48       double smallest = qualities[0]; int smallestIndex = 0;
    49       double biggest = qualities[0]; int biggestIndex = 0;
    50       for (int i = 1; i < qualities.Length; i++) {
    51         if(qualities[i] < smallest) {
    52           smallest = qualities[i];
    53           smallestIndex = i;
     62        IVariable bestSolutionVariable = scope.GetVariable("BestSolution");
     63        if(bestSolutionVariable != null) {
     64
     65          double bestQuality = ((IScope)bestSolutionVariable.Value).GetVariableValue<DoubleData>("Quality", false).Data;
     66
     67          // do nothing if the best solution of the current scope is not better than the best solution of the whole run so far.
     68          if((maximization && biggest <= bestQuality) ||
     69            (!maximization && smallest >= bestQuality)) return null;
    5470        }
    55         if(qualities[i] > biggest) {
    56           biggest = qualities[i];
    57           biggestIndex = i;
     71
     72        IScope bestSolutionClone;
     73        if(maximization) {
     74          bestSolutionClone = (IScope)scope.SubScopes[biggestIndex].Clone();
     75        } else {
     76          bestSolutionClone = (IScope)scope.SubScopes[smallestIndex].Clone();
    5877        }
     78
     79        if(SubOperators.Count > 0) {
     80          scope.AddSubScope(bestSolutionClone);
     81          scope.AddVariable(new Variable(Guid.ToString() + "-Active", new BoolData(true)));
     82
     83          CompositeOperation compOp = new CompositeOperation();
     84          AtomicOperation operation = new AtomicOperation(SubOperators[0], bestSolutionClone);
     85          AtomicOperation continuation = new AtomicOperation(this, scope);
     86
     87          compOp.AddOperation(operation);
     88          compOp.AddOperation(continuation);
     89          return compOp;
     90        } else {
     91          StoreBestSolution(bestSolutionClone, scope);
     92          return null;
     93        }
     94      } else {  // operator already executed
     95        scope.RemoveVariable(Guid.ToString() + "-Active");
     96        IScope bestSolutionClone = scope.SubScopes[scope.SubScopes.Count - 1];
     97        scope.RemoveSubScope(bestSolutionClone);
     98
     99        StoreBestSolution(bestSolutionClone, scope);
     100        return null;
    59101      }
     102    }
    60103
    61       if(!GetVariableValue<BoolData>("Maximization", scope, true).Data) {
    62         SetValue(GetVariableInfo("BestSolution"), (IScope)scope.SubScopes[smallestIndex].Clone(), scope);
    63       } else {
    64         SetValue(GetVariableInfo("BestSolution"), (IScope)scope.SubScopes[biggestIndex].Clone(), scope);
    65       }
    66       return null;
     104    private void StoreBestSolution(IScope bestSolution, IScope scope) {
     105      SetValue(GetVariableInfo("BestSolution"), bestSolution, scope);
    67106    }
    68107
Note: See TracChangeset for help on using the changeset viewer.