Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Web/Mappings/tsp.js @ 9395

Last change on this file since 9395 was 9395, checked in by fschoepp, 11 years ago

#1888:

  • Added visual extensions (dynamic JavaScript) which will be used to render additional result parameters specific to scenarios (e. g. create a graphical representation of a TSP).
  • Added relationship between jobs and experiments (otherwise, it's not possible to get the job's experiment).
  • Updated Admin page to allow removal/addition of visual extensions.
  • Added back-end logic to store/retrieve/delete visual extensions.
  • Added visual extension functionality to the JavaScript views/controllers (job.*.js).
  • Added tsp.js which is a visual extension for the "Genetic Algorithm - TSP" scenario. It adds a graphical representation of the TSP (just like the C# version does) to the results.
File size: 3.0 KB
Line 
1/*
2 * Model ... the domain model
3 * Element ... the table to add new elements to
4 */
5function addExtension(model, element) {
6  /* Specify the name of your row entry */
7  var name = 'TSP Solution';
8
9  /* Create the content of your row by using the model */
10  var content = $('<div/>'); 
11
12  // look up coordinates for TSP
13  var coords = [];
14 
15  coords = _.find(model.get('results'), function(elem) {
16    return elem.Name == "Best TSP Solution";
17  }).Value;
18   
19  // draw TSP
20  var canvasWidth = 400;
21  var canvasHeight = 400;
22  var radius = 6;
23  var canvas = $('<canvas/>').prop({
24      width: canvasWidth,
25      height: canvasHeight
26  }).appendTo(content);
27
28  var ctx = undefined;
29  if (canvas[0].getContext == undefined) {
30    ctx = G_vmlCanvasManager.initElement(canvas[0]).getContext("2d");
31  } else {
32    ctx = canvas[0].getContext("2d");
33  }
34  ctx.fillStyle = "#FFFFFF";   
35  ctx.fillRect(0, 0, canvasWidth, canvasHeight);
36  ctx.fillStyle = "#000000";
37  if (coords && coords.length > 0) {
38      // determine max/min boundings
39    var xMin = Number.MAX_VALUE;
40    var xMax = Number.MIN_VALUE;
41    var yMin = Number.MAX_VALUE;
42    var yMax = Number.MIN_VALUE;
43    for (var i = 0; i < coords.length; i++) {
44      if (xMin > coords[i][0]) xMin = coords[i][0];
45      if (yMin > coords[i][1]) yMin = coords[i][1];
46      if (xMax < coords[i][0]) xMax = coords[i][0];
47      if (yMax < coords[i][1]) yMax = coords[i][1];
48    }
49
50    var border = 20;
51    var xStep = 1;
52    if (xMax != xMin) xStep = (canvasWidth - 2 * border) / (xMax - xMin);
53    var yStep = 1;
54    if (yMax != yMin) yStep = (canvasHeight - 2 * border) / (yMax - yMin);
55
56    ctx.beginPath();
57    var x = Math.round(border + (coords[0][0] - xMin) * xStep);
58    var y = Math.round(canvasHeight - (border + (coords[0][1] - yMin) * yStep));
59    ctx.moveTo(x, y);
60    for (var i = 1; i < coords.length + 1; i++) {
61      var x = Math.round(border + (coords[i % coords.length][0] - xMin) * xStep);
62      var y = Math.round(canvasHeight - (border + (coords[i % coords.length][1] - yMin) * yStep));
63      ctx.lineTo(x, y);
64    }
65    ctx.closePath();
66    ctx.stroke();
67    for (var i = 0; i < coords.length + 1; i++) {
68      var x = Math.round(border + (coords[i % coords.length][0] - xMin) * xStep);
69      var y = Math.round(canvasHeight - (border + (coords[i % coords.length][1] - yMin) * yStep));
70      ctx.beginPath();
71      ctx.arc(x, y, radius, 0, 2 * Math.PI);
72      ctx.closePath();
73      ctx.fill();
74    }
75  } else {
76    ctx.font = "20px Arial";
77    var txt = "No coordinates defined or in wrong format.";
78    var w = ctx.measureText(txt).width;
79    ctx.fillText("No coordinates defined or in wrong format.", (canvasWidth - w) / 2, (canvasHeight - 20) / 2);
80  }
81
82  /* Creates a new row and adds it to element */
83  var row = $('<tr/>');
84  var nameTd = $('<td/>').text(name).appendTo(row);
85  var contentTd = $('<td/>').appendTo(row);
86  content.appendTo(contentTd);
87  row.appendTo(element);
88}
Note: See TracBrowser for help on using the repository browser.