-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.html
361 lines (329 loc) · 13.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>GLPK Online</title>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"/>
<link rel="stylesheet" href="codemirror/lib/codemirror.css"/>
<link rel="stylesheet" href="codemirror/addon/dialog/dialog.css">
<link rel="stylesheet" href="codemirror/addon/display/fullscreen.css">
<link rel="stylesheet" href="codemirror/addon/hint/show-hint.css">
<link rel="stylesheet" href="codemirror/addon/search/matchesonscrollbar.css">
<style>
.line-error {
background: #FBC2C4 !important;
color: #8a1f11 !important;
}
.CodeMirror{
height:70vh;
border: 1px solid #eee;
}
.helpLogo{
font-size:.8em;
margin-left:20px;
}
#helpModal h2{
font-size:1.5em;
}
</style>
</head>
<body>
<div class="container">
<h1>GLPK-ONLINE <a href="#" id="helpBtn" onclick="showHelp()"><span class="helpLogo glyphicon glyphicon-question-sign"></span></a></h1>
<div class="row">
<div class="col-md-12">
<textarea id="modelInput" class="form-control"></textarea>
</div>
<div class="col-md-12" style="padding:2em;">
<input type="button" onclick="solve()" value="Solve" class="pull-right btn btn-success" title="CTRL Space"/>
<input type="button" onclick="saveModelToFile('model.txt')" value="Save" class="btn btn-default"/>
<input type="button" onclick="shareModelAsUrl()" title="Store model in URL" value="Share" class="btn btn-default"/>
<input type="button" onclick="myCodeMirror.setValue('')" value="Clear" class="btn btn-danger"/>
</div>
<div class="col-md-12" id="msgZone"></div>
</div>
<div class="row">
<div class="col-md-12">
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><a href="#status" aria-controls="home" role="tab" data-toggle="tab">Summary</a></li>
<li role="presentation"><a href="#log" aria-controls="log" role="tab" data-toggle="tab">Logs</a></li>
<li role="presentation"><a href="#output" aria-controls="output" role="tab" data-toggle="tab">Output</a></li>
<li role="presentation"><a href="#variables" aria-controls="variables" role="tab" data-toggle="tab">Variables</a></li>
<li role="presentation"><a href="#constraints" aria-controls="constraints" role="tab" data-toggle="tab">Constraints</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="status">
<table class="table table-striped">
<tr><td>Status<td><span id="cplxStatus" class="resField"></span>
<tr><td>Objective<td><span id="cplxObj" class="resField"></span>
<tr><td>Direction<td><span id="cplxDirection" class="resField"></span>
<tr><td>Number of Rows<td><span id="cplxNbRows" class="resField"></span>
<tr><td>Number of Columns<td><span id="cplxNbCols" class="resField"></span>
<tr><td>Non-zero elements<td><span id="cplxNbNZ" class="resField"></span>
<tr><td>Number of integer variables<td><span id="cplxNbInt" class="resField"></span>
<tr><td>Number of binary variables<td><span id="cplxNbBool" class="resField"></span>
</table>
</div>
<div role="tabpanel" class="tab-pane" id="log">
<pre id="glpkLog"></pre>
</div>
<div role="tabpanel" class="tab-pane" id="output">
<pre id="glpkOutput"></pre>
</div>
<div role="tabpanel" class="tab-pane table-responsive" id="variables">
<table class="table table-bordered table-striped" id="varTable">
<thead><tr><th>Name<th>Value<th>Type<th>LB<th>UB<th>Objective Coef<th>Primal<th>Dual</tr></thead>
<tbody>
</tbody>
</table>
</div>
<div role="tabpanel" class="tab-pane table-responsive" id="constraints">
<table class="table table-bordered table-striped" id="rowTable">
<thead><tr><th>Name<th>Value<th>LB<th>UB<th>Primal<th>Dual</tr></thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<!--Fork me on github ribbon -->
<a href="https://github.com/cocoto/glpk-online" target="_blank"><img style="position: absolute; top: 0; right: 0; border: 0;" src="forkme.png" alt="Fork me on GitHub"></a>
<div class="modal fade" tabindex="-1" id="helpModal" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">About GLPK-Online</h4>
</div>
<div class="modal-body">
<p><a href="https://github.com/cocoto/glpk-online/" target="_blank">Glpk-Online</a> is an free open-source embedded mixed linear programing solver interface based
on <a href="https://github.com/hgourvest/glpk.js">Glpk.js</a>
</p>
<h2>Features</h2>
<ul>
<li>Mixed Integer Binary Linear Solver
<li>Fully embedded in navigator
<li>GNU Mathprog language (<a href="gmpl.pdf" target="_blank">manual</a>)
<li>Variable and Constraints display
<li>Syntax Highlighting
<li>Auto-complete (ctrl+space)
<li>Drag-and-drop file import
<li>Auto backup (every 30s)
</ul>
<h2>Keyboard Shorcuts</h2>
<i>Only valid in editor</i>
<table class="table table-striped">
<tr><td>Ctrl+Enter<td>Solve
<tr><td>Ctrl+Space<td>Auto-complete
<tr><td>Ctrl+F<td>Search
<tr><td>Ctrl+G<td>Next (search)
<tr><td>Ctrl+Shift+G<td>Previous (search)
<tr><td>Ctrl+Shift+F<td>Search and Replace
<tr><td>Ctrl+D<td>Delete Line
<tr><td>F11<td>Switch fullscreen editor mode
</table>
<h2>External libraries and license</h2>
<ul>
<li> <a href="https://github.com/hgourvest/glpk.js">Glpk.js</a>, GNU Linear Programming Kit for Javascript
<li> <a href="https://github.com/codemirror/CodeMirror/">CodeMirror</a>, In-browser code editor
<li> <a href="https://github.com/jquery/jquery">jQuery</a>, New Wave JavaScript
<li> <a href="https://github.com/twbs/bootstrap">Bootstrap</a>, The most popular HTML, CSS, and JavaScript framework for developing responsive, mobile first projects on the web.
<li> <a href="https://github.com/eligrey/FileSaver.js">FileSaver.js</a>, An HTML5 saveAs() FileSaver implementation
</ul>
<p><small>Copyright © 2017 Quentin Tonneau <qtonneau@gmail.com><br/>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
</small></p>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</body>
<script src="jquery.min.js"></script>
<script src="glpk.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<script src="FileSaver.min.js"></script>
<script src="lz-string.min.js"></script>
<script src="codemirror/lib/codemirror.js"></script>
<script src="codemirror/addon/mode/simple.js"></script>
<script src="codemirror/mode/gmpl/gmpl.js"></script>
<script src="codemirror/addon/edit/matchbrackets.js"></script>
<script src="codemirror/addon/hint/show-hint.js"></script>
<script src="codemirror/addon/hint/anyword-hint.js"></script>
<script src="codemirror/addon/display/fullscreen.js"></script>
<script src="codemirror/addon/dialog/dialog.js"></script>
<script src="codemirror/addon/search/searchcursor.js"></script>
<script src="codemirror/addon/search/search.js"></script>
<script src="codemirror/addon/scroll/annotatescrollbar.js"></script>
<script src="codemirror/addon/search/matchesonscrollbar.js"></script>
<script src="codemirror/addon/search/jump-to-line.js"></script>
<script>
var highlightedLine;
var myCodeMirror = CodeMirror.fromTextArea($("#modelInput")[0],{
lineNumbers:true,
matchBrackets:true,
autofocus:true,
extraKeys: {
"F11": function(cm) {
cm.setOption("fullScreen", !cm.getOption("fullScreen"));
},
"Esc": function(cm) {
if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
},
"Ctrl-Space": "autocomplete",
"Ctrl-Enter": function(){solve();}
}
});
function solve(){
$("#msgZone").html("");
if(highlightedLine) myCodeMirror.removeLineClass(highlightedLine,'background','line-error');
$("#glpkLog").html("");
$("#glpkOutput").html("");
$("#varTable tbody").empty();
$("#rowTable tbody").empty();
$(".resField").empty();
glp_set_print_func(function(data){
$("#glpkLog").append(data+"<br/>");
});
var model = myCodeMirror.getValue();
var lp = glp_create_prob();
var tran = glp_mpl_alloc_wksp();
_glp_mpl_init_rand(tran, 1);
try{
var ret = glp_mpl_read_model_from_string(tran,"model",model,0);
glp_mpl_generate(tran, null, function(data){$("#glpkOutput").append(data+"<br/>")});
glp_mpl_build_prob(tran, lp);
//glp_scale_prob(lp);
var smcp = new SMCP({presolve: GLP_ON});
glp_simplex(lp, smcp);
//Integer Optimizer Parameters
var iocp = new IOCP({presolve: GLP_ON});
glp_intopt(lp, iocp);
//Return back to the mpl model
glp_mpl_postsolve(tran, lp, GLP_MIP);
}catch(err){
if(err.line){
highlightedLine = err.line-1;
myCodeMirror.addLineClass(highlightedLine,'background','line-error');
}
$("#msgZone").append("<div class=' alert alert-danger'>"+err.toString()+"</div>");
console.log(err.toString());
return;
}
var direction
if(glp_get_obj_dir(lp)==GLP_MIN){
direction = "MINIMIZATION";
}else{
direction = "MAXIMIZATION";
}
var status;
switch(glp_mip_status(lp)){
case GLP_OPT : status = "OPTIMAL"; break;
case GLP_UNDEF : status = "UNDEFINED SOLUTION"; break;
case GLP_INFEAS : status = "INFEASIBLE SOLUTION"; break;
case GLP_NOFEAS : status = "NO FEASIBLE SOLUTION"; break;
case GLP_FEAS : status = "FEASIBLE SOLUTION"; break;
case GLP_UNBND : status = "UNBOUNDED SOLUTION"; break;
}
if(status=="OPTIMAL"){
$("#msgZone").append("<div class='alert alert-success'>"+status+" : "+glp_mip_obj_val(lp)+"</div>");
}else{
$("#msgZone").append("<div class='alert alert-warning'>"+status+"</div>");
}
$("#cplxNbRows").html(glp_get_num_rows(lp));
$("#cplxNbCols").html(glp_get_num_cols(lp));
$("#cplxNbNZ").html(glp_get_num_nz(lp));
$("#cplxNbInt").html(glp_get_num_int(lp));
$("#cplxNbBool").html(glp_get_num_bin(lp));
$("#cplxDirection").html(direction);
$("#cplxStatus").html(status);
$("#cplxObj").html(glp_mip_obj_val(lp));
//VARIABLE DISPLAY
for( var i = 1; i <= glp_get_num_cols(lp); i++){
var colType;
switch(glp_get_col_kind(lp,i)){
case GLP_CV : colType="continuous";break;
case GLP_IV : colType="integer";break;
case GLP_BV : colType="binary";break;
}
var ub = glp_get_col_ub(lp, i);
if(ub >= Number.MAX_VALUE){
ub = "+inf";
}
var lb = glp_get_col_lb(lp, i);
if(lb <= -Number.MAX_VALUE){
lb = "-inf";
}
$("#varTable tbody").append("<tr><td>"+glp_get_col_name(lp, i)
+"<td>"+glp_mip_col_val(lp, i)
+"<td>"+colType
+"<td>"+lb
+"<td>"+ub
+"<td>"+glp_get_obj_coef(lp, i)
+"<td>"+glp_get_col_prim(lp, i)
+"<td>"+glp_get_col_dual(lp, i)
+"</tr>");
//console.log(glp_get_col_name(lp, i) + " = " + glp_mip_col_val(lp, i));
}
//CONSTRAINTS DISPLAY
for( var i = 1; i <= glp_get_num_rows(lp); i++){
var ub = glp_get_row_ub(lp, i);
if(ub >= Number.MAX_VALUE){
ub = "+inf";
}
var lb = glp_get_row_lb(lp, i);
if(lb <= -Number.MAX_VALUE){
lb = "-inf";
}
$("#rowTable tbody").append("<tr><td>"+glp_get_row_name(lp, i)
+"<td>"+glp_mip_row_val(lp, i)
+"<td>"+lb
+"<td>"+ub
+"<td>"+glp_get_row_prim(lp, i)
+"<td>"+glp_get_row_dual(lp, i)
+"</tr>");
//console.log(glp_get_col_name(lp, i) + " = " + glp_mip_col_val(lp, i));
}
}
function saveModelToFile(filename){
var blob = new Blob([myCodeMirror.getValue("\r\n")], {type: "text/plain;charset=utf-8"});
saveAs(blob, filename);
}
function shareModelAsUrl(){
var modelString = LZString.compressToEncodedURIComponent(myCodeMirror.getValue("\r\n"));
window.history.pushState({}, document.title, "#" + modelString);
}
/**
* Automatic local save and restore
* Used if browser is accidentally closed
* Save every 30s and restore on page loading
**/
modelHash = window.location.hash;
if(modelHash) {
myCodeMirror.setValue(LZString.decompressFromEncodedURIComponent(modelHash.substring(1)));
window.location.hash = "";
}
else if(localStorage.getItem("savedModel")){
myCodeMirror.setValue(localStorage.getItem("savedModel"));
}
setInterval(function(){
localStorage.setItem("savedModel",myCodeMirror.getValue());
},30000);
function showHelp(){
$("#helpModal").modal("show");
}
</script>
</html>