Skip to content

Commit dbb2974

Browse files
author
Stefan Fenn
committed
Work in progress for bayesian optimization sample
1 parent 252fe84 commit dbb2974

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

baysianOptimization.html

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<style>
2+
.canvasContainer {
3+
position: relative;
4+
}
5+
6+
.canvasContainer canvas {
7+
position: absolute;
8+
left: 0;
9+
top: 0;
10+
}
11+
</style>
12+
13+
<div class="canvasContainer">
14+
<canvas id="grid" width="600px" height="600px"></canvas>
15+
<canvas id="fn2" width="600px" height="600px"></canvas>
16+
<canvas id="observations" width="600px" height="600px"></canvas>
17+
</div>
18+
19+
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/6.6.4/math.min.js"></script>
20+
<script src="graphplot.js" type="module"></script>
21+
<script type="module">
22+
import {drawFunction, drawGrid, drawPoints} from './graphplot.js';
23+
24+
const canvas = document.getElementById("grid");
25+
const config = {
26+
xMin: -0.2,
27+
xMax: 3.4,
28+
yMin: -0.2,
29+
yMax: 4.2,
30+
ctx: canvas.getContext("2d")
31+
};
32+
33+
const canvasF2 = document.getElementById("fn2");
34+
const configF2 = Object.assign({}, config);
35+
configF2.ctx = canvasF2.getContext("2d");
36+
37+
const canvasObs = document.getElementById("observations");
38+
const configObs = Object.assign({}, config);
39+
configObs.ctx = canvasObs.getContext("2d");
40+
41+
drawGrid(config);
42+
const func = x => {
43+
if ((x < 0) || (x > Math.PI)) return 0;
44+
let result = 0;
45+
for (let i = 1; i <= 10; i++) {
46+
result += Math.sin(x) * Math.pow(Math.sin(i * x * x / Math.PI), 20);
47+
}
48+
return result;
49+
};
50+
drawFunction(configF2, "#000000", func);
51+
52+
let observations = [0.9, 1.2, 1.5, 1.6, 2.1, 2.5, 2.8].map(x => [x, func(x)]);
53+
drawPoints(
54+
configObs,
55+
"#0000FF",
56+
observations
57+
);
58+
59+
const kernel = function (x1, x2) {
60+
let n = math.norm(math.subtract(x1, x2));
61+
return math.exp(-0.5 * n * n);
62+
}
63+
console.log(kernel([1,2],[3,5]));
64+
65+
/*
66+
"def K(X0, X1, kernel=sq_exp_kernel):\n",
67+
" K = []\n",
68+
" for x0 in X0:\n",
69+
" k_x_x = []\n",
70+
" for x1 in X1:\n",
71+
" k_x_x.append(kernel(x0, x1))\n",
72+
" K.append(k_x_x)\n",
73+
" if len(K) == 1:\n",
74+
" K = K[0]\n",
75+
" return np.array(K)"
76+
77+
*/
78+
79+
/*
80+
"def posterior_Mean(X, X_new, sigma, y):\n",
81+
" X_new = [X_new]\n",
82+
" return np.dot(np.dot(K(X_new, X), np.linalg.inv(K(X, X)+ sigma*np.eye(len(X)))), np.array([y]).T).flatten()"
83+
*/
84+
</script>
85+
86+
87+
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
88+
<script id="MathJax-script" async
89+
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
90+
</script>
91+
<div style="width:600px;padding-top:620px">
92+
<h3>Plot of function</h3>
93+
94+
<p>
95+
\[ f(x) = \sum_{i=1}^{10}\sin(x)*\sin(\frac{i*x^2}{\pi})^{20} \]
96+
</p>
97+
</div>

graphplot.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ export function drawFunction(c, strokeStyle, func) {
136136
c.ctx.stroke();
137137
}
138138

139+
export function drawPoints(c, fillStyle, points) {
140+
clear(c);
141+
c.ctx.fillStyle = fillStyle;
142+
for(let point of points){
143+
c.ctx.beginPath();
144+
c.ctx.arc(xCoordToPix(c,point[0]), yCoordToPix(c,point[1]), 5, 0, 2 * Math.PI, true);
145+
c.ctx.fill();
146+
}
147+
}
148+
139149
export function getGradientVector(colorMapIndex, levels) {
140150
const gradientColors1 = {
141151
0.0: "rgb(51, 59, 126)",

0 commit comments

Comments
 (0)