Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Test with html directly on page
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom-van-Woudenberg committed Apr 24, 2024
1 parent 38f5647 commit 6576992
Show file tree
Hide file tree
Showing 2 changed files with 255 additions and 1 deletion.
123 changes: 123 additions & 0 deletions book/_static/block.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<!DOCTYPE html>
<html>
<head>
<title>Find the optimum solution yourself!</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/numeric/1.2.6/numeric.min.js"></script>
<style>
.slider {
display: block;
margin-bottom: 10px;
width: 100%;
}
.slider-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 10px;
width: 100%;
}
.output {
margin-top: 20px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f9f9f9;
}
.output p {
margin: 5px 0;
}
.output .emoji {
font-size: 20px;
margin-right: 5px;
}
</style>
</head>
<body>
<div id="sliderContainer" class="slider-container"></div>
<div id="outputContainer" class="output"></div>

<script>
function func(x) {
var vol = x[0] * x[1] * x[2];
return vol;
}

function nonlinconfun(x) {
var c1 = 0.8 - x[0] * x[1];
var c2 = 0.8 - x[0] * x[2];
var c3 = 0.8 - x[1] * x[2];
var c4 = 3000 - 2500 * x[0] * x[1] * x[2];
return [c1, c2, c3, c4];
}

function eval() {
var x_1 = parseFloat(document.getElementById("x_1").value);
var x_2 = parseFloat(document.getElementById("x_2").value);
var x_3 = parseFloat(document.getElementById("x_3").value);
var x = [x_1, x_2, x_3];

var output = document.getElementById("outputContainer");
output.innerHTML = "";

var vol = func(x);
output.innerHTML += "<p><strong><span style='font-size: 24px;'>Objective function: " + vol.toFixed(3) + " m<sup>3</sup></span></strong></p>";

var constraints = nonlinconfun(x);
for (var i = 0; i < constraints.length; i++) {
if (i === 3) {
if (constraints[i] < 0) {
output.innerHTML += "<p><span class='emoji'>👍</span> Constraint function " + (i + 1) + ": " + constraints[i].toFixed(0) + " kg</p>";
} else {
output.innerHTML += "<p><span class='emoji'>👎</span> Constraint function " + (i + 1) + ": " + constraints[i].toFixed(0) + " kg</p>";
}
} else {
if (constraints[i] < 0) {
output.innerHTML += "<p><span class='emoji'>👍</span> Constraint function " + (i + 1) + ": " + constraints[i].toFixed(2) + " m<sup>2</sup></p>";
} else {
output.innerHTML += "<p><span class='emoji'>👎</span> Constraint function " + (i + 1) + ": " + constraints[i].toFixed(2) + " m<sup>2</sup></p>";
}
}
}
}
var sliderContainer = document.getElementById("sliderContainer");
var sliders = [
{ id: "x_1", min: 0, max: 5, value: 2, step: 0.01, description: "x<sub>1</sub>" },
{ id: "x_2", min: 0, max: 5, value: 2, step: 0.01, description: "x<sub>2</sub>" },
{ id: "x_3", min: 0, max: 5, value: 2, step: 0.01, description: "x<sub>3</sub>" }
];

sliders.forEach(function(slider) {
var input = document.createElement("input");
input.type = "range";
input.min = slider.min;
input.max = slider.max;
input.value = slider.value;
input.step = slider.step;
input.id = slider.id;
input.oninput = function() {
document.getElementById(slider.id + "_value").textContent = this.value;
eval();
};
input.classList.add("slider");

var label = document.createElement("label");
label.innerHTML = slider.description;
label.setAttribute("for", slider.id);
label.style.textAlign = "right";

var valueDisplay = document.createElement("span"); // Create a span element for value display
valueDisplay.id = slider.id + "_value"; // Set the id for value display
valueDisplay.textContent = slider.value; // Set the initial value

sliderContainer.appendChild(label);
sliderContainer.appendChild(input);
sliderContainer.appendChild(valueDisplay); // Append the value display element
});

