1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Text;
|
---|
4 | using HeuristicLab.Problems.QuadraticAssignment;
|
---|
5 |
|
---|
6 | namespace 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 | }
|
---|