fmin_vis.js 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (factory((global.fmin_vis = global.fmin_vis || {})));
  5. }(this, function (exports) { 'use strict';
  6. function LineGraph() {
  7. var xDomain = [-4.9, 6],
  8. yDomain = [0, 5],
  9. f = function(x) { return Math.log(1 + Math.pow(Math.abs(x), 2+Math.sin(x))); };
  10. function chart(div) {
  11. var width = div.nodes()[0].offsetWidth,
  12. height = width * 0.5,
  13. xScale = d3.scaleLinear()
  14. .domain(xDomain)
  15. .range([0, width]),
  16. yScale = d3.scaleLinear()
  17. .domain(yDomain)
  18. .range([height-8, 8]);
  19. // create svg if not already existing
  20. div.selectAll("svg").data([0]).enter().append("svg");
  21. var svg = div.select("svg")
  22. .attr("width", width)
  23. .attr("height", height);
  24. var colours = d3.schemeCategory10;
  25. function line(f) {
  26. return d3.line()
  27. .x(function (d) { return xScale(d); })
  28. .y(function (d) { return yScale(f(d)); });
  29. }
  30. var samples = 2000;
  31. var data = d3.range(xDomain[0], xDomain[1], xDomain[1]/samples);
  32. var paths = svg.selectAll("path")
  33. .data([f])
  34. .enter()
  35. .append("path")
  36. .attr("stroke", function(d ,i) { return colours[0];})
  37. .attr("stroke-width", 2)
  38. .style("stroke-opacity", 0.8)
  39. .attr("fill", "None")
  40. .attr("d", function(f) {
  41. return line(f)(data);
  42. });
  43. d3.select("body").selectAll(".tooltip").data([0]).enter().append("div")
  44. .attr("class", "tooltip")
  45. .style("font-size", "12px")
  46. .style("position", "absolute")
  47. .style("text-align", "center")
  48. .style("width", "128px")
  49. .style("height", "32px")
  50. .style("background", "#333")
  51. .style("color", "#ddd")
  52. .style("padding", "0px")
  53. .style("border", "0px")
  54. .style("border-radius", "8px")
  55. .style("opacity", "0");
  56. var tooltip = d3.selectAll(".tooltip");
  57. var linetip = svg.append("line")
  58. .attr("stroke-width", 1)
  59. .attr("stroke-opacity", 0)
  60. .attr("stroke", "#CCC");
  61. var circletip = svg.append("circle")
  62. .attr("stroke-opacity", 0)
  63. .attr("fill-opacity", 0)
  64. .attr("r", 3)
  65. .attr("stroke", "#AAA")
  66. .attr("fill", "#AAA");
  67. function showtooltip() {
  68. tooltip.style("opacity", 0.9);
  69. linetip.style("stroke-opacity", 1);
  70. circletip.style("fill-opacity", 1);
  71. tooltip.style("z-index", "");
  72. }
  73. function hidetooltip() {
  74. tooltip.style("z-index", -1);
  75. tooltip.style("opacity", 0);
  76. linetip.style("stroke-opacity", 0);
  77. circletip.style("fill-opacity", 0);
  78. }
  79. svg.attr("width", width)
  80. .attr("height", height)
  81. .on("mouseover", showtooltip)
  82. .on("mouseout", hidetooltip)
  83. .on("mousemove", function() {
  84. var point = d3.mouse(this),
  85. x = xScale.invert(point[0]),
  86. fx = f(x),
  87. y = yScale(fx);
  88. if (y < 0 || y > height) {
  89. hidetooltip();
  90. } else {
  91. showtooltip();
  92. tooltip.style("left", (d3.event.pageX) + "px")
  93. .style("top", (d3.event.pageY - 44) + "px");
  94. tooltip.html("x = " + x.toFixed(2) + "<br>f(x) = " + fx.toFixed(2) );
  95. linetip.attr("x1", point[0])
  96. .attr("x2", point[0])
  97. .attr("y1", point[1])
  98. .attr("y2", yScale(fx));
  99. circletip.attr("cx", point[0])
  100. .attr("cy", yScale(fx));
  101. }
  102. });
  103. return {'svg' : svg, 'xScale' : xScale, 'yScale' : yScale};
  104. }
  105. chart.xDomain = function(_) {
  106. if (!arguments.length) return xDomain;
  107. xDomain = _;
  108. return chart;
  109. };
  110. chart.yDomain = function(_) {
  111. if (!arguments.length) return yDomain;
  112. yDomain = _;
  113. return chart;
  114. };
  115. chart.f = function(_) {
  116. if (!arguments.length) return f;
  117. f = _;
  118. return chart;
  119. };
  120. return chart;
  121. }
  122. function Slider(div, domain, callback, params) {
  123. params = params || {};
  124. var margin = params.margin || {right: 13, left: 10},
  125. height = params.height || 35,
  126. format = params.format || function(d) { return d + "*"; },
  127. width = params.width || div.nodes()[0].offsetWidth;
  128. div.selectAll("svg").data([0]).enter().append("svg");
  129. var svg = div.select("svg")
  130. .attr("width", width)
  131. .attr("height", height);
  132. var x = (params.scale || d3.scaleLinear())
  133. .domain(domain)
  134. .range([0, width - margin.left - margin.right])
  135. .clamp(true);
  136. var slider = svg.append("g")
  137. .attr("class", "slider")
  138. .attr("transform", "translate(" + margin.left + "," + 10 + ")");
  139. slider.append("line")
  140. .attr("class", "track")
  141. .attr("x1", x.range()[0])
  142. .attr("x2", x.range()[1])
  143. .select(function() { return this.parentNode.appendChild(this.cloneNode(true)); })
  144. .attr("class", "track-inset")
  145. .select(function() { return this.parentNode.appendChild(this.cloneNode(true)); })
  146. .attr("class", "track-overlay");
  147. // ugh: stroke-width on safari of track-overlay seems messed. drag on whole svg
  148. // instead
  149. svg
  150. .call(d3.drag()
  151. .on("start.interrupt", function() { slider.interrupt(); })
  152. .on("start drag", function() {
  153. var value = x.invert(d3.event.x);
  154. handle.attr("cx", x(value));
  155. callback(value);
  156. }));
  157. slider.insert("g", ".track-overlay")
  158. .attr("class", "ticks")
  159. .attr("transform", "translate(0," + 18 + ")")
  160. .selectAll("text")
  161. .data(x.ticks(params.ticks || 5))
  162. .enter().append("text")
  163. .attr("x", x)
  164. .attr("text-anchor", "middle")
  165. .text(format);
  166. var handle = slider.insert("circle", ".track-overlay")
  167. .attr("class", "handle")
  168. .attr("r", 9)
  169. .attr("cx", x(params.initial || x.invert(0)));
  170. function move(value, duration) {
  171. handle.transition().duration(duration).attr("cx", x(value));
  172. }
  173. function change(value) {
  174. handle.attr("cx", x(value));
  175. callback(value);
  176. }
  177. return {'change': change, 'move': move};
  178. }
  179. // from http://stackoverflow.com/questions/12277776/how-to-add-drop-shadow-to-d3-js-pie-or-donut-chart
  180. function createDropShadowFilter(svg) {
  181. var defs = svg.selectAll("defs").data([0]).enter().append("defs");
  182. var filter = defs.append("filter")
  183. .attr("id", "dropshadow");
  184. filter.append("feGaussianBlur")
  185. .attr("in", "SourceAlpha")
  186. .attr("stdDeviation", 0.5)
  187. .attr("result", "blur");
  188. filter.append("feOffset")
  189. .attr("in", "blur")
  190. .attr("dx", 1)
  191. .attr("dy", 1)
  192. .attr("result", "offsetBlur");
  193. var feMerge = filter.append("feMerge");
  194. feMerge.append("feMergeNode")
  195. .attr("in", "offsetBlur");
  196. feMerge.append("feMergeNode")
  197. .attr("in", "SourceGraphic");
  198. }
  199. function NelderMead1d(div) {
  200. this.div = div;
  201. this.graph = LineGraph();
  202. this.plot = null;
  203. this.states =[];
  204. this.stateIndex = 0;
  205. this.initial = 0;
  206. this.cycle = 0;
  207. this.params = {'chi' : 1, 'psi' : -1, 'sigma' : 1, 'rho' : 1};
  208. var obj = this, graph = this.obj, params = this.params;
  209. div.select(".function_floor").on("click", function() {
  210. obj.graph.xDomain([25, 75])
  211. .yDomain([0, 25])
  212. .f(function(x) { return Math.floor(Math.abs(x-50)); });
  213. obj.redraw();
  214. obj.initialize([30]);
  215. div.select(".function_label").html(d3.select(this).html());
  216. });
  217. div.select(".function_smooth").on("click", function() {
  218. obj.graph.xDomain([-4.9, 6])
  219. .yDomain([0, 5])
  220. .f(function(x) { return Math.log(1 + Math.pow(Math.abs(x), 2+Math.sin(x))); });
  221. obj.redraw();
  222. obj.initialize([-4.5]);
  223. div.select(".function_label").html(d3.select(this).html());
  224. });
  225. div.select(".function_noisy").on("click", function() {
  226. obj.graph.xDomain([-5, 5])
  227. .yDomain([0, 4])
  228. .f(function(x) { return (2 + Math.sin(50*x)/50) * Math.atan(x) * Math.atan(x);});
  229. obj.redraw();
  230. obj.initialize([-4.5]);
  231. div.select(".function_label").html(d3.select(this).html());
  232. });
  233. this.redraw();
  234. this.initialize([-4.5]);
  235. this.expansion = Slider(div.select("#expansion"), [1, 5],
  236. function(x) {
  237. div.select("#expansionvalue").text(" = " + x.toFixed(1) + "x");
  238. params.chi = x;
  239. obj.initialize(obj.initial);
  240. },
  241. {'format': function(d) { return d.toFixed(1) + "x"; }, 'initial' : 1.0});
  242. this.contraction = Slider(div.select("#contraction"), [0.2, 1],
  243. function(x) {
  244. div.select("#contractionvalue").text(" = " + x.toFixed(2) + "x");
  245. obj.params.sigma = x;
  246. obj.params.psi = -1 * x;
  247. obj.initialize(obj.initial);
  248. },
  249. {'format': function(d) { return (d).toFixed(1) + "x"; }, 'initial' : 1.0});
  250. }
  251. NelderMead1d.prototype.redraw = function() {
  252. this.div.select("svg").data([]).exit().remove();
  253. this.plot = this.graph(this.div.select("#vis"));
  254. createDropShadowFilter(this.plot.svg);
  255. var obj = this;
  256. this.plot.svg.on("click" , function() {
  257. var pos = d3.mouse(this);
  258. obj.initialize([ obj.plot.xScale.invert(pos[0])]);
  259. });
  260. };
  261. NelderMead1d.prototype.initialize = function(initial) {
  262. // stop any previous iteration
  263. this.stop();
  264. this.initial = initial.slice();
  265. this.stateIndex = 0;
  266. var states = this.states = [], graph = this.graph;
  267. this.params.history = states;
  268. fmin.nelderMead(x => this.graph.f()(x[0]), initial, this.params);
  269. var lines = this.plot.svg.selectAll(".simplex_line")
  270. .data(this.states[0].simplex)
  271. .enter()
  272. .append("line")
  273. .attr("class", "simplex_line")
  274. .attr("stroke-opacity", 0.7)
  275. .attr("stroke", "red")
  276. .attr("stroke-width", 2);
  277. var circles = this.plot.svg.selectAll(".simplex_circle")
  278. .data(this.states[0].simplex)
  279. .enter()
  280. .append("circle")
  281. .attr("class", "simplex_circle")
  282. .style("fill", "red")
  283. .style("fill-opacity", 0.9)
  284. .attr("r", 5)
  285. .attr("cx", d => this.plot.xScale(d[0]))
  286. .attr("cy", d => this.plot.yScale(d.fx))
  287. .attr("filter", "url(#dropshadow)");
  288. this.increment(this.cycle, 1500);
  289. };
  290. NelderMead1d.prototype.stop = function() {
  291. this.cycle += 1;
  292. };
  293. NelderMead1d.prototype.start = function() {
  294. this.initialize(this.initial);
  295. };
  296. NelderMead1d.prototype.increment = function(currentCycle, duration) {
  297. if (this.cycle != currentCycle) {
  298. return;
  299. }
  300. this.div.select(".iterations").text("Iteration " + (this.stateIndex + 1) + "/" +
  301. this.states.length + ", Loss=" + this.states[this.stateIndex].fx.toFixed(5));
  302. duration = duration || 500;
  303. var state = this.states[this.stateIndex].simplex;
  304. var lines = this.plot.svg.selectAll(".simplex_line")
  305. .data(state)
  306. .transition()
  307. .duration(this.stateIndex ? duration : 0)
  308. .attr("x1", d => this.plot.xScale(d[0]))
  309. .attr("y1", d => this.plot.yScale(d.fx))
  310. .attr("x2", (d, i) => this.plot.xScale(state[i ? i - 1 : state.length - 1][0]))
  311. .attr("y2", (d, i) => this.plot.yScale(state[i ? i - 1 : state.length - 1].fx));
  312. var circles = this.plot.svg.selectAll(".simplex_circle")
  313. .data(state)
  314. .transition()
  315. .duration(this.stateIndex ? duration : 0)
  316. .attr("cx", d => this.plot.xScale(d[0]))
  317. .attr("cy", d => this.plot.yScale(d.fx));
  318. this.stateIndex += 1;
  319. if (this.stateIndex >= this.states.length) {
  320. this.stateIndex = 0;
  321. duration = 5000;
  322. }
  323. this.plot.svg.transition()
  324. .duration(duration)
  325. .on("end", () => this.increment(currentCycle));
  326. };
  327. var banana = {
  328. 'initial': [-1, -1],
  329. 'f' : function(X) {
  330. var x = X[0], y = X[1];
  331. return (1 - x) * (1 - x) + 100 * (y - x * x) * (y - x * x);
  332. },
  333. 'fprime' : function(X, fxprime) {
  334. fxprime = fxprime || [0, 0];
  335. var x = X[0], y = X[1];
  336. fxprime[0] = 400 * x * x * x - 400 * y * x + 2 * x - 2;
  337. fxprime[1] = 200 * y - 200 * x * x;
  338. return fxprime;
  339. },
  340. 'xDomain': [-2, 2],
  341. 'yDomain': [2, -2]
  342. };
  343. var matyas = {
  344. 'initial': [-9.08, -7.83],
  345. 'f' : function(X) {
  346. var x = X[0], y = X[1];
  347. return 0.26 * (x * x + y * y) - 0.48 * x * y;
  348. },
  349. 'fprime' : function(X, fxprime) {
  350. fxprime = fxprime || [0, 0];
  351. var x = X[0], y = X[1];
  352. fxprime[0] = 0.52 * x - 0.48 * y;
  353. fxprime[1] = 0.52 * y - 0.48 * x;
  354. return fxprime;
  355. },
  356. 'xDomain': [-10, 10],
  357. 'yDomain': [10, -10]
  358. };
  359. var himmelblau = {
  360. 'initial': [-0.15, 0.67],
  361. 'f': function(X) {
  362. var x = X[0], y = X[1];
  363. return (x * x + y - 11) * ( x * x + y - 11) + (x + y * y - 7) * (x + y * y - 7);
  364. },
  365. 'fprime': function(X, fxprime) {
  366. fxprime = fxprime || [0, 0];
  367. var x = X[0], y = X[1];
  368. fxprime[0] = 4 * (x * x + y - 11) * x + 2 * (x + y * y - 7);
  369. fxprime[1] = 2 * (x * x + y - 11) + 4 * (x + y * y - 7) * y;
  370. return fxprime;
  371. },
  372. 'xDomain': [-6.1, 6],
  373. 'yDomain': [6, -6],
  374. 'minima' : [{x : 3.584428, y : -1.848126},
  375. {x : -2.805118, y : 3.131312},
  376. {x : -3.779310, y : -3.283186},
  377. {x : 3, y : 2}],
  378. 'colourDomain' : [2, 13]
  379. };
  380. var flower = {
  381. 'initial': [-3.5, 3.5],
  382. 'f': function(X) {
  383. var x = X[0], y = X[1];
  384. return Math.sin(y) * x + Math.sin(x) * y + x * x + y *y;
  385. },
  386. 'fprime': function(X, fxprime) {
  387. fxprime = fxprime || [0, 0];
  388. var x = X[0], y = X[1];
  389. fxprime[0] = Math.sin(y) + Math.cos(x) * y + 2 * x;
  390. fxprime[1] = Math.sin(x) + Math.cos(y) * x + 2 * y;
  391. return fxprime;
  392. },
  393. 'xDomain': [-6, 6],
  394. 'yDomain': [6, -6]
  395. };
  396. /** finds the zeros of a function, given two starting points (which must
  397. * have opposite signs */
  398. function bisect(f, a, b, parameters) {
  399. parameters = parameters || {};
  400. var maxIterations = parameters.maxIterations || 100,
  401. tolerance = parameters.tolerance || 1e-10,
  402. fA = f(a),
  403. fB = f(b),
  404. delta = b - a;
  405. if (fA * fB > 0) {
  406. throw "Initial bisect points must have opposite signs";
  407. }
  408. if (fA === 0) return a;
  409. if (fB === 0) return b;
  410. for (var i = 0; i < maxIterations; ++i) {
  411. delta /= 2;
  412. var mid = a + delta,
  413. fMid = f(mid);
  414. if (fMid * fA >= 0) {
  415. a = mid;
  416. }
  417. if ((Math.abs(delta) < tolerance) || (fMid === 0)) {
  418. return mid;
  419. }
  420. }
  421. return a + delta;
  422. }
  423. // This file is modified from the d3.geom.contour
  424. // plugin found here https://github.com/d3/d3-plugins/tree/master/geom/contour
  425. /*
  426. Copyright (c) 2012-2015, Michael Bostock
  427. All rights reserved.
  428. Redistribution and use in source and binary forms, with or without
  429. modification, are permitted provided that the following conditions are met:
  430. * Redistributions of source code must retain the above copyright notice, this
  431. list of conditions and the following disclaimer.
  432. * Redistributions in binary form must reproduce the above copyright notice,
  433. this list of conditions and the following disclaimer in the documentation
  434. and/or other materials provided with the distribution.
  435. * The name Michael Bostock may not be used to endorse or promote products
  436. derived from this software without specific prior written permission.
  437. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  438. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  439. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  440. DISCLAIMED. IN NO EVENT SHALL MICHAEL BOSTOCK BE LIABLE FOR ANY DIRECT,
  441. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  442. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  443. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  444. OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  445. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  446. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  447. */
  448. function d3_contour(grid, start) {
  449. var s = start || d3_geom_contourStart(grid), // starting point
  450. c = [], // contour polygon
  451. x = s[0], // current x position
  452. y = s[1], // current y position
  453. dx = 0, // next x direction
  454. dy = 0, // next y direction
  455. pdx = NaN, // previous x direction
  456. pdy = NaN, // previous y direction
  457. i = 0;
  458. do {
  459. // determine marching squares index
  460. i = 0;
  461. if (grid(x-1, y-1)) i += 1;
  462. if (grid(x, y-1)) i += 2;
  463. if (grid(x-1, y )) i += 4;
  464. if (grid(x, y )) i += 8;
  465. // determine next direction
  466. if (i === 6) {
  467. dx = pdy === -1 ? -1 : 1;
  468. dy = 0;
  469. } else if (i === 9) {
  470. dx = 0;
  471. dy = pdx === 1 ? -1 : 1;
  472. } else {
  473. dx = d3_geom_contourDx[i];
  474. dy = d3_geom_contourDy[i];
  475. }
  476. // update contour polygon
  477. if (dx != pdx && dy != pdy) {
  478. c.push([x, y]);
  479. pdx = dx;
  480. pdy = dy;
  481. } else {
  482. c.push([x, y]);
  483. }
  484. x += dx;
  485. y += dy;
  486. } while (s[0] != x || s[1] != y);
  487. return c;
  488. }
  489. // lookup tables for marching directions
  490. var d3_geom_contourDx = [1, 0, 1, 1,-1, 0,-1, 1,0, 0,0,0,-1, 0,-1,NaN];
  491. var d3_geom_contourDy = [0,-1, 0, 0, 0,-1, 0, 0,1,-1,1,1, 0,-1, 0,NaN];
  492. function d3_geom_contourStart(grid) {
  493. var x = 0,
  494. y = 0;
  495. // search for a starting point; begin at origin
  496. // and proceed along outward-expanding diagonals
  497. while (true) {
  498. if (grid(x,y)) {
  499. return [x,y];
  500. }
  501. if (x === 0) {
  502. x = y + 1;
  503. y = 0;
  504. } else {
  505. x = x - 1;
  506. y = y + 1;
  507. }
  508. }
  509. }
  510. function isoline(f, value, xScale, yScale) {
  511. var xRange = xScale.range(), yRange = yScale.range();
  512. return function(x, y) {
  513. if ((x < xRange[0]) || (x > xRange[1]) ||
  514. (y < yRange[0]) || (y > yRange[1])) return false;
  515. return f(xScale.invert(x), yScale.invert(y)) < value;
  516. };
  517. }
  518. function smoothPoints(f, points, level, xScale, yScale) {
  519. var xRange = xScale.range(), yRange = yScale.range();
  520. var ySmooth = function(y) {
  521. return f(xScale.invert(x), yScale.invert(y)) - level;
  522. };
  523. var xSmooth = function(x) {
  524. return f(xScale.invert(x), yScale.invert(y)) - level;
  525. };
  526. for (var k = 0; k < points.length; ++k) {
  527. var point = points[k],
  528. x = point[0], y = point[1];
  529. if ((x <= xRange[0]) || (x >= xRange[1]) ||
  530. (y <= yRange[0]) || (y >= yRange[1])) continue;
  531. var currentSmooth = ySmooth(y);
  532. var p = {'maxIterations' : 9};
  533. for (var delta = 0.5; delta <= 3; delta += 0.5) {
  534. if (ySmooth(y - delta) * currentSmooth < 0) {
  535. y = bisect(ySmooth, y, y - delta, p);
  536. } else if (xSmooth(x - delta) * currentSmooth < 0) {
  537. x = bisect(xSmooth, x, x - delta, p);
  538. } else if (ySmooth(y + delta) * currentSmooth < 0) {
  539. y = bisect(ySmooth, y, y + delta, p);
  540. } else if (xSmooth(x + delta) * currentSmooth < 0) {
  541. x = bisect(xSmooth, x, x + delta, p);
  542. } else {
  543. continue;
  544. }
  545. break;
  546. }
  547. point[0] = x;
  548. point[1] = y;
  549. }
  550. }
  551. function getLogLevels(f, xScale, yScale, count) {
  552. var xRange = xScale.range(), yRange = yScale.range();
  553. // figure out min/max values by sampling pointson a grid
  554. var maxValue, minValue, value;
  555. maxValue = minValue = f(xScale.invert(xRange[0]), yScale.invert(yRange[0]));
  556. for (var y = yRange[0]; y < yRange[1]+1; ++y) {
  557. for (var x = xRange[0]; x < xRange[1]+1; ++x) {
  558. value = f(xScale.invert(x),yScale.invert(y));
  559. minValue = Math.min(value, minValue);
  560. maxValue = Math.max(value, maxValue);
  561. }
  562. }
  563. // lets get contour lines on a log scale, keeping
  564. // values on an integer scale (if possible)
  565. var levels = [];
  566. var logRange = Math.log(maxValue - Math.floor(minValue));
  567. var base = Math.ceil(Math.exp(logRange / (count))),
  568. upper = Math.pow(base, Math.ceil(logRange / Math.log(base)));
  569. for (var i = 0; i < count; ++i) {
  570. var current = Math.floor(minValue) + upper;
  571. if (current < minValue) {
  572. break;
  573. }
  574. levels.push(current);
  575. upper /= base;
  576. }
  577. return levels;
  578. }
  579. function getStartingPoint(lineFunc, x, y) {
  580. x = Math.floor(x);
  581. y = Math.floor(y);
  582. var j = 0;
  583. while (true) {
  584. j += 1;
  585. if (!lineFunc(x+j, y)) {
  586. return [x+j, y];
  587. }
  588. if (!lineFunc(x, y+j)) {
  589. return [x, y+j];
  590. }
  591. }
  592. }
  593. function getContours(f, xScale, yScale, count, minima) {
  594. // figure out even distribution in log space of values
  595. var levels = getLogLevels(f, xScale, yScale, count);
  596. // use marching squares algo from d3.geom.contour to build up a series of paths
  597. var ret = [];
  598. for (var i = 0; i < levels.length; ++i) {
  599. var level = levels[i];
  600. var lineFunc = isoline(f, level, xScale, yScale);
  601. var points= [];
  602. if (minima) {
  603. var initialPoints = [];
  604. for (var m = 0; m < minima.length; ++m) {
  605. var initial = getStartingPoint(lineFunc, xScale(minima[m].x), yScale(minima[m].y));
  606. var current = d3_contour(lineFunc, initial);
  607. // don't add points if already seen
  608. var duplicate = false;
  609. for (var j = 0 ; j < current.length; ++j) {
  610. var point = current[j];
  611. for (var k = 0; k < initialPoints.length; ++k) {
  612. var other = initialPoints[k];
  613. if ((point[0] == other[0]) &&
  614. (point[1] == other[1])) {
  615. duplicate = true;
  616. break;
  617. }
  618. }
  619. if (duplicate) break;
  620. }
  621. if (duplicate) continue;
  622. initialPoints.push(initial);
  623. smoothPoints(f, current, level, xScale, yScale);
  624. if (points.length) points.push(null);
  625. points = points.concat(current);
  626. }
  627. } else {
  628. points = d3_contour(lineFunc);
  629. smoothPoints(f, points, level, xScale, yScale);
  630. }
  631. ret.push(points);
  632. }
  633. // return the contours
  634. return {'paths': ret, 'levels': levels};
  635. }
  636. function ContourPlot() {
  637. var drawAxis = false,
  638. f = function (x, y) { return (1 - x) * (1 - x) + 100 * (y - x * x) * ( y - x * x); },
  639. yDomain = [3, -3],
  640. xDomain = [-2, 2],
  641. minima = null,
  642. contourCount = 14,
  643. colourScale = d3.scaleLinear().domain([0, contourCount]).range(["white", d3.schemeCategory10[0]]);
  644. // todo: resolution independent (sample say 200x200)
  645. // todo: handle function with multiple local minima
  646. function chart(selection) {
  647. var width = selection.nodes()[0].offsetWidth,
  648. height = width * 0.65,
  649. padding = (drawAxis) ? 24 : 0,
  650. yScale = d3.scaleLinear()
  651. .range([padding, height - padding])
  652. .domain(yDomain),
  653. xScale = d3.scaleLinear()
  654. .range([padding, width - padding])
  655. .domain(xDomain);
  656. // create tooltip if doesn't exist
  657. d3.select("body").selectAll(".contour_tooltip").data([0]).enter()
  658. .append("div")
  659. .attr("class", "contour_tooltip")
  660. .style("font-size", "12px")
  661. .style("position", "absolute")
  662. .style("text-align", "center")
  663. .style("width", "128px")
  664. .style("height", "32px")
  665. .style("background", "#333")
  666. .style("color", "#ddd")
  667. .style("padding", "0px")
  668. .style("border", "0px")
  669. .style("border-radius", "8px")
  670. .style("opacity", "0");
  671. var tooltip = d3.selectAll(".contour_tooltip");
  672. // create the svg element if it doesn't already exist
  673. selection.selectAll("svg").data([0]).enter().append("svg");
  674. var svg = selection.selectAll("svg").data([0]);
  675. svg.attr("width", width)
  676. .attr("height", height)
  677. .on("mouseover", function() {
  678. tooltip.transition().duration(400).style("opacity", 0.9);
  679. tooltip.style("z-index", "");
  680. })
  681. .on("mousemove", function() {
  682. var point = d3.mouse(this),
  683. x = xScale.invert(point[0]),
  684. y = yScale.invert(point[1]),
  685. fx = f(x, y);
  686. tooltip.style("left", (d3.event.pageX) + "px")
  687. .style("top", (d3.event.pageY - 44) + "px");
  688. tooltip.html("x = " + x.toFixed(2) + " y = " + y.toFixed(2) + "<br>f(x,y) = " + fx.toFixed(2) );
  689. })
  690. .on("mouseout", function() {
  691. tooltip.transition().duration(400).style("opacity", 0);
  692. tooltip.style("z-index", -1);
  693. });
  694. var contours = getContours(f, xScale, yScale, contourCount, minima);
  695. var paths = contours.paths,
  696. levels = contours.levels;
  697. var line = d3.line()
  698. .x(function(d) { return d[0]; })
  699. .y(function(d) { return d[1]; })
  700. .curve(d3.curveLinearClosed)
  701. .defined(function(d) { return d; });
  702. var pathGroup = svg.append("g");
  703. pathGroup.selectAll("path").data(paths).enter()
  704. .append("path")
  705. .attr("d", line)
  706. .style("fill", function(d, i) { return colourScale(i); })
  707. .style("stroke-width", 1.5)
  708. .style("stroke", "white")
  709. .on("mouseover", function() {
  710. d3.select(this).style("stroke-width", "4");
  711. })
  712. .on("mouseout", function() {
  713. d3.select(this).style("stroke-width", "1.5");
  714. });
  715. // draw axii
  716. if (drawAxis) {
  717. var xAxis = d3.axisBottom().scale(xScale),
  718. yAxis = d3.axisLeft().scale(yScale);
  719. svg.append("g")
  720. .attr("class", "axis")
  721. .attr("transform", "translate(0," + (height - 1.0 * padding) + ")")
  722. .call(xAxis);
  723. svg.append("g")
  724. .attr("class", "axis")
  725. .attr("transform", "translate(" + (padding) + ",0)")
  726. .call(yAxis);
  727. }
  728. return {'xScale' : xScale, 'yScale' : yScale, 'svg' : svg};
  729. }
  730. chart.drawAxis = function(_) {
  731. if (!arguments.length) return drawAxis;
  732. drawAxis = _;
  733. return chart;
  734. };
  735. chart.xDomain = function(_) {
  736. if (!arguments.length) return xDomain;
  737. xDomain = _;
  738. return chart;
  739. };
  740. chart.yDomain = function(_) {
  741. if (!arguments.length) return yDomain;
  742. yDomain = _;
  743. return chart;
  744. };
  745. chart.colourScale = function(_) {
  746. if (!arguments.length) return colourScale;
  747. colourScale = _;
  748. return chart;
  749. };
  750. chart.contourCount = function(_) {
  751. if (!arguments.length) return contourCount;
  752. contourCount = _;
  753. return chart;
  754. };
  755. chart.minima = function(_) {
  756. if (!arguments.length) return minima;
  757. minima = _;
  758. return chart;
  759. };
  760. chart.f = function(_) {
  761. if (!arguments.length) return f;
  762. f = _;
  763. return chart;
  764. };
  765. return chart;
  766. }
  767. function AnimatedContour(div) {
  768. this.current = this.current || himmelblau;
  769. this.initial = this.current.initial.slice() || [1, 1];
  770. this.plot = null;
  771. this.div = div;
  772. this.colour = this.colour || d3.schemeCategory10[0];
  773. this.states =[];
  774. this.stateIndex = 0;
  775. this.cycle = 0;
  776. var contour = this;
  777. div.select(".function_flower").on("click", function() {
  778. contour.current = flower;
  779. contour.redraw();
  780. contour.initialize(contour.current.initial.slice());
  781. div.select(".function_label").html(d3.select(this).html());
  782. });
  783. div.select(".function_himmelblau").on("click", function() {
  784. contour.current = himmelblau;
  785. contour.redraw();
  786. contour.initialize(contour.current.initial.slice());
  787. div.select(".function_label").html(d3.select(this).html());
  788. });
  789. div.select(".function_banana").on("click", function() {
  790. contour.current = banana;
  791. contour.redraw();
  792. contour.initialize(contour.current.initial.slice());
  793. div.select(".function_label").html(d3.select(this).html());
  794. div.select(".function_label").html(d3.select(this).html());
  795. });
  796. div.select(".function_matyas").on("click", function() {
  797. contour.current = matyas;
  798. contour.redraw();
  799. contour.initialize(contour.current.initial.slice());
  800. div.select(".function_label").html(d3.select(this).html());
  801. });
  802. this.redraw();
  803. this.initialize(this.initial);
  804. this.drawControls();
  805. }
  806. AnimatedContour.prototype.redraw = function() {
  807. var colourDomain = this.current.colourDomain || [1, 13],
  808. colourScale = d3.scaleLinear().domain(colourDomain).range(["white", this.colour]);
  809. var plot = ContourPlot()
  810. .f((x,y) => this.current.f([x, y]))
  811. .xDomain(this.current.xDomain)
  812. .yDomain(this.current.yDomain)
  813. .minima(this.current.minima)
  814. .colourScale(colourScale);
  815. // remove old graph if there
  816. this.div.select("svg").data([]).exit().remove();
  817. this.plot = plot(this.div.select("#vis"));
  818. createDropShadowFilter(this.plot.svg);
  819. var svg = this.plot.svg, xScale = this.plot.xScale, yScale = this.plot.yScale, contour = this;
  820. svg.on("click" , function() {
  821. var pos = d3.mouse(this);
  822. contour.initialize([ xScale.invert(pos[0]), yScale.invert(pos[1])]);
  823. });
  824. };
  825. AnimatedContour.prototype.increment = function(currentCycle, duration) {
  826. // hack: prevent incrementing if we've reset
  827. if (currentCycle != this.cycle) {
  828. return true;
  829. }
  830. this.displayState();
  831. this.div.select(".iterations").text("Iteration " + (this.stateIndex + 1) + "/" +
  832. this.states.length + ", Loss=" + this.states[this.stateIndex].fx.toFixed(5));
  833. duration = duration || this.duration;
  834. this.stateIndex += 1;
  835. if (this.stateIndex >= this.states.length) {
  836. this.stateIndex = 0;
  837. duration = 5000;
  838. }
  839. this.plot.svg.transition()
  840. .duration(duration)
  841. .on("end", () => this.increment(currentCycle));
  842. };
  843. AnimatedContour.prototype.stop = function() {
  844. this.cycle += 1;
  845. };
  846. AnimatedContour.prototype.start = function() {
  847. this.initialize(this.initial);
  848. };
  849. function NelderMeadContour(div) {
  850. this.colour = d3.schemeCategory10[0];
  851. this.current = flower;
  852. this.duration = 500;
  853. this.params = {'chi' : 2, 'psi' : -0.5, 'sigma' : 0.5, 'rho' : 1};
  854. AnimatedContour.call(this, div);
  855. }
  856. NelderMeadContour.prototype = Object.create(AnimatedContour.prototype);
  857. NelderMeadContour.prototype.drawControls = function() {
  858. var obj = this, params = this.params, div = this.div;
  859. Slider(div.select("#expansion"), [1, 5],
  860. function(x) {
  861. params.chi = x;
  862. obj.initialize(obj.initial);
  863. div.select("#expansionvalue").text(" = " + x.toFixed(1) + "x");
  864. },
  865. {'format': function(d) { return d.toFixed(1) + "x"; }, 'initial' : 2.0});
  866. Slider(div.select("#contraction"), [0.2, 1],
  867. function(x) {
  868. params.sigma = x;
  869. params.psi = -1 * x;
  870. obj.initialize(obj.initial);
  871. div.select("#contractionvalue").text(" = " + x.toFixed(2) + "x");
  872. },
  873. {'format': function(d) { return (d).toFixed(1) + "x"; },
  874. 'initial': 0.5});
  875. };
  876. NelderMeadContour.prototype.initialize = function(initial) {
  877. this.stop();
  878. this.initial = initial.slice();
  879. var states = this.states = [];
  880. this.stateIndex = 0;
  881. this.params.history = states;
  882. fmin.nelderMead(this.current.f, initial, this.params);
  883. var lines = this.plot.svg.selectAll(".simplex_line").data(this.states[0].simplex);
  884. lines.enter()
  885. .append("line")
  886. .attr("class", "simplex_line")
  887. .attr("stroke-opacity", 0.7)
  888. .attr("stroke", "red")
  889. .attr("stroke-width", 2);
  890. var circles = this.plot.svg.selectAll("circle").data(this.states[0].simplex);
  891. circles.enter()
  892. .append("circle")
  893. .style("fill", "red")
  894. .style("fill-opacity", 0.9)
  895. .attr("r", 5)
  896. .attr("cx", d => this.plot.xScale(d[0]))
  897. .attr("cy", d => this.plot.yScale(d[1]))
  898. .attr("filter", "url(#dropshadow)");
  899. this.increment(this.cycle, this.duration);
  900. };
  901. NelderMeadContour.prototype.displayState = function() {
  902. var duration = duration || this.duration;
  903. var state = this.states[this.stateIndex].simplex;
  904. var lines = this.plot.svg.selectAll(".simplex_line")
  905. .data(state)
  906. .transition()
  907. .duration(this.stateIndex ? duration :0)
  908. .attr("x1", d => this.plot.xScale(d[0]))
  909. .attr("y1", d => this.plot.yScale(d[1]))
  910. .attr("x2", (d, i) => this.plot.xScale(state[i ? i - 1 : 2][0]))
  911. .attr("y2", (d, i) => this.plot.yScale(state[i ? i - 1 : 2][1]));
  912. var circles = this.plot.svg.selectAll("circle")
  913. .data(state)
  914. .transition()
  915. .duration(this.stateIndex ? duration :0)
  916. .attr("cx", d => this.plot.xScale(d[0]))
  917. .attr("cy", d => this.plot.yScale(d[1]));
  918. };
  919. function GradientContour(div) {
  920. this.stepSize = 0.01;
  921. this.colour = this.colour || d3.schemeCategory10[1];
  922. this.duration = this.duration || 500;
  923. this.enableLineSearch = false;
  924. AnimatedContour.call(this, div);
  925. var obj = this;
  926. div.select("#linesearchcheck").on("change", function() {
  927. obj.enableLineSearch =document.getElementById("linesearchcheck").checked;
  928. obj.initialize(obj.initial);
  929. });
  930. }
  931. GradientContour.prototype = Object.create(AnimatedContour.prototype);
  932. GradientContour.prototype.drawControls = function() {
  933. var obj = this;
  934. this.learnRate = Slider(this.div.select("#learningrate"), [0.0001, 1],
  935. // TODO: why can't I just go 'this.setStepSize' here instead?
  936. // feel like I fundamentally am missing something with JS
  937. function(x) { return obj.setStepSize(x); },
  938. {'format': function(d) { return d.toString(); },
  939. 'initial': this.stepSize,
  940. 'scale': d3.scaleLog(),
  941. 'ticks': 4});
  942. };
  943. GradientContour.prototype.setStepSize = function(x) {
  944. this.stepSize = x;
  945. this.initialize(this.initial);
  946. this.div.select("#learningratevalue").text(" = " + x.toFixed(4));
  947. };
  948. GradientContour.prototype.calculateStates = function(initial) {
  949. this.stateIndex = 0;
  950. this.states = [];
  951. var f = (x, fxprime) => { this.current.fprime(x, fxprime); return this.current.f(x); };
  952. var params = {"history": this.states, 'maxIterations' : 5000, 'learnRate' : this.stepSize};
  953. if (this.enableLineSearch) {
  954. fmin.gradientDescentLineSearch(f, initial, params);
  955. } else {
  956. fmin.gradientDescent(f, initial, params);
  957. }
  958. };
  959. GradientContour.prototype.initialize = function(initial) {
  960. this.stop();
  961. this.initial = initial.slice();
  962. this.calculateStates(initial);
  963. var svg = this.plot.svg, xScale = this.plot.xScale, yScale = this.plot.yScale;
  964. svg.selectAll(".current").data([]).exit().remove();
  965. var group = svg.selectAll(".current").data([this.states[0]])
  966. .enter()
  967. .append("g")
  968. .attr("class", "current");
  969. group.append("g")
  970. .attr("class", "under");
  971. group.append("g")
  972. .attr("class", "gradient");
  973. group.append("circle")
  974. .attr("class", "ball")
  975. .style("fill", "red")
  976. .style("fill-opacity", 0.9)
  977. .attr("filter", "url(#dropshadow)")
  978. .attr("r", 5)
  979. .attr("cx", function(d) { return xScale(d.x[0]); })
  980. .attr("cy", function(d) { return yScale(d.x[1]); });
  981. this.increment(this.cycle, this.duration);
  982. };
  983. GradientContour.prototype.displayState = function(){
  984. var state = this.states[this.stateIndex];
  985. var group = this.plot.svg.selectAll(".current")
  986. .data([state])
  987. .transition()
  988. .duration(this.stateIndex ? this.duration :0);
  989. group.select(".ball")
  990. .attr("cx", d => this.plot.xScale(d.x[0]))
  991. .attr("cy", d => this.plot.yScale(d.x[1]));
  992. if (this.stateIndex) {
  993. var d = this.states[this.stateIndex-1];
  994. if (this.enableLineSearch) {
  995. this.learnRate.move(d.learnRate, this.duration);
  996. this.div.select("#learningratevalue").text(" = " + d.learnRate.toFixed(4));
  997. }
  998. var line = this.plot.svg.selectAll(".current .gradient").append("line")
  999. .attr("stroke-opacity", 0.9)
  1000. .attr("stroke", "red")
  1001. .attr("stroke-width", 3)
  1002. .attr("x1", this.plot.xScale(d.x[0]))
  1003. .attr("y1", this.plot.yScale(d.x[1]))
  1004. .attr("x2", this.plot.xScale(d.x[0]))
  1005. .attr("y2", this.plot.yScale(d.x[1]));
  1006. line.transition().duration(this.duration)
  1007. .attr("x2", this.plot.xScale(state.x[0]))
  1008. .attr("y2", this.plot.yScale(state.x[1]));
  1009. } else {
  1010. this.plot.svg.selectAll(".current line").data([]).exit().remove();
  1011. }
  1012. };
  1013. function ConjugateGradientContour(div) {
  1014. this.colour = d3.schemeCategory10[2];
  1015. this.current = banana;
  1016. this.duration = 1000;
  1017. GradientContour.call(this, div);
  1018. }
  1019. ConjugateGradientContour.prototype = Object.create(GradientContour.prototype);
  1020. ConjugateGradientContour.prototype.drawControls = function() {
  1021. // TODO
  1022. };
  1023. ConjugateGradientContour.prototype.redraw = function() {
  1024. // add an arrow marker for the gradients
  1025. GradientContour.prototype.redraw.call(this);
  1026. this.plot.svg.append("marker")
  1027. .attr("id", "arrow")
  1028. .attr("fill", "yellow")
  1029. .attr("fill-opacity", "1")
  1030. .attr("viewBox", "0 0 10 10")
  1031. .attr("refX", 0)
  1032. .attr("refY", 5)
  1033. .attr("markerUnits", "strokeWidth")
  1034. .attr("markerWidth", 5)
  1035. .attr("markerHeight", 5)
  1036. .attr("orient", "auto")
  1037. .append("path")
  1038. .attr("d", "M 0 0 L 10 5 L 0 10 z");
  1039. };
  1040. ConjugateGradientContour.prototype.calculateStates = function(initial) {
  1041. this.stateIndex = 0;
  1042. this.states = [];
  1043. var f = (x, fxprime) => { this.current.fprime(x, fxprime); return this.current.f(x); };
  1044. fmin.conjugateGradient(f, initial, {"history": this.states});
  1045. };
  1046. ConjugateGradientContour.prototype.displayState = function(){
  1047. GradientContour.prototype.displayState.call(this);
  1048. if (this.stateIndex) {
  1049. var d = this.states[this.stateIndex-1];
  1050. this.plot.svg.selectAll(".current .gradient").append("line")
  1051. .attr("stroke-opacity", 0.9)
  1052. .attr("stroke", "yellow")
  1053. .attr("stroke-width", 2)
  1054. .attr("marker-end", "url(#arrow)")
  1055. .attr("x1", this.plot.xScale(d.x[0]))
  1056. .attr("y1", this.plot.yScale(d.x[1]))
  1057. .attr("x2", this.plot.xScale(d.x[0] - d.alpha * d.fxprime[0]))
  1058. .attr("y2", this.plot.yScale(d.x[1] - d.alpha * d.fxprime[1]));
  1059. }
  1060. };
  1061. function LineSearchContour(div) {
  1062. this.duration = 1000;
  1063. this.colour = d3.schemeCategory10[1];
  1064. this.current = matyas;
  1065. this.params = {'c1': 1e-4,'c2': 0.5};
  1066. GradientContour.call(this, div, true);
  1067. }
  1068. LineSearchContour.prototype = Object.create(GradientContour.prototype);
  1069. LineSearchContour.prototype.drawControls = function() {
  1070. var obj = this;
  1071. Slider(this.div.select("#c1"), [1e-5, 1],
  1072. function(x) {
  1073. obj.params.c1 = x;
  1074. obj.div.select("#c1value").text(" = " + x.toFixed(4));
  1075. obj.initialize(obj.initial);
  1076. },
  1077. {'format': function(d) { return d.toString(); },
  1078. 'initial': obj.params.c1,
  1079. 'scale': d3.scaleLog(),
  1080. 'ticks': 5});
  1081. Slider(this.div.select("#c2"), [1e-5, 1],
  1082. function(x) {
  1083. obj.params.c2 = x;
  1084. obj.div.select("#c2value").text(" = " + x.toFixed(4));
  1085. obj.initialize(obj.initial);
  1086. },
  1087. {'format': function(d) { return d.toString(); },
  1088. 'initial': obj.params.c2,
  1089. 'scale': d3.scaleLog(),
  1090. 'ticks': 5});
  1091. };
  1092. LineSearchContour.prototype.calculateStates = function(initial) {
  1093. this.stateIndex = 0;
  1094. this.states = [];
  1095. var f = (x, fxprime) => { this.current.fprime(x, fxprime); return this.current.f(x); };
  1096. this.params.history = this.states;
  1097. this.params.maxIterations = 5000;
  1098. fmin.gradientDescentLineSearch(f, initial, this.params);
  1099. };
  1100. LineSearchContour.prototype.displayState = function(){
  1101. if (this.stateIndex) {
  1102. var d = this.states[this.stateIndex-1];
  1103. var g = this.plot.svg.select(".current .under")
  1104. .append("g");
  1105. g.selectAll("circle")
  1106. .data(d.functionCalls)
  1107. .enter()
  1108. .append("circle")
  1109. .attr("stroke-opacity", 0.8)
  1110. .attr("stroke", "black")
  1111. .attr("stroke-width", 1)
  1112. .attr("fill-opacity", 0)
  1113. .attr("cx", p => this.plot.xScale(p[0]))
  1114. .attr("cy", p => this.plot.yScale(p[1]))
  1115. .attr("r", 3);
  1116. } else {
  1117. this.plot.svg.selectAll(".current .under g").data([]).exit().remove();
  1118. }
  1119. GradientContour.prototype.displayState.call(this);
  1120. };
  1121. function mdsGradient(x, distances, fxprime) {
  1122. var loss = 0, i;
  1123. fxprime = fxprime || fmin.zeros(x.length);
  1124. for (i = 0; i < fxprime.length; ++i) {
  1125. fxprime[i] = 0;
  1126. }
  1127. for (i = 0; i < distances.length; ++i) {
  1128. var xi = x[2 * i], yi = x[2 * i + 1];
  1129. for (var j = i + 1; j < distances.length; ++j) {
  1130. var xj = x[2 * j], yj = x[2 * j + 1],
  1131. dij = distances[i][j];
  1132. var squaredDistance = (xj - xi) * (xj - xi) + (yj - yi) * (yj - yi),
  1133. distance = Math.sqrt(squaredDistance),
  1134. delta = squaredDistance - dij * dij;
  1135. loss += 2 * delta * delta;
  1136. fxprime[2*i] += 4 * delta * (xi - xj);
  1137. fxprime[2*i + 1] += 4 * delta * (yi - yj);
  1138. fxprime[2*j] += 4 * delta * (xj - xi);
  1139. fxprime[2*j + 1] += 4 * delta * (yj - yi);
  1140. }
  1141. }
  1142. return loss;
  1143. }
  1144. function mds(distances, params) {
  1145. // fully normalize solution (so that initial guess is somewhat reasonable looking
  1146. var norm = fmin.norm2(distances.map(fmin.norm2))/(distances.length);
  1147. // TODO: why is this needed for CG? norm = 1; understaqnd for GD
  1148. distances = distances.map(function (row) {
  1149. return row.map(function (value) { return value / norm; });
  1150. });
  1151. params = params || {};
  1152. params.history = [];
  1153. // minimize maintaining history (so we can animate the solution)
  1154. var solver = params.solver || fmin.conjugateGradient,
  1155. solution = solver(function(x, fxprime) { return mdsGradient(x, distances, fxprime); },
  1156. params.initial || fmin.zeros(distances.length*2).map(Math.random),
  1157. params);
  1158. // convert the history back to a matrix of unnormalized (x,y) points
  1159. return params.history.map(function(state) {
  1160. var ret = fmin.zerosM(distances.length, 2);
  1161. for (var i = 0; i < distances.length; ++i) {
  1162. ret[i][0] = norm * state.x[2* i];
  1163. ret[i][1] = norm * state.x[2 * i + 1];
  1164. }
  1165. return ret;
  1166. });
  1167. }
  1168. function normalizeSolution(solution) {
  1169. var finalPositions = solution[solution.length-1];
  1170. // rotate everything in place such that city 0 (vancouver) is
  1171. // directly north of city 1 (Portland)
  1172. var rotation = Math.atan2(finalPositions[0][0] - finalPositions[1][0],
  1173. finalPositions[0][1] - finalPositions[1][1]);
  1174. solution.map(function(positions) {
  1175. var c = Math.cos(rotation),
  1176. s = Math.sin(rotation), x, y;
  1177. for (var i = 0; i < positions.length; ++i) {
  1178. x = positions[i][0];
  1179. y = positions[i][1];
  1180. positions[i][0] = c * x - s * y;
  1181. positions[i][1] = s * x + c * y;
  1182. }
  1183. return positions;
  1184. });
  1185. // mirror solution around X if vancouver isn't east of city 2
  1186. if (finalPositions[0][0] > finalPositions[2][0]) {
  1187. solution.map(function(positions) {
  1188. for (var i = 0; i < positions.length; ++i) {
  1189. positions[i][0] *= -1;
  1190. }
  1191. });
  1192. }
  1193. }
  1194. function getDomain(positions, index) {
  1195. var values = positions.map(function(pos) { return pos[index]; });
  1196. return [Math.min.apply(null, values), Math.max.apply(null, values)];
  1197. }
  1198. function animatedScatterPlot(element, history, labels, duration) {
  1199. // global setup
  1200. var w = element.nodes()[0].offsetWidth,
  1201. h = w * 0.7,
  1202. padding = 20 + w / 20,
  1203. pointRadius = (w > 400 ? 5: 3),
  1204. stopped = false;
  1205. // create the svg element if it doesn't already exist
  1206. element.selectAll("svg").data([0]).enter().append("svg");
  1207. var svg = element.selectAll("svg").data([0])
  1208. .attr("width", w)
  1209. .attr("height", h);
  1210. var under = svg.append("g"),
  1211. over = svg.append("g");
  1212. var colours = d3.schemeCategory20;
  1213. // setup per trial
  1214. var positions = history[history.length-1],
  1215. xDomain = getDomain(positions, 0),
  1216. yDomain = getDomain(positions, 1).reverse();
  1217. var xScale = d3.scaleLinear().
  1218. domain(xDomain)
  1219. .range([padding, w - padding]),
  1220. yScale = d3.scaleLinear().
  1221. domain(yDomain)
  1222. .range([padding, h-padding]);
  1223. function updateState(state) {
  1224. if (stopped) {
  1225. return;
  1226. }
  1227. element.select(".iterations").text("Iteration " + (state + 1) + "/" + history.length);
  1228. var positions = history[state];
  1229. var nodes = over.selectAll("g")
  1230. .data(positions);
  1231. var enter = nodes.enter()
  1232. .append("g");
  1233. enter.append("circle")
  1234. .attr("r", pointRadius)
  1235. .attr("fill", function(d, i) { return colours[i % colours.length]; })
  1236. .attr("cx", function(d) { return xScale(d[0]); })
  1237. .attr("cy", function(d) { return yScale(d[1]); });
  1238. enter.append("text")
  1239. .style("text-anchor", "middle")
  1240. .style("font-size", w < 400 ? "10px" : "12px" )
  1241. .attr("x", function(d, i) { return xScale(d[0]); })
  1242. .attr("y", function(d, i) { return yScale(d[1]) - 2 - pointRadius; })
  1243. .text(function(d, i) { return labels[i]; });
  1244. var update = nodes.transition().duration(duration).ease(d3.easeLinear);
  1245. if (state) {
  1246. var previous = history[state-1];
  1247. under.selectAll(".line" + state)
  1248. .data(positions)
  1249. .enter()
  1250. .append("line")
  1251. .attr("stroke", function(d, i) { return colours[i % colours.length]; })
  1252. .attr("stroke-width", (w > 400 ? 5: 1))
  1253. .attr("stroke-opacity", ".25")
  1254. .attr("x1", function(d, i) { return xScale(previous[i][0]); })
  1255. .attr("y1", function(d, i) { return yScale(previous[i][1]); })
  1256. .attr("x2", function(d, i) { return xScale(previous[i][0]); })
  1257. .attr("y2", function(d, i) { return yScale(previous[i][1]); })
  1258. .transition()
  1259. .duration(duration)
  1260. .ease(d3.easeLinear)
  1261. .attr("x2", function(d) { return xScale(d[0]); })
  1262. .attr("y2", function(d) { return yScale(d[1]); });
  1263. }
  1264. update.select("circle")
  1265. .attr("cx", function(d) { return xScale(d[0]); })
  1266. .attr("cy", function(d) { return yScale(d[1]); });
  1267. update.select("text")
  1268. .attr("x", function(d, i) { return xScale(d[0]); })
  1269. .attr("y", function(d, i) { return yScale(d[1]) - 2 - pointRadius; });
  1270. if (state < history.length - 1) {
  1271. svg.transition().duration(duration).on("end", function() { updateState(state+1);});
  1272. }
  1273. }
  1274. updateState(0);
  1275. return {'setDuration' : function(d) { duration = d; },
  1276. 'stop' : function() { stopped = true; }};
  1277. }
  1278. function createCitiesAnimation(div) {
  1279. var params = {},
  1280. distances = can_us.distances,
  1281. labels = can_us.labels,
  1282. duration = 500,
  1283. count = 20,
  1284. previous = null;
  1285. params.learnRate = 0.002;
  1286. params.maxIterations = 10000;
  1287. function recalculateSolution() {
  1288. if (previous) {
  1289. previous.stop();
  1290. }
  1291. var truncated = distances.slice(0, count).map(function(x) { return x.slice(0, count); });
  1292. var solution = mds(truncated, params);
  1293. normalizeSolution(solution);
  1294. div.select("svg").selectAll("g").data([]).exit().remove();
  1295. previous = animatedScatterPlot(div, solution, labels.slice(0, count), duration);
  1296. }
  1297. function setAlgorithm(name, solver, showSlider) {
  1298. div.select(".learningrateslider").style("display", showSlider? "block" : "none");
  1299. params.solver = solver;
  1300. div.select(".algorithm_label").text(name);
  1301. recalculateSolution();
  1302. return true;
  1303. }
  1304. function setSpeed(speed) {
  1305. duration = speed;
  1306. previous.setDuration(duration);
  1307. div.select(".speed_label").text(speed + "ms / Iteration");
  1308. }
  1309. function setCount(c) {
  1310. count = c;
  1311. div.select(".count_label").text(c + " Cities");
  1312. recalculateSolution();
  1313. }
  1314. function setLearnRate(c) {
  1315. params.learnRate = c;
  1316. div.select("#learningratevalue").text(c.toFixed(5));
  1317. recalculateSolution();
  1318. }
  1319. div.select(".randomize").on("click", recalculateSolution);
  1320. div.select(".algo_cg").on("click", function() { setAlgorithm('Conjugate Gradient', fmin.conjugateGradient); });
  1321. div.select(".algo_gd").on("click", function() {
  1322. setAlgorithm('Gradient Descent', fmin.gradientDescent, true);});
  1323. div.select(".algo_gdls").on("click", function() { setAlgorithm('Gradient Descent w/ Linesearch', fmin.gradientDescentLineSearch); });
  1324. div.select(".algo_neldermead").on("click", function() {
  1325. setSpeed(25);
  1326. if (count > 15) {
  1327. setCount(15);
  1328. }
  1329. setAlgorithm('Nelder Mead', fmin.nelderMead);
  1330. });
  1331. div.select(".speed_500").on("click", function() { setSpeed(500); });
  1332. div.select(".speed_100").on("click", function() { setSpeed(100); });
  1333. div.select(".speed_50").on("click", function() { setSpeed(50); });
  1334. div.select(".speed_25").on("click", function() { setSpeed(25); });
  1335. div.select(".count5").on("click", function() { setCount(5); });
  1336. div.select(".count10").on("click", function() { setCount(10); });
  1337. div.select(".count15").on("click", function() { setCount(15); });
  1338. div.select(".count20").on("click", function() { setCount(20); });
  1339. div.select(".count25").on("click", function() { setCount(25); });
  1340. Slider(div.select("#learningrate"), [0.00001, 0.01],
  1341. function(x) { return setLearnRate(x); },
  1342. {'format': function(d) { return d.toString(); },
  1343. 'initial': params.learnRate,
  1344. 'scale': d3.scaleLog(),
  1345. 'ticks': 3});
  1346. setAlgorithm('Conjugate Gradient', fmin.conjugateGradient);
  1347. }
  1348. exports.NelderMead1d = NelderMead1d;
  1349. exports.NelderMeadContour = NelderMeadContour;
  1350. exports.GradientContour = GradientContour;
  1351. exports.ConjugateGradientContour = ConjugateGradientContour;
  1352. exports.LineSearchContour = LineSearchContour;
  1353. exports.banana = banana;
  1354. exports.flower = flower;
  1355. exports.himmelblau = himmelblau;
  1356. exports.matyas = matyas;
  1357. exports.createCitiesAnimation = createCitiesAnimation;
  1358. }));