-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.pl
More file actions
executable file
·384 lines (339 loc) · 13.6 KB
/
control.pl
File metadata and controls
executable file
·384 lines (339 loc) · 13.6 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/perl
## Colors are extracted from VBI logo:
# #009977 green
# #226677 blue
## Hussein Vastani
## Franziska Hinkelmann
## Bonbons
## June 2010
## Visualizer for controlled Polynomial Dynamical Systems
use CGI qw( :standard );
use Fcntl qw( :flock );
`mkdir -p ../../htdocs/no-ssl`;
`touch ../../htdocs/no-ssl/accessControl`;
#get the clients ip address
$clientip = $ENV{'REMOTE_ADDR'};
$clientip =~ s/\./\-/g;
($sec,$min,$hr) = localtime();
$clientip = $clientip.'-'.$sec.'-'.$min.'-'.$hr;
$clientip = '../../htdocs/no-ssl/files/'. $clientip;
#$clientip = $sec.'-'.$min.'-'.$hr;
print header, start_html( -title=>'Visualizer of Controlled Polynomial Dynamical Systems Web Interface',
-script=>{-language=>'JavaScript',-src=>'/fnct2.js'},
-head=>[Link({-rel=>'icon',-type=>'image/png',-href=>'https://www.vbi.vt.edu/images/favicon.ico'}),]);
#print "<body background=\"https://www.vbi.vt.edu/templates/vbi/images/background-body-vbi.png\" link=\"#009977\" vlink=\"#226677\">";
#print "<body background=\"http://dvd.vbi.vt.edu/gradient.gif\" link=\"#009977\" vlink=\"#226677\">";
print start_multipart_form(-name=>'form1', -method =>"POST", -onSubmit=>"return validate()");
print "<div style=\"font-family:Verdana,Arial\"><div id=\"tipDiv\" style=\"position:absolute\; visibility:hidden\; z-index:100\"></div><br>";
print "<table width=\"100%\" border=\"0\" cellpadding=\"10\" cellspacing=\"5\">";
print "<tr><td width=\"7%\"></td> <td align=right><img src=\"http://dvd.vbi.vt.edu/vbi-logo.png\"></td> <td align=left> <b><font size=\"5\">Visualizer of Controlled Polynomial Dynamical Systems v0.9 </font></b><br>";
#print "<font size=2><a href=\"http://www.math.vt.edu/people/fhinkel/\">Franziska Hinkelmann</a></font><p>
print "</td></tr>";
print "<tr><td colspan=3 align=center>";
print "You can visualize a controlled Polynomial dynamical system. This is experimental, please be patient with us. Thank you for trying it out! <br>
If you have any questions or comments, <a href=\"mailto:fhinkel\@vt.edu\">please email Franziska Hinkelmann</a>! </td></tr></table>";
#print "<table background=\"http://dvd.vbi.vt.edu/gradient.gif\" width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"10\">";
print "<table width=\"100%\" border=\"0\" cellpadding=\"30\" cellspacing=\"10\">";
## This is the box around Network Description
print "<tr valign=top><td width=50%>";
print "<table width=\"100%\" cellSpacing=\"0\" cellPadding=\"1\" align=\"center\" bgColor=\"#ababab\" border=\"0\"><tr><td>";
print "<table border=\"0\" bgcolor=\"#FFFFCC\" width=\"100%\" cellspacing=\"0\" cellpadding=\"1\"><tr><td bgcolor=\"#666666\" nowrap>";
print "<strong><font color=\"#FFFFFF\">Network Description</font></strong></td></tr><tr><td BGCOLOR=\"#DCDCDC\" HEIGHT=\"1\"></td></tr>";
## number of state variables
print "<tr><td BGCOLOR=\"#DCDCDC\" HEIGHT=\"1\"></td></tr>";
print "<tr valign=\"top\"><td nowrap><font size=\"2\"><br>Enter number of state variables: </font>",
textfield(-name=>'n_nodes', -size=>2, -maxlength=>2, -default=>4),
"<font size=2> (For more than 10 variables, no graph is generated)</font>";
print "<br>";
print "<br>";
## number of control variables
print "</td></tr><tr><td BGCOLOR=\"#DCDCDC\" HEIGHT=\"1\"></td></tr>";
print "<tr valign=\"top\"><td nowrap><font size=\"2\"><br>Enter number of control variables: </font>",
textfield(-name=>'u_nodes', -size=>2, -maxlength=>2, -default=>2);
print "<br>";
print "<br>";
## p value
print "</td></tr><tr><td BGCOLOR=\"#DCDCDC\" HEIGHT=\"1\"> </td></tr>
<tr valign=\"top\"><td nowrap><font size=\"2\"><br>Enter number of states per node: </font>";
print textfield(-name=>'p_value',-size=>2,-maxlength=>2, default=>2),
"<font size=2> (Must be a prime number)</font>";
print "<br>";
print "<br>";
## Explainations
print "</td></tr><tr><td BGCOLOR=\"#DCDCDC\" HEIGHT=\"1\"></td></tr><tr valign=\"top\"><td wrap><font size=\"2\">";
print "<br>";
print "A controlled polynomial dynamical system has a number of state variables x1, ... xn <br>";
print "and a number of control variables u1, ..., um. The idea is, that the
system evolves according <br>";
print "to certain rules, this corresponds to a regular PDS, but the control variables can be <br> externally controlled. Therefore ";
print "the state space of a controlled PDS looks slightly different <br>than that of a regular PDS: ";
print "There are p^n states where each state has out-degree at <br>most 2^u. Every edge in the graph is ";
print "labeled with the control that has been applied at this transition. <br>";
print "<br>";
print "</td></tr>";
# Input Functions Block
print "<tr><td nowrap bgColor=\"#666666\"><strong><font
color=\"#ffffff\">Input Functions</font></strong></td></tr>
<tr><td BGCOLOR=\"#DCDCDC\" HEIGHT=\"1\"></td></tr>";
print "<tr><td BGCOLOR=\"#DCDCDC\" HEIGHT=\"1\"></td></tr><tr valign=\"top\"><td nowrap><div align=\"center\">";
print textarea(-name=>'edit_functions',
-default=>
'f1 = x1 + x2 + u1
f2 = u2 + x1 * x3
f3 = x2 * u1
f4 = x1 + u1 + u2
',
-rows=>8,
-columns=>50);
print "</div></td></tr><tr><td BGCOLOR=\"#DCDCDC\" HEIGHT=\"1\"></td></tr>";
print "</table>";
print "</td></tr>";
print "</table>";
# control Options
# controlGroup
# - nothing
# - given
# - heuristic
# - best
print "
<td width=50%>
<table width=\"100%\" cellSpacing=\"0\" cellPadding=\"1\" align=\"center\" bgColor=\"#ababab\" border=\"0\">
<tr>
<td>
<table cellSpacing=\"0\" cellPadding=\"1\" width=\"100%\" bgColor=\"#ffffcc\" border=\"0\">
<tr><td nowrap bgColor=\"#666666\"><strong><font color=\"#ffffff\">
Controller</font></strong></td>
</tr>
<tr>
<td BGCOLOR=\"#DCDCDC\" HEIGHT=\"1\">
</td>
</tr>
<tr>
<td BGCOLOR=\"#DCDCDC\" HEIGHT=\"1\">
</td>
</tr>
<tr valign=\"top\">
<td nowrap><div align=\"center\">
<tr valign=\"top\"><td nowrap><font size=\"2\">
<table width=100% border=0>
<tr>
<td colspan=3>
<label>
<input type=\"radio\" name=\"controlGroup\" value=\"nothing\" checked=\"checked\" >
Do not search for a control sequence, just compute the complete phase space.
</label>
</td>
</tr>
<tr><td colspan=3 BGCOLOR=\"#DCDCDC\" HEIGHT=\"1\" width=100%></td></tr>
<tr>
<td colspan=3>
Enter initial state, separated by spaces: ", textfield( -name=>'initialState', -size=>20, -default=>'1 0 1 1'), "
</td>
</tr>
<tr>
<td width5%>
</td>
<td colspan=2>
<label>
<input type=\"radio\" name=\"controlGroup\" value=\"given\">
Apply a given control sequence repeatedly
</label>
</td>
</tr>
<tr>
<td width=5%>
</td>
<td colspan=2>
Enter a control sequence, the sequence will be repeatedly applied until a repeated node is found.<br>",
textarea(-name=>'given_control',
-default=>
'0 0
1 0
',
-rows=>6,
-columns=>15), "
</td>
</tr>
<tr>
<td>
</td>
<td colspan=2 BGCOLOR=\"#DCDCDC\" HEIGHT=\"1\" width=100%>
</td>
</tr>
<tr>
<td>
</td>
<td colspan=2>
Enter final state, separated by spaces:   ", textfield( -name=>'finalState', -size=>20, -default=>'0 0 1 1'),"
</td>
</tr>
<tr>
<td width=5%>
</td>
<td width=5%>
</td>
<td>
<label>
<input type=\"radio\" name=\"controlGroup\" value=\"heuristic\">
Use heuristic controller to find control sequence
</label>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
A heuristic algorithm will try to find the cheapest trajectory from the initial to the <br>
final state. Cheap means with the cheapest possible control. As this is an experimental <br>
version, we consider uniform cost for every control variable that is set, i.e., not 0.<br>
If a sequence of control inputs is found, that drives the system from the initial state <br>
to the final state, this trajectory is highlighted in the state space graph in green. If <br>
no sequence can be found, no trajectory will be highlighted in the phase space. <br>
<br>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
<label>
<input type=\"radio\" name=\"controlGroup\" value=\"best\">
Find the true optimal controller
</label>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
Find a truly optimal controller from the initial to the final state. <br>
This is done by enumeration. <br><br>
</td>
</tr>
<tr>
<td colspan=3 nowrap bgColor=\"#666666\"><strong><font color=\"#ffffff\" size=3>
Cost Function</font></strong>
</td>
</tr>
<tr valign=\"top\"><td></td><td></td>
<td wrap><font size=2>
The algorithms find a control sequence that is best with respect to a cost<br>
function. Please enter your cost function here:
",
textfield(-name=>'costFunction', -size=>20, -default=>'u1+u2'),
"<br>The control variables are referred to as u = {u1, u2, ..., um},<br>
where m is the number of control variables. The total cost for a<br>
trajectory is the sum of the cost of the controls applied. For example, if<br>
you applied the controls {0,1}, {2,1}, {0,0}, {1,1}, then the cost is<br>
c({0,1}) + c({2,1}) + c({0,0}) + c({1,1}). <b>Please note, for now, only the<br>
default cost is possible, changing this field is not working yet</b>.</font>
</td>
</tr> ";
print "</table></div></td></tr><tr><td colspan=3 BGCOLOR=\"#DCDCDC\"
HEIGHT=\"1\"></td></tr>";
print "</table></td></tr></table></td></tr>";
print "<tr>";
print "<br>";
print "<td align=\"center\" colspan=\"2\">",
submit('generateButton', 'Generate'),
"<br><font style=bold color=\"#009977\"><br><i>Results will be displayed below.</i></font></td></tr>";
print end_form;
##Google Analytics, Franzi's Account
#print <<ENDHTML;
#<script type="text/javascript">
#var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
#document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
#</script>
#<script type="text/javascript">
#try {
#var pageTracker = _gat._getTracker("UA-11219893-5");
#pageTracker._trackPageview();
#} catch(err) {}</script>
#ENDHTML
open(ACCESS, ">>../../htdocs/no-ssl/accessControl") or die("Failed to open file for writing");
flock(ACCESS, LOCK_EX) or die ("Could not get exclusive lock $!");
print ACCESS ($ENV{REMOTE_ADDR});
system("date >>../../htdocs/no-ssl/accessControl");
flock(ACCESS, LOCK_UN) or die ("Could not unlock file $!");
close(ACCESS);
$p_value = param('p_value');
$n_nodes = param('n_nodes');
$u_nodes = param('u_nodes');
$functions = param('edit_functions');
$controlType = param( 'controlGroup' );
$initialState = param( 'initialState' );
$finalState = param( 'finalState');
$givenControl = param( 'given_control');
$generateButton = param('generateButton');
print "<tr>";
print "<br>";
print "<td align=\"left\" colspan=\"2\">";
$DEBUG = 0;
print "access was ok <br>" if ($DEBUG);
print "client ip is $clientip <br>" if ($DEBUG);
print "$n_nodes<br>" if ($DEBUG);
print "$u_nodes<br>" if ($DEBUG);
print "$p_value<br>" if ($DEBUG);
print "$functions<br>" if ($DEBUG);
print "$initialState<br>" if ($DEBUG);
print "$finalState<br>" if ($DEBUG);
print "generate button: $generateButton<br>" if $DEBUG;
print "controlType is $controlType, <br>
initialState $initialState, <br>
finalState $finalState<br>" if $DEBUG;
if (param) {
if ($initialState ne null) {
$initialState = &cleanUpState( $initialState );
}
if ($finalState ne null) {
$finalState = &cleanUpState( $finalState );
}
if ($controlType eq "nothing") {
print "No control<br>" if $DEBUG;
$ret = system("ruby control_runner.rb $n_nodes $u_nodes $p_value \"$functions\" $clientip.out.gif $controlType ");
} elsif ($controlType eq "given") {
print "given control <br>" if $DEBUG;
print "<font color=\"#226677\"><b>Finding trajectory from $initialState
for given control $givenControl:</b></font><br>" if $DEBUG;
if ($givenControl eq null) {
print "<br><font color=red>Sorry. You have to specify a control sequence if you want chose given control.</font><br>";
die("Program quitting.");
}
print ("ruby control_runner.rb $n_nodes $u_nodes $p_value \"$functions\" $clientip.out.gif $controlType $initialState \"$givenControl\" <br>") if $DEBUG;
$ret = system("ruby control_runner.rb $n_nodes $u_nodes $p_value \"$functions\" $clientip.out.gif $controlType $initialState \"$givenControl\"");
} elsif ($controlType eq "heuristic" or $controlType eq "best") {
print "heuristic or best <br>" if $DEBUG;
print "<font color=\"#226677\"><b>Finding $controlType controller from $initialState to $finalState:</b></font><br>";
print ("ruby control_runner.rb $n_nodes $u_nodes $p_value \"$functions\" $clientip.out.gif $controlType $initialState $finalState") if $DEBUG;
$ret = system("ruby control_runner.rb $n_nodes $u_nodes $p_value \"$functions\" $clientip.out.gif $controlType $initialState $finalState");
} else {
print "<br><font color=red>Sorry. Something went wrong.</font><br>";
die("Program quitting. Unknown error");
exit 1;
}
if ( $ret == 0 ) {
print "everything ok<br>" if ($DEBUG);
if (-e "$clientip.out.gif") {
print "<A href=\"$clientip.out.gif\" target=\"_blank\"> <font color=\"#226677\"><i>Click to view the state space graph of your controlled polynomial dynamical system.</i></font></A><br>";
}
} else {
print "<br><font color=red>Sorry. Something went wrong.</font><br>";
die("Program quitting. Unknown error");
}
}
print "</td></tr>";
print "</table></div>";
print end_html;
sub cleanUpState() {
($s) = @_;
$s =~ s/^\s+|\s+$//g; #remove all leading and trailing white spaces
$s =~ s/(\d+)(\s+|,+)+/$1 /g; # remove extra spaces in between the numbers
$s =~ s/ /_/g;
$s;
}