Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/02/12 19:09:48 (13 years ago)
Author:
jkarder
Message:

#1331:

  • added operators for TestFunctions problems
  • added more path relinking operators for the TravelingSalesman and the Knapsack problem
  • added 2-tier version of the SolutionPoolUpdateMethod
  • added parameters and adjusted types
  • added some exception handling
  • adjusted event handling
  • updated plugin dependencies
  • minor code improvements
Location:
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/BinaryVectorDiversityCalculator.cs

    r7756 r7775  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    4142    }
    4243
    43     protected override int CalculateDiversity(IItem left, IItem right) {
     44    protected override double CalculateDiversity(IItem left, IItem right) {
     45      if (!(left is BinaryVector) || !(right is BinaryVector))
     46        throw new ArgumentException("Cannot calculate diversity because one of the provided objects or both have the wrong type.");
     47
    4448      var v1 = left as BinaryVector;
    4549      var v2 = right as BinaryVector;
    46       int diversity = 0;
    47       if (v1 != null && v2 != null)
    48         for (int i = 0; i < v1.Length; i++)
    49           if (v1[i] != v2[i]) diversity++;
     50      double diversity = 0;
     51      for (int i = 0; i < v1.Length; i++)
     52        if (v1[i] != v2[i]) diversity++;
    5053      return diversity;
    5154    }
  • branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/KnapsackImprovementOperator.cs

    r7756 r7775  
    6767      get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }
    6868    }
     69    public IValueLookupParameter<DoubleValue> PenaltyParameter {
     70      get { return (IValueLookupParameter<DoubleValue>)Parameters["Penalty"]; }
     71    }
    6972    #region ILocalImprovementOperator Parameters
    7073    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
     
    99102      get { return TargetParameter.ActualValue; }
    100103    }
     104    private DoubleValue Penalty {
     105      get { return PenaltyParameter.ActualValue; }
     106      set { PenaltyParameter.ActualValue = value; }
     107    }
    101108    #endregion
    102109
     
    115122      Parameters.Add(new ValueLookupParameter<IntValue>("KnapsackCapacity"));
    116123      Parameters.Add(new ValueLookupParameter<IItem>("Target"));
     124      Parameters.Add(new ValueLookupParameter<DoubleValue>("Penalty"));
    117125      #endregion
    118126      TargetParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem
     
    128136      // calculate value-to-weight ratio
    129137      double[] ratio = new double[Values.Length];
    130       for (int i = 0; i < ratio.Length; i++) {
     138      for (int i = 0; i < ratio.Length; i++)
    131139        ratio[i] = (double)Values[i] / (double)Weights[i];
    132       }
     140
    133141
    134142      // calculate order for ratio
     
    140148
    141149      int j = sol.Length - 1;
    142       while (KnapsackEvaluator.Apply(sol, KnapsackCapacity, new DoubleValue(1.0), Weights, Values).SumWeights.Value > KnapsackCapacity.Value && j >= 0) {
     150      while (KnapsackEvaluator.Apply(sol, KnapsackCapacity, Penalty, Weights, Values)
     151                              .SumWeights.Value > KnapsackCapacity.Value && j >= 0)
    143152        sol[order[j--]] = false;
    144       }
    145153
    146154      // calculate weight
    147       int lhs = 0;
    148       for (int i = 0; i < sol.Length; i++) {
    149         if (sol[i]) lhs += Weights[i];
    150       }
     155      int weight = 0;
     156      for (int i = 0; i < sol.Length; i++)
     157        if (sol[i]) weight += Weights[i];
    151158
    152159      // improve solution
     
    154161      while (feasible && j < sol.Length) {
    155162        while (sol[order[j]]) j++;
    156         if (lhs + Weights[order[j]] <= KnapsackCapacity.Value) {
     163        if (weight + Weights[order[j]] <= KnapsackCapacity.Value) {
    157164          sol[order[j]] = true;
    158           lhs += Weights[order[j]];
    159         } else {
    160           feasible = false;
    161         }
     165          weight += Weights[order[j]];
     166        } else feasible = false;
    162167      }
    163168
     
    166171      return base.Apply();
    167172    }
    168 
    169     private bool IsFeasiblse(BinaryVector sol) {
    170       int sum = 0;
    171       for (int i = 0; i < sol.Length; i++)
    172         if (sol[i]) sum += Weights[i];
    173       return sum <= KnapsackCapacity.Value;
    174     }
    175173  }
    176174}
  • branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/KnapsackPathRelinker.cs

    r7756 r7775  
    4949
    5050    public static ItemArray<IItem> Apply(IItem initiator, IItem guide, PercentValue n) {
     51      if (!(initiator is BinaryVector) || !(guide is BinaryVector))
     52        throw new ArgumentException("Cannot relink path because one of the provided solutions or both have the wrong type.");
     53      if (n.Value <= 0.0)
     54        throw new ArgumentException("RelinkingAccuracy must be greater than 0.");
     55
    5156      var v1 = initiator.Clone() as BinaryVector;
    5257      var v2 = guide as BinaryVector;
    5358
    5459      if (v1.Length != v2.Length)
    55         throw new ArgumentException("KnapsackPathRelinker: The solutions are of different length.");
    56       if (n.Value == 0.0)
    57         throw new ArgumentException("KnapsackPathRelinker: RelinkingAccuracy is 0.");
     60        throw new ArgumentException("The solutions are of different length.");
    5861
    5962      var solutions = new List<BinaryVector>();
    60       for (int i = 0; i < v1.Length; i++) {
     63      for (int i = 0; i < v1.Length; i++)
    6164        if (v1[i] != v2[i]) {
    6265          v1[i] = v2[i];
    63           solutions.Add((BinaryVector)v1.Clone());
     66          solutions.Add(v1.Clone() as BinaryVector);
    6467        }
    65       }
    6668
    67       var stepSize = 1 / n.Value;
    6869      var selection = new List<IItem>();
    69       for (int i = 0; i < solutions.Count; i = (int)Math.Round(i + stepSize)) {
    70         selection.Add(solutions.ElementAt(i));
     70      if (solutions.Count > 0) {
     71        var noSol = (int)Math.Round(solutions.Count * n.Value);
     72        if (noSol <= 0) noSol++;
     73        var stepSize = (double)solutions.Count / (double)noSol;
     74        for (int i = 0; i < noSol; i++)
     75          selection.Add(solutions.ElementAt((int)Math.Round((i + 1) * stepSize - stepSize * 0.5)));
    7176      }
    7277
     
    7580
    7681    protected override ItemArray<IItem> Relink(ItemArray<IItem> parents, PercentValue n) {
    77       if (parents.Length != 2) throw new ArgumentException("KnapsackPathRelinker: The number of parents is not equal to 2.");
     82      if (parents.Length != 2)
     83        throw new ArgumentException("The number of parents is not equal to 2.");
    7884      return Apply(parents[0], parents[1], n);
    7985    }
  • branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/NBinaryVectorCrossover.cs

    r7756 r7775  
    7272    public sealed override IOperation Apply() {
    7373      var offspringSolutions = Cross(RandomParameter.ActualValue, ParentsParameter.ActualValue);
    74       for (int i = 0; i < offspringSolutions.Length; i++) {
     74      for (int i = 0; i < offspringSolutions.Length; i++)
    7575        CurrentScope.Variables.Add(new Variable(string.Format("Child {0}", i), offspringSolutions[i]));
    76       }
    7776      return base.Apply();
    7877    }
  • branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/NChildCrossover.cs

    r7756 r7775  
    5858      if (parent1.Length != parent2.Length)
    5959        throw new ArgumentException("NChildCrossover: The parents are of different length.");
    60 
    6160      if (n.Value > Math.Pow(2, parent1.Length) - 2)
    6261        throw new ArgumentException("NChildCrossover: There cannot be more children than 2^size of parents - 2.");
    63 
    6462      if (n.Value < 1)
    6563        throw new ArgumentException("NChildCrossover: N cannot be < 1.");
     
    6866      for (int i = 0; i < n.Value; i++) {
    6967        var solution = new BinaryVector(parent1.Length);
    70         for (int j = 0; j < solution.Length; j++) {
     68        for (int j = 0; j < solution.Length; j++)
    7169          solution[j] = random.Next(2) % 2 == 0 ? parent1[j] : parent2[j];
    72         }
    7370        solutions[i] = solution;
    7471      }
     
    7875
    7976    protected override ItemArray<BinaryVector> Cross(IRandom random, ItemArray<BinaryVector> parents) {
    80       if (parents.Length != 2) throw new ArgumentException("NChildCrossover: The number of parents is not equal to 2.");
    81 
    82       if (NParameter.ActualValue == null) throw new InvalidOperationException("NChildCrossover: Parameter " + NParameter.ActualName + " could not be found.");
    83 
     77      if (parents.Length != 2)
     78        throw new ArgumentException("NChildCrossover: The number of parents is not equal to 2.");
     79      if (NParameter.ActualValue == null)
     80        throw new InvalidOperationException("NChildCrossover: Parameter " + NParameter.ActualName + " could not be found.");
    8481      return Apply(random, parents[0], parents[1], NParameter.Value);
    8582    }
Note: See TracChangeset for help on using the changeset viewer.