Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Services.ProblemInstances.QAPInitializer/Program.cs @ 6843

Last change on this file since 6843 was 6733, checked in by abeham, 13 years ago

#1619

  • Updated the model slightly
  • Deployed the service
  • Updated the GUI to perform asynchronous calls to the service
File size: 2.8 KB
Line 
1using System;
2using System.Linq;
3using System.Text;
4using HeuristicLab.Problems.QuadraticAssignment;
5
6namespace HeuristicLab.Services.ProblemInstances.QAPInitializer {
7  class Program {
8    static void Main(string[] args) {
9      QuadraticAssignmentProblem qap = new QuadraticAssignmentProblem();
10      using (ProblemInstancesEntities context = new ProblemInstancesEntities()) {
11        foreach (string instance in qap.EmbeddedInstances) {
12          qap.LoadEmbeddedInstance(instance);
13          // transform DoubleMatrix into multidimensional array
14          double[,] weights = new double[qap.Weights.Rows, qap.Weights.Columns];
15          double[,] distances = new double[qap.Distances.Rows, qap.Distances.Columns];
16          for (int i = 0; i < weights.GetLength(0); i++) {
17            for (int j = 0; j < weights.GetLength(1); j++) {
18              weights[i, j] = qap.Weights[i, j];
19              distances[i, j] = qap.Distances[i, j];
20            }
21          }
22
23          QAPInstance dbInstance = context.CreateObject<QAPInstance>();
24          dbInstance.Name = qap.Name;
25          dbInstance.Description = qap.Description;
26          dbInstance.Maximization = qap.Maximization.Value;
27          dbInstance.Weights = SimpleSerializer.SerializeDoubleMatrix(weights);
28          dbInstance.WeightsHash = GetSHA1Hash(dbInstance.Weights);
29          dbInstance.Distances = SimpleSerializer.SerializeDoubleMatrix(distances);
30          dbInstance.DistancesHash = GetSHA1Hash(dbInstance.Distances);
31          dbInstance.ProblemSize = qap.Weights.Rows;
32          context.QAPInstances.AddObject(dbInstance);
33
34          if (qap.BestKnownQuality != null) {
35            QAPSolution dbSolution = context.CreateObject<QAPSolution>();
36            if (qap.BestKnownSolution != null) {
37              dbSolution.Assignment = SimpleSerializer.SerializeIntArray(qap.BestKnownSolution.ToArray());
38              dbSolution.AssignmentHash = GetSHA1Hash(dbSolution.Assignment);
39            }
40            dbSolution.Instance = dbInstance;
41            dbSolution.Quality = qap.BestKnownQuality.Value;
42            context.QAPSolutions.AddObject(dbSolution);
43          }
44        }
45        context.SaveChanges();
46      }
47    }
48
49    public static string GetSHA1Hash(string text) {
50      var SHA1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
51
52      byte[] arrayData;
53      byte[] arrayResult;
54      string result = null;
55      string temp = null;
56
57      arrayData = Encoding.ASCII.GetBytes(text);
58      arrayResult = SHA1.ComputeHash(arrayData);
59      for (int i = 0; i < arrayResult.Length; i++) {
60        temp = Convert.ToString(arrayResult[i], 16);
61        if (temp.Length == 1)
62          temp = "0" + temp;
63        result += temp;
64      }
65      return result;
66    }
67  }
68}
Note: See TracBrowser for help on using the repository browser.