var outputContainer = document.getElementById("outputContainer");
var button = document.createElement("button");
button.innerHTML = "Evaluate";
button.onclick = eval;
outputContainer.appendChild(button);
</script>
</body>
</html>
133 changes: 132 additions & 1 deletion book/pages/nonlinear_constrained_optimization_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,130 @@
"\n",
":::{card} Test yourself\n",
"Click {fa}`rocket` --> {guilabel}`Live Code` to enable this applet. Try and adjust the values for $x_1$, $x_2$ and $x_3$. Can you find the optimal solution?\n",
":::"
":::\n",
"\n",
"<html>\n",
"<head>\n",
" <title>Find the optimum solution yourself!</title>\n",
" <script src=\"https://cdnjs.cloudflare.com/ajax/libs/numeric/1.2.6/numeric.min.js\"></script>\n",
" <style>\n",
" .slider {\n",
" display: block;\n",
" margin-bottom: 10px;\n",
" width: 100%;\n",
" }\n",
" .slider-container {\n",
" display: grid;\n",
" grid-template-columns: repeat(3, 1fr);\n",
" grid-gap: 10px;\n",
" width: 100%;\n",
" }\n",
" .output {\n",
" margin-top: 20px;\n",
" padding: 10px;\n",
" border: 1px solid #ccc;\n",
" border-radius: 5px;\n",
" background-color: #f9f9f9;\n",
" }\n",
" .output p {\n",
" margin: 5px 0;\n",
" }\n",
" .output .emoji {\n",
" font-size: 20px;\n",
" margin-right: 5px;\n",
" }\n",
" </style>\n",
"</head>\n",
"<body>\n",
" <div id=\"sliderContainer\" class=\"slider-container\"></div>\n",
" <div id=\"outputContainer\" class=\"output\"></div>\n",
"\n",
" <script>\n",
" function func(x) {\n",
" var vol = x[0] * x[1] * x[2];\n",
" return vol;\n",
" }\n",
"\n",
" function nonlinconfun(x) {\n",
" var c1 = 0.8 - x[0] * x[1];\n",
" var c2 = 0.8 - x[0] * x[2];\n",
" var c3 = 0.8 - x[1] * x[2];\n",
" var c4 = 3000 - 2500 * x[0] * x[1] * x[2];\n",
" return [c1, c2, c3, c4];\n",
" }\n",
"\n",
" function eval() {\n",
" var x_1 = parseFloat(document.getElementById(\"x_1\").value);\n",
" var x_2 = parseFloat(document.getElementById(\"x_2\").value);\n",
" var x_3 = parseFloat(document.getElementById(\"x_3\").value);\n",
" var x = [x_1, x_2, x_3];\n",
"\n",
" var output = document.getElementById(\"outputContainer\");\n",
" output.innerHTML = \"\";\n",
"\n",
" var vol = func(x);\n",
" output.innerHTML += \"<p><strong><span style='font-size: 24px;'>Objective function: \" + vol.toFixed(3) + \" m<sup>3</sup></span></strong></p>\";\n",
"\n",
" var constraints = nonlinconfun(x);\n",
" for (var i = 0; i < constraints.length; i++) {\n",
" if (i === 3) {\n",
" if (constraints[i] < 0) {\n",
" output.innerHTML += \"<p><span class='emoji'>👍</span> Constraint function \" + (i + 1) + \": \" + constraints[i].toFixed(0) + \" kg</p>\";\n",
" } else {\n",
" output.innerHTML += \"<p><span class='emoji'>👎</span> Constraint function \" + (i + 1) + \": \" + constraints[i].toFixed(0) + \" kg</p>\";\n",
" }\n",
" } else {\n",
" if (constraints[i] < 0) {\n",
" output.innerHTML += \"<p><span class='emoji'>👍</span> Constraint function \" + (i + 1) + \": \" + constraints[i].toFixed(2) + \" m<sup>2</sup></p>\";\n",
" } else {\n",
" output.innerHTML += \"<p><span class='emoji'>👎</span> Constraint function \" + (i + 1) + \": \" + constraints[i].toFixed(2) + \" m<sup>2</sup></p>\";\n",
" }\n",
" }\n",
" }\n",
" }\n",
" var sliderContainer = document.getElementById(\"sliderContainer\");\n",
" var sliders = [\n",
" { id: \"x_1\", min: 0, max: 5, value: 2, step: 0.01, description: \"x<sub>1</sub>\" },\n",
" { id: \"x_2\", min: 0, max: 5, value: 2, step: 0.01, description: \"x<sub>2</sub>\" },\n",
" { id: \"x_3\", min: 0, max: 5, value: 2, step: 0.01, description: \"x<sub>3</sub>\" }\n",
" ];\n",
"\n",
" sliders.forEach(function(slider) {\n",
" var input = document.createElement(\"input\");\n",
" input.type = \"range\";\n",
" input.min = slider.min;\n",
" input.max = slider.max;\n",
" input.value = slider.value;\n",
" input.step = slider.step;\n",
" input.id = slider.id;\n",
" input.oninput = function() {\n",
" document.getElementById(slider.id + \"_value\").textContent = this.value;\n",
" eval();\n",
" };\n",
" input.classList.add(\"slider\");\n",
"\n",
" var label = document.createElement(\"label\");\n",
" label.innerHTML = slider.description;\n",
" label.setAttribute(\"for\", slider.id);\n",
" label.style.textAlign = \"right\";\n",
"\n",
" var valueDisplay = document.createElement(\"span\"); // Create a span element for value display\n",
" valueDisplay.id = slider.id + \"_value\"; // Set the id for value display\n",
" valueDisplay.textContent = slider.value; // Set the initial value\n",
"\n",
" sliderContainer.appendChild(label);\n",
" sliderContainer.appendChild(input);\n",
" sliderContainer.appendChild(valueDisplay); // Append the value display element\n",
" });\n",
"\n",
" var outputContainer = document.getElementById(\"outputContainer\");\n",
" var button = document.createElement(\"button\");\n",
" button.innerHTML = \"Evaluate\";\n",
" button.onclick = eval;\n",
" outputContainer.appendChild(button);\n",
" </script>\n",
"</body>\n",
"</html>"
]
},
{
Expand Down Expand Up @@ -178,6 +301,14 @@
" x_3 = widgets.FloatSlider(min=0, max=5, value=2, step=0.01, description=\"x_3\"));"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0cea188a",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "2a609af6",
Expand Down

0 comments on commit 6576992

Please sign in to comment.