1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
28 | using HeuristicLab.Problems.QuadraticAssignment.QAPInstanceService;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.QuadraticAssignment {
|
---|
31 | public class QAPService {
|
---|
32 | private Dictionary<string, QAPInstanceDto> instanceCache;
|
---|
33 | private Dictionary<string, QAPSolutionDto[]> solutionsCache;
|
---|
34 |
|
---|
35 | private void Initialize() {
|
---|
36 | instanceCache = new Dictionary<string, QAPInstanceDto>();
|
---|
37 | solutionsCache = new Dictionary<string, QAPSolutionDto[]>();
|
---|
38 | }
|
---|
39 |
|
---|
40 | public bool IsCached(string instance) {
|
---|
41 | return instanceCache != null && instanceCache.ContainsKey(instance);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public void ClearCache() {
|
---|
45 | instanceCache.Clear();
|
---|
46 | solutionsCache.Clear();
|
---|
47 | }
|
---|
48 |
|
---|
49 | public void Configure(QuadraticAssignmentProblem problem, QAPClient client, string instance, Action<int> progressReporter) {
|
---|
50 | if (string.IsNullOrEmpty(instance)) return;
|
---|
51 | if (progressReporter != null) progressReporter(10);
|
---|
52 | QAPInstanceDto data;
|
---|
53 | if (instanceCache == null) Initialize();
|
---|
54 | if (instanceCache.ContainsKey(instance)) data = instanceCache[instance];
|
---|
55 | else {
|
---|
56 | data = client.GetProblemInstanceData(instance);
|
---|
57 | instanceCache.Add(instance, data);
|
---|
58 | }
|
---|
59 | if (progressReporter != null) progressReporter(50);
|
---|
60 | DoubleMatrix weights = new DoubleMatrix(data.Weights.Length, data.Weights.Length);
|
---|
61 | DoubleMatrix distances = new DoubleMatrix(data.Weights.Length, data.Weights.Length);
|
---|
62 | try {
|
---|
63 | for (int i = 0; i < data.Weights.Length; i++)
|
---|
64 | for (int j = 0; j < data.Weights.Length; j++) {
|
---|
65 | weights[i, j] = data.Weights[i][j];
|
---|
66 | distances[i, j] = data.Distances[i][j];
|
---|
67 | }
|
---|
68 | } catch (IndexOutOfRangeException) {
|
---|
69 | throw new InvalidOperationException("The problem data is malformed, the problem could not be loaded.");
|
---|
70 | }
|
---|
71 | if (progressReporter != null) progressReporter(65);
|
---|
72 | problem.Name = data.Name;
|
---|
73 | problem.Description = data.Description;
|
---|
74 | problem.Maximization.Value = data.Maximization;
|
---|
75 | problem.Weights = weights;
|
---|
76 | problem.Distances = distances;
|
---|
77 |
|
---|
78 | problem.BestKnownQualityParameter.Value = null;
|
---|
79 | problem.BestKnownSolution = null;
|
---|
80 | problem.BestKnownSolutions = new ItemSet<Permutation>();
|
---|
81 | QAPSolutionDto[] solutions;
|
---|
82 | if (solutionsCache.ContainsKey(instance)) solutions = solutionsCache[instance];
|
---|
83 | else {
|
---|
84 | solutions = client.GetBestSolutionsData(instance);
|
---|
85 | solutionsCache.Add(instance, solutions);
|
---|
86 | }
|
---|
87 | if (progressReporter != null) progressReporter(90);
|
---|
88 | if (solutions.Any()) {
|
---|
89 | problem.BestKnownQualityParameter.Value = new DoubleValue(solutions.First().Quality);
|
---|
90 | if (solutions.First().Assignment != null && solutions.First().Assignment.Length > 0)
|
---|
91 | problem.BestKnownSolution = new Permutation(PermutationTypes.Absolute, solutions.First().Assignment);
|
---|
92 | foreach (var solution in solutions) {
|
---|
93 | if (solution.Assignment != null && solution.Assignment.Length > 0)
|
---|
94 | problem.BestKnownSolutions.Add(new Permutation(PermutationTypes.Absolute, solution.Assignment));
|
---|
95 | }
|
---|
96 | }
|
---|
97 | if (progressReporter != null) progressReporter(100);
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|