Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/16/13 13:13:41 (12 years ago)
Author:
spimming
Message:

#1888:

  • Merged revisions from trunk
Location:
branches/OaaS
Files:
8 edited
4 copied

Legend:

Unmodified
Added
Removed
  • branches/OaaS

  • branches/OaaS/HeuristicLab.Problems.QuadraticAssignment

  • branches/OaaS/HeuristicLab.Problems.QuadraticAssignment/3.3/BoundsCalculators/GilmoreLawlerBoundCalculator.cs

    r8092 r9363  
    2222using System;
    2323using HeuristicLab.Data;
     24using HeuristicLab.Encodings.PermutationEncoding;
    2425using HeuristicLab.Problems.LinearAssignment;
    2526
     
    2728  public static class GilmoreLawlerBoundCalculator {
    2829    public static double CalculateLowerBound(DoubleMatrix weights, DoubleMatrix distances) {
     30      Permutation tmp;
     31      return CalculateLowerBound(weights, distances, out tmp);
     32    }
     33
     34    public static double CalculateLowerBound(DoubleMatrix weights, DoubleMatrix distances, out Permutation solution) {
    2935      int N = weights.Rows;
    3036      DoubleMatrix sortedWeights = SortEachRowExceptDiagonal(weights), sortedDistances = SortEachRowExceptDiagonal(distances);
     
    4046
    4147      double result;
    42       LinearAssignmentProblemSolver.Solve(costs, out result);
     48      solution = new Permutation(PermutationTypes.Absolute, LinearAssignmentProblemSolver.Solve(costs, out result));
    4349      return result;
    4450    }
  • branches/OaaS/HeuristicLab.Problems.QuadraticAssignment/3.3/HeuristicLab.Problems.QuadraticAssignment-3.3.csproj

    r7877 r9363  
    125125    <Compile Include="Interfaces\IQAPEvaluator.cs" />
    126126    <Compile Include="Interfaces\IQAPMoveEvaluator.cs" />
     127    <Compile Include="LocalImprovement\QAPExhaustiveInsertionLocalImprovement.cs" />
     128    <Compile Include="LocalImprovement\QAPStochasticScrambleLocalImprovement.cs" />
    127129    <Compile Include="LocalImprovement\QAPExhaustiveSwap2LocalImprovement.cs" />
     130    <Compile Include="LocalImprovement\QAPExhaustiveInversionLocalImprovement.cs" />
    128131    <Compile Include="QAPAssignment.cs" />
    129132    <Compile Include="QAPPermutationProximityCalculator.cs" />
    130133    <Compile Include="QuadraticAssignmentProblem.cs" />
     134    <Compile Include="QAPSimilarityCalculator.cs" />
    131135    <None Include="Plugin.cs.frame" />
    132136    <Compile Include="Plugin.cs" />
     
    178182      <Private>False</Private>
    179183    </ProjectReference>
     184    <ProjectReference Include="..\..\HeuristicLab.Optimization.Operators\3.3\HeuristicLab.Optimization.Operators-3.3.csproj">
     185      <Project>{25087811-f74c-4128-bc86-8324271da13e}</Project>
     186      <Name>HeuristicLab.Optimization.Operators-3.3</Name>
     187      <Private>False</Private>
     188    </ProjectReference>
    180189    <ProjectReference Include="..\..\HeuristicLab.Optimization\3.3\HeuristicLab.Optimization-3.3.csproj">
    181190      <Project>{14AB8D24-25BC-400C-A846-4627AA945192}</Project>
     
    210219  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    211220  <PropertyGroup>
    212     <PreBuildEvent>set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
     221    <PreBuildEvent Condition=" '$(OS)' == 'Windows_NT' ">set Path=%25Path%25;$(ProjectDir);$(SolutionDir)
    213222set ProjectDir=$(ProjectDir)
    214223set SolutionDir=$(SolutionDir)
     
    216225
    217226call PreBuildEvent.cmd</PreBuildEvent>
     227    <PreBuildEvent Condition=" '$(OS)' != 'Windows_NT' ">
     228export ProjectDir=$(ProjectDir)
     229export SolutionDir=$(SolutionDir)
     230
     231$SolutionDir/PreBuildEvent.sh
     232</PreBuildEvent>
    218233  </PropertyGroup>
    219234  <PropertyGroup>
  • branches/OaaS/HeuristicLab.Problems.QuadraticAssignment/3.3/LocalImprovement/QAPExhaustiveSwap2LocalImprovement.cs

    r7878 r9363  
    2121
    2222using System;
     23using System.Threading;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
     
    4647    }
    4748
     49    public ILookupParameter<IntValue> LocalIterationsParameter {
     50      get { return (ILookupParameter<IntValue>)Parameters["LocalIterations"]; }
     51    }
     52
    4853    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
    4954      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
     
    7681    public ILookupParameter<DoubleMatrix> DistancesParameter {
    7782      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
     83    }
     84
     85    public IValueLookupParameter<BoolValue> UseFastEvaluationParameter {
     86      get { return (IValueLookupParameter<BoolValue>)Parameters["UseFastEvaluation"]; }
    7887    }
    7988
     
    8695    public QAPExhaustiveSwap2LocalImprovement()
    8796      : base() {
    88       Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum amount of iterations that should be performed (note that this operator will abort earlier when a local optimum is reached.", new IntValue(10000)));
     97      Parameters.Add(new LookupParameter<IntValue>("LocalIterations", "The number of iterations that have already been performed."));
     98      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum amount of iterations that should be performed (note that this operator will abort earlier when a local optimum is reached).", new IntValue(10000)));
    8999      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The amount of evaluated solutions (here a move is counted only as 4/n evaluated solutions with n being the length of the permutation)."));
    90100      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection where to store results."));
     
    94104      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix."));
    95105      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
     106      Parameters.Add(new ValueLookupParameter<BoolValue>("UseFastEvaluation", "Enabling this option will use a NxN double matrix to save the last move qualities. The moves of the first iteration will then be evaluated in O(N) while all further moves will be evaluated in O(1).", new BoolValue(true)));
    96107    }
    97108
     
    100111    }
    101112
    102     public static double Improve(Permutation assignment, double quality, DoubleMatrix weights, DoubleMatrix distances, bool maximization, int maxIterations, out double evaluatedSolutions) {
    103       evaluatedSolutions = 0.0;
     113    // BackwardsCompatibility3.3
     114    #region Backwards compatible code, remove with 3.4
     115    [StorableHook(HookType.AfterDeserialization)]
     116    private void AfterDeserialization() {
     117      if (!Parameters.ContainsKey("LocalIterations"))
     118        Parameters.Add(new LookupParameter<IntValue>("LocalIterations", "The number of iterations that have already been performed."));
     119      if (!Parameters.ContainsKey("UseFastEvaluation"))
     120        Parameters.Add(new ValueLookupParameter<BoolValue>("UseFastEvaluation", "Enabling this option will use a NxN double matrix to save the last move qualities. The moves of the first iteration will then be evaluated in O(N) while all further moves will be evaluated in O(1).", new BoolValue(false)));
     121    }
     122    #endregion
     123
     124    public static void Improve(Permutation assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, CancellationToken cancellation) {
    104125      double evalSolPerMove = 4.0 / assignment.Length;
    105126
    106       for (int i = 0; i < maxIterations; i++) {
     127      for (int i = localIterations.Value; i < maxIterations; i++) {
    107128        Swap2Move bestMove = null;
    108129        double bestQuality = 0; // we have to make an improvement, so 0 is the baseline
     130        double evaluations = 0.0;
    109131        foreach (Swap2Move move in ExhaustiveSwap2MoveGenerator.Generate(assignment)) {
    110132          double moveQuality = QAPSwap2MoveEvaluator.Apply(assignment, move, weights, distances);
    111           evaluatedSolutions += evalSolPerMove;
     133          evaluations += evalSolPerMove;
     134          if (maximization && moveQuality > bestQuality
     135            || !maximization && moveQuality < bestQuality) {
     136            bestQuality = moveQuality;
     137            bestMove = move;
     138          }
     139        }
     140        evaluatedSolutions.Value += (int)Math.Ceiling(evaluations);
     141        if (bestMove == null) break;
     142        Swap2Manipulator.Apply(assignment, bestMove.Index1, bestMove.Index2);
     143        quality.Value += bestQuality;
     144        localIterations.Value++;
     145        cancellation.ThrowIfCancellationRequested();
     146      }
     147    }
     148
     149    public static void ImproveFast(Permutation assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, CancellationToken cancellation) {
     150      Swap2Move bestMove = null;
     151      double evaluations = 0.0;
     152      var lastQuality = new double[assignment.Length, assignment.Length];
     153      for (int i = localIterations.Value; i < maxIterations; i++) {
     154        double bestQuality = 0; // we have to make an improvement, so 0 is the baseline
     155        var lastMove = bestMove;
     156        bestMove = null;
     157        foreach (Swap2Move move in ExhaustiveSwap2MoveGenerator.Generate(assignment)) {
     158          double moveQuality;
     159          if (lastMove == null) {
     160            moveQuality = QAPSwap2MoveEvaluator.Apply(assignment, move, weights, distances);
     161            evaluations += 4.0 / assignment.Length;
     162          } else {
     163            moveQuality = QAPSwap2MoveEvaluator.Apply(assignment, move, lastQuality[move.Index1, move.Index2], weights, distances, lastMove);
     164            if (move.Index1 == lastMove.Index1 || move.Index2 == lastMove.Index1 || move.Index1 == lastMove.Index2 || move.Index2 == lastMove.Index2)
     165              evaluations += 4.0 / assignment.Length;
     166            else evaluations += 2.0 / (assignment.Length * assignment.Length);
     167          }
     168          lastQuality[move.Index1, move.Index2] = moveQuality;
    112169          if (maximization && moveQuality > bestQuality
    113170            || !maximization && moveQuality < bestQuality) {
     
    118175        if (bestMove == null) break;
    119176        Swap2Manipulator.Apply(assignment, bestMove.Index1, bestMove.Index2);
    120         quality += bestQuality;
     177        quality.Value += bestQuality;
     178        localIterations.Value++;
     179        if (cancellation.IsCancellationRequested) {
     180          evaluatedSolutions.Value += (int)Math.Round(evaluations);
     181          throw new OperationCanceledException();
     182        }
    121183      }
    122       return quality;
     184      evaluatedSolutions.Value += (int)Math.Round(evaluations);
    123185    }
    124186
    125187    public override IOperation Apply() {
    126       int maxIterations = MaximumIterationsParameter.ActualValue.Value;
    127       Permutation assignment = AssignmentParameter.ActualValue;
    128       bool maximization = MaximizationParameter.ActualValue.Value;
    129       DoubleMatrix weights = WeightsParameter.ActualValue;
    130       DoubleMatrix distances = DistancesParameter.ActualValue;
    131       double quality = QualityParameter.ActualValue.Value;
    132 
    133       double evaluations;
    134       QualityParameter.ActualValue.Value = Improve(assignment, quality, weights, distances, maximization, maxIterations, out evaluations);
    135       EvaluatedSolutionsParameter.ActualValue.Value += (int)Math.Ceiling(evaluations);
     188      var maxIterations = MaximumIterationsParameter.ActualValue.Value;
     189      var assignment = AssignmentParameter.ActualValue;
     190      var maximization = MaximizationParameter.ActualValue.Value;
     191      var weights = WeightsParameter.ActualValue;
     192      var distances = DistancesParameter.ActualValue;
     193      var quality = QualityParameter.ActualValue;
     194      var localIterations = LocalIterationsParameter.ActualValue;
     195      var evaluations = EvaluatedSolutionsParameter.ActualValue;
     196      if (localIterations == null) {
     197        localIterations = new IntValue(0);
     198        LocalIterationsParameter.ActualValue = localIterations;
     199      }
     200
     201      if (UseFastEvaluationParameter.ActualValue.Value)
     202        ImproveFast(assignment, weights, distances, quality, localIterations, evaluations, maximization, maxIterations, CancellationToken);
     203      else Improve(assignment, weights, distances, quality, localIterations, evaluations, maximization, maxIterations, CancellationToken);
     204
     205      localIterations.Value = 0;
    136206      return base.Apply();
    137207    }
  • branches/OaaS/HeuristicLab.Problems.QuadraticAssignment/3.3/Plugin.cs.frame

    r7877 r9363  
    2323
    2424namespace HeuristicLab.Problems.QuadraticAssignment {
    25   [Plugin("HeuristicLab.Problems.QuadraticAssignment", "3.3.6.$WCREV$")]
     25  [Plugin("HeuristicLab.Problems.QuadraticAssignment", "3.3.7.$WCREV$")]
    2626  [PluginFile("HeuristicLab.Problems.QuadraticAssignment-3.3.dll", PluginFileType.Assembly)]
    2727  [PluginDependency("HeuristicLab.Analysis", "3.3")]
     
    3434  [PluginDependency("HeuristicLab.Operators", "3.3")]
    3535  [PluginDependency("HeuristicLab.Optimization", "3.3")]
     36  [PluginDependency("HeuristicLab.Optimization.Operators", "3.3")]
    3637  [PluginDependency("HeuristicLab.Parameters", "3.3")]
    3738  [PluginDependency("HeuristicLab.Persistence", "3.3")]
  • branches/OaaS/HeuristicLab.Problems.QuadraticAssignment/3.3/Properties/AssemblyInfo.cs.frame

    r7259 r9363  
    5555// [assembly: AssemblyVersion("1.0.*")]
    5656[assembly: AssemblyVersion("3.3.0.0")]
    57 [assembly: AssemblyFileVersion("3.3.6.$WCREV$")]
     57[assembly: AssemblyFileVersion("3.3.7.$WCREV$")]
  • branches/OaaS/HeuristicLab.Problems.QuadraticAssignment/3.3/QuadraticAssignmentProblem.cs

    r7877 r9363  
    177177      if (!Parameters.ContainsKey("AverageQuality")) {
    178178        Parameters.Add(new OptionalValueParameter<DoubleValue>("AverageQuality", "The expected quality of a random solution."));
    179         AverageQuality = new DoubleValue(Weights.Average() * Distances.Average() * Weights.Rows * Weights.Rows);
     179        AverageQuality = new DoubleValue(ComputeAverageQuality());
    180180      }
    181181      #endregion
     
    303303      Operators.Add(new QAPPopulationDiversityAnalyzer());
    304304      Operators.Add(new QAPExhaustiveSwap2LocalImprovement());
     305      Operators.Add(new QAPSimilarityCalculator());
    305306      ParameterizeAnalyzers();
    306307      ParameterizeOperators();
     
    386387        localOpt.WeightsParameter.ActualName = WeightsParameter.Name;
    387388      }
     389
     390      QAPSimilarityCalculator similarityCalculator = Operators.OfType<QAPSimilarityCalculator>().SingleOrDefault();
     391      if (similarityCalculator != null) {
     392        similarityCalculator.SolutionVariableName = SolutionCreator.PermutationParameter.ActualName;
     393        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
     394      }
    388395    }
    389396
     
    401408
    402409    private void UpdateParameterValues() {
    403       LowerBound = new DoubleValue(GilmoreLawlerBoundCalculator.CalculateLowerBound(Weights, Distances));
    404       AverageQuality = new DoubleValue(Weights.Average() * Distances.Average() * Weights.Rows * Weights.Rows);
     410      Permutation lbSolution;
     411      // calculate the optimum of a LAP relaxation and use it as lower bound of our QAP
     412      LowerBound = new DoubleValue(GilmoreLawlerBoundCalculator.CalculateLowerBound(Weights, Distances, out lbSolution));
     413      // evalute the LAP optimal solution as if it was a QAP solution
     414      var lbSolutionQuality = QAPEvaluator.Apply(lbSolution, Weights, Distances);
     415      // in case both qualities are the same it means that the LAP optimum is also a QAP optimum
     416      if (LowerBound.Value.IsAlmost(lbSolutionQuality)) {
     417        BestKnownSolution = lbSolution;
     418        BestKnownQuality = new DoubleValue(LowerBound.Value);
     419      }
     420      AverageQuality = new DoubleValue(ComputeAverageQuality());
     421    }
     422
     423    private double ComputeAverageQuality() {
     424      double rt = 0, rd = 0, wt = 0, wd = 0;
     425      int n = Weights.Rows;
     426      for (int i = 0; i < n; i++)
     427        for (int j = 0; j < n; j++) {
     428          if (i == j) {
     429            rd += Distances[i, i];
     430            wd += Weights[i, i];
     431          } else {
     432            rt += Distances[i, j];
     433            wt += Weights[i, j];
     434          }
     435        }
     436
     437      return rt * wt / (n * (n - 1)) + rd * wd / n;
    405438    }
    406439    #endregion
     
    412445      Description = data.Description;
    413446      Load(weights, distances);
     447      if (data.BestKnownQuality.HasValue) BestKnownQuality = new DoubleValue(data.BestKnownQuality.Value);
    414448      EvaluateAndLoadAssignment(data.BestKnownAssignment);
    415449      OnReset();
     
    426460      Description = data.Description;
    427461      Load(weights, distances);
     462      if (data.BestKnownQuality.HasValue) BestKnownQuality = new DoubleValue(data.BestKnownQuality.Value);
    428463      EvaluateAndLoadAssignment(data.BestKnownTour);
    429464      OnReset();
Note: See TracChangeset for help on using the changeset viewer.