-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsource.tex
5806 lines (5042 loc) · 202 KB
/
source.tex
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\section{backend/MLServer/five-seconds}
\begin{lstlisting}\#!/bin/bash
ulimit -t 1
./planner --optimise domain.pddl 5713a9028794d11d17c53d56_temp.pddl
\end{lstlisting}
\newpage
\section{backend/MLServer/domain.pddl}
\begin{lstlisting}(define (domain museum)
(:requirements :typing :durative-actions :fluents)
(:types
exhibit person - object
)
(:predicates
(at ?p - person ?e - exhibit)
(path ?e1 ?e2 - exhibit)
(want-to-see ?e - exhibit)
(visited ?e - exhibit)
(open)
)
(:functions
(time-to-walk ?e1 ?e2 - exhibit)
(time-to-see ?e - exhibit)
(excitement ?e - exhibit)
(seen)
)
(:durative-action walk
:parameters
(?p - person
?e1 ?e2 - exhibit)
:duration (= ?duration (time-to-walk ?e1 ?e2))
:condition
(and (over all (path ?e1 ?e2)) (at start (at ?p ?e1)) (over all (open)))
:effect
(and (at start (not (at ?p ?e1))) (at end (at ?p ?e2))))
(:durative-action view
:parameters
(?p - person
?e - exhibit)
:duration (= ?duration (time-to-see ?e))
:condition
(and (over all (at ?p ?e)) (at start (want-to-see ?e)) (over all (open)))
:effect
(and (at start (not (want-to-see ?e))) (at start (increase (seen) (excitement ?e))))
)
)
\end{lstlisting}
\newpage
\section{backend/MLServer/src/tools/Rectangle.java}
\begin{lstlisting}package tools;
import java.io.Serializable;
public class Rectangle implements Serializable{
private Point coordinates;
private double width, length;
public Rectangle(Point coordinates, double width, double length){
this.coordinates = coordinates;
this.width = width;
this.length = length;
}
public Point getCoordinates() {
return coordinates;
}
public double getWidth() {
return width;
}
public double getLength() {
return length;
}
public void setCoordinates(Point coordinates) {
this.coordinates = coordinates;
}
public void setWidth(double width) {
this.width = width;
}
public void setLength(double length) {
this.length = length;
}
public Point getCenter(){
return new Point((width+coordinates.getX())/2,(length +coordinates.getY())/2);
}
}
\end{lstlisting}
\newpage
\section{backend/MLServer/src/tools/Point.java}
\begin{lstlisting}package tools;
import java.io.Serializable;
public class Point implements Serializable {
private double x, y;
public Point(double x, double y)
{
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
\end{lstlisting}
\newpage
\section{backend/MLServer/src/tools/RectangleDB.java}
\begin{lstlisting}package tools;
import java.io.Serializable;
public class RectangleDB implements Serializable{
private Point lt,rt,lb,rb;
public RectangleDB(Point lt, Point rt, Point lb, Point rb) {
this.lt = lt;
this.rt = rt;
this.lb = lb;
this.rb = rb;
}
public Point getLt() {
return lt;
}
public void setLt(Point lt) {
this.lt = lt;
}
public Point getRt() {
return rt;
}
public void setRt(Point rt) {
this.rt = rt;
}
public Point getLb() {
return lb;
}
public void setLb(Point lb) {
this.lb = lb;
}
public Point getRb() {
return rb;
}
public void setRb(Point rb) {
this.rb = rb;
}
public boolean isNeighbour (RectangleDB r){
if(this.getRt() == r.getLt() && this.getRb() == r.getLb() )
return true;
if(this.getLt() == r.getRt() && this.getLb() == r.getRb())
return true;
if(this.getLt() == r.getLb() && this.getRt() == r.getRb())
return true;
if(this.getLb() == r.getLt() && this.getRb() == this.getRt())
return true;
return true;
}
}
\end{lstlisting}
\newpage
\section{backend/MLServer/src/models/Room.java}
\begin{lstlisting}package models.indoormapping;
import tools.Point;
import tools.RectangleDB;
import tools.Rectangle;
import java.io.Serializable;
public class Room implements Serializable{
private String roomName;
private String id;
private String building_id;
private Rectangle roomRectangle;
private RectangleDB rectangleDB;
private String roomDescription;
private double width, length;
private double est_time;
private int excitement;
public Room(String id, String building_id, String roomName, RectangleDB rectangleDB,
double width, double length, double est_time, int excitement){
this.roomName = roomName;
this.id = id;
this.width = width;
this.length = length;
this.rectangleDB = rectangleDB;
this.building_id = building_id;
this.roomRectangle = new Rectangle(new Point(0,0),0,0); //for testing!!!
this.est_time = est_time;
this.excitement = excitement;
}
public String getBuilding_id() {
return building_id;
}
public Room(String building_id, String roomName, RectangleDB rectangleDB, double width, double length){
this.building_id = building_id;
this.roomName = roomName;
this.width = width;
this.length = length;
this.roomDescription = "";
this.rectangleDB = rectangleDB;
this.roomRectangle = new Rectangle(new Point(0,0),0,0); //for testing!!!
}
public Room(Rectangle r){
this.roomRectangle = r;
roomName = "";
roomDescription = "";
}
public String getRoomName() {
return roomName;
}
public void setRoomName(String roomName) {
this.roomName = roomName;
}
public Rectangle getRoomRectangle() {
return roomRectangle;
}
public void setRoomRectangle(Rectangle roomRectangle) {
this.roomRectangle = roomRectangle;
}
public String getRoomDescription() {
return roomDescription;
}
public void setRoomDescription(String roomDescription) {
this.roomDescription = roomDescription;
}
public boolean isNeighbour(Room room){
if(this == room)
return false;
if(this.getRoomRectangle().getCoordinates().getX() + this.getRoomRectangle().getWidth()
== room.getRoomRectangle().getCoordinates().getX()
|| this.getRoomRectangle().getCoordinates().getY() + this.getRoomRectangle().getLength()
== room.getRoomRectangle().getCoordinates().getY()
|| room.getRoomRectangle().getCoordinates().getX() + room.getRoomRectangle().getWidth()
== this.getRoomRectangle().getCoordinates().getX()
|| room.getRoomRectangle().getCoordinates().getY() + room.getRoomRectangle().getLength()
== this.getRoomRectangle().getCoordinates().getY())
return true;
return false;
}
public String getId() {
return id;
}
public RectangleDB getRectangleDB() {
return rectangleDB;
}
public double getWidth() {
return width;
}
public double getLength() {
return length;
}
public void setEst_time(double est_time) {
this.est_time = est_time;
}
public int getExcitement(){return excitement; }
public double getEst_time() {
return est_time;
}
}
\end{lstlisting}
\newpage
\section{backend/MLServer/src/models/Floor.java}
\begin{lstlisting}package models.indoormapping;
import java.io.Serializable;
import java.util.ArrayList;
public class Floor implements Serializable{
Room[] rooms;
public Floor(Room[] rooms)
{
this.rooms = rooms;
}
public Room[] getRooms() {
return rooms;
}
public Room[] getNeighbours(Room room) {
ArrayList<Room> neighbours = new ArrayList<>();
for(Room r:rooms){
if(r.isNeighbour(room))
neighbours.add(r);
}
return neighbours.toArray(new Room[neighbours.size()]);
}
public void setRooms(Room[] rooms) {
this.rooms = rooms;
}
}
\end{lstlisting}
\newpage
\section{backend/MLServer/src/models/Building.java}
\begin{lstlisting}package models.indoormapping;
import tools.RectangleDB;
import java.io.Serializable;
public class Building implements Serializable{
private RectangleDB rectangle;
private String name;
private double width;
private double length;
private String id;
private Room[] rooms;
public Building(String id, RectangleDB rectangle, String name, double width, double length, Room[] rooms) {
this.rectangle = rectangle;
this.id = id;
this.name = name;
this.width = width;
this.length = length;
this.rooms = rooms;
}
public Building(RectangleDB rectangle, String name, double width, double length) {
this.rectangle = rectangle;
this.id = "";
this.name = name;
this.width = width;
this.length = length;
}
public void setRectangle(RectangleDB rectangle) {
this.rectangle = rectangle;
}
public void setName(String name) {
this.name = name;
}
public void setWidth(double width) {
this.width = width;
}
public void setLength(double length) {
this.length = length;
}
public RectangleDB getRectangle() {
return rectangle;
}
public String getName() {
return name;
}
public double getWidth() {
return width;
}
public double getLength() {
return length;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Room[] getRooms() {
return rooms;
}
public void setRooms(Room[] rooms) {
this.rooms = rooms;
}
}
\end{lstlisting}
\newpage
\section{backend/MLServer/src/com/Planner.java}
\begin{lstlisting}package com.company;
import models.indoormapping.Room;
import tools.Point;
import org.json.JSONArray;
import org.json.JSONObject;
import tools.RectangleDB;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
public class Planner {
private static final String[] pddlIntro = {
"(define (problem simplemuseum)",
"(:domain museum)",
"(:objects",
" visitor - person"
};
public static String route(JSONArray jsonArray, int deadline, String building_id){
ArrayList<Room> rooms = getRoomsFromJSON(jsonArray);
makePDDL(rooms,deadline);
building_id = building_id.replaceAll("[\\s&;]+","");
ArrayList<String> toExecute = new ArrayList<>();
toExecute.add("#!/bin/bash\n" +
"ulimit -t 1\n");
toExecute.add( "./planner " + "--optimise " + "domain.pddl "+building_id+"_temp.pddl");
Path execPath = Paths.get("five-seconds");
try {
Files.write(execPath,toExecute, Charset.forName("UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
String command = "./five-seconds";
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
ArrayList<String> output = new ArrayList<>();
String s;
try {
while ((s = reader.readLine()) != null) {
System.out.println("Script output: " + s);
output.add(s);
}
} catch (IOException e) {
e.printStackTrace();
}
return makeFinalResult(parseOutput(output),rooms);
}
public static ArrayList<Room> getRoomsFromJSON(JSONArray jsonArray){
ArrayList<Room> toReturn = new ArrayList<>();
for(Object o:jsonArray){
JSONObject room = (JSONObject) o;
Point lt = new Point(room.getJSONObject("rectangle").getJSONObject("lt").getDouble("x"),
room.getJSONObject("rectangle").getJSONObject("lt").getDouble("y"));
Point rt = new Point(room.getJSONObject("rectangle").getJSONObject("rt").getDouble("x"),
room.getJSONObject("rectangle").getJSONObject("rt").getDouble("y"));
Point lb = new Point(room.getJSONObject("rectangle").getJSONObject("lb").getDouble("x"),
room.getJSONObject("rectangle").getJSONObject("lb").getDouble("y"));
Point rb = new Point(room.getJSONObject("rectangle").getJSONObject("rb").getDouble("x"),
room.getJSONObject("rectangle").getJSONObject("rb").getDouble("y"));
RectangleDB r = new RectangleDB(lt,rt,lb,rb);
Room toAdd = new Room(room.getString("_id"),room.getString("building_id"),room.getString("name"),r,
room.getDouble("width"),room.getDouble("length"),
room.getDouble("est_time"),room.getInt("excitement"));
toReturn.add(toAdd);
}
return toReturn;
}
private static void makePDDL(ArrayList<Room> rooms, int deadline){
Path tempPath = Paths.get(rooms.get(0).getBuilding_id()+"_temp.pddl");
ArrayList<String> toWrite = new ArrayList<>();
toWrite.addAll(Arrays.asList(pddlIntro));
toWrite.add(" ");
for(Room r:rooms){
toWrite.set(toWrite.size()-1,toWrite.get(toWrite.size()-1)+"e"+rooms.indexOf(r)+" ");
System.out.println("ROOM CREATED: "+"\nINDEX: "+rooms.indexOf(r)+" "+r.getRoomName());
}
toWrite.set(toWrite.size()-1,toWrite.get(toWrite.size()-1)+"- exhibit");
toWrite.add(")");
toWrite.add("(:init");
for(Room r:rooms){
toWrite.add("(want-to-see "+"e"+rooms.indexOf(r)+")");
}
toWrite.addAll(getBuildingLayout(rooms));
for(Room r:rooms){
toWrite.add("(= (time-to-see "+"e"+rooms.indexOf(r)+") "+(int) r.getEst_time()+")");
}
toWrite.add("(at visitor "+"e0"+")");
toWrite.add("(= (seen) 0)");
for(Room r:rooms){
toWrite.add("(= (excitement "+"e"+rooms.indexOf(r)+") "+ r.getExcitement()+(")"));
}
toWrite.add("(open)");
toWrite.add("(at "+deadline+" (not (open)))");
toWrite.add(")");
toWrite.add("(:goal (and");
for(Room r:rooms){
toWrite.add(";(visited "+"e"+rooms.indexOf(r)+")");
}
toWrite.add(")");
toWrite.add(")");
toWrite.add("(:metric maximize (seen))");
toWrite.add(")");
try {
Files.write(tempPath,toWrite, Charset.forName("UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
private static ArrayList<String> getBuildingLayout(ArrayList<Room> rooms){
ArrayList<String> toReturn = new ArrayList<>();
for(int i = 0; i<rooms.size()-1; ++i){
for(int j=i+1; j<rooms.size();++j){
if(rooms.get(i).getRectangleDB().isNeighbour(rooms.get(j).getRectangleDB())){
toReturn.add("(path " + "e"+i + " "+"e"+ j + ") (path " +"e"+j+" "+"e"+i+")");
toReturn.add("(= (time-to-walk "+"e"+i+" "+"e"+j+") 0)");
toReturn.add("(= (time-to-walk "+"e"+j+" "+"e"+i+") 0)");
}
}
}
return toReturn;
}
private static ArrayList<String> parseOutput(ArrayList<String> output){
ArrayList<String> toReturn = new ArrayList<>();
ArrayList<String> currentResult = new ArrayList<>();
for(String s:output){
if(s.equals(";;;; Solution Found")) {
toReturn = currentResult;
currentResult.clear();
}
if(!((s.startsWith(";")|| s.isEmpty()))) {
if (s.contains("(") && s.contains(")")) {
currentResult.add( s.substring(s.indexOf("(")+1, s.indexOf(")")));
}
}
if(s.startsWith("..")){
toReturn = currentResult;
}
}
return toReturn;
}
private static String makeFinalResult(ArrayList<String> output, ArrayList<Room> rooms){
String toReturn = "";
for(String s:output){
String[] tempSplit = s.split(" ");
if(tempSplit[0].equals("view")){
int roomIndex =Integer.parseInt(tempSplit[2].substring(tempSplit[2].indexOf("e")+1));
toReturn += "view:"+rooms.get(roomIndex).getId()+";";
} else if(tempSplit[0].equals("walk")){
int roomIndexOr = Integer.parseInt(tempSplit[2].substring(tempSplit[2].indexOf("e")+1));
int roomIndexDest = Integer.parseInt(tempSplit[3].substring(tempSplit[3].indexOf("e")+1));
toReturn += "walk:"+rooms.get(roomIndexOr).getId()+","+rooms.get(roomIndexDest).getId()+";";
}
}
return toReturn.substring(0,toReturn.length()-1);
}
}
\end{lstlisting}
\newpage
\section{backend/MLServer/src/com/Server.java}
\begin{lstlisting}package com.company;
import org.apache.commons.lang3.tuple.Pair;
import org.json.JSONObject;
import weka.classifiers.bayes.BayesNet;
import weka.classifiers.bayes.NaiveBayes;
import java.net.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.io.*;
public class Server
{
String currentBID;
ArrayList<Pair<String,BayesNet>> classifiersBN;
ArrayList<Pair<String,NaiveBayes>> classifiersNB;
public Server()
{
classifiersBN = new ArrayList<>();
classifiersNB = new ArrayList<>();
try {
ServerSocket sSocket = new ServerSocket(5000);
System.out.println("Server started!");
currentBID = "";
while(true) {
Socket socket = sSocket.accept();
ClientThread cT = new ClientThread(socket);
new Thread(cT).start();
}
} catch(IOException exception) {
System.out.println("Error: " + exception);
}
}
class ClientThread implements Runnable
{
Socket socket;
public ClientThread(Socket socket)
{
this.socket = socket;
}
public void run()
{
try {
PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String rec = br.readLine();
while (rec == null) {
rec = br.readLine();
}
System.out.println(rec);
JSONObject recJSON = new JSONObject(rec);
if(recJSON.getString("command").equals("learn")){
BayesNet bn = Learner.learnFromJSON_BN(recJSON.getString("building_id"),
recJSON.getJSONArray("learning_set"));
NaiveBayes nb = Learner.learnFromJSON_NB(recJSON.getString("building_id"),
recJSON.getJSONArray("learning_set"));
int i = buildingClassifierBNInitialised(recJSON.getString("building_id"));
int j = buildingClassifierNBInitialised(recJSON.getString("building_id")); //not really needed.
if(i>-1){
classifiersBN.set(i,Pair.of(recJSON.getString("building_id"),bn));
} else {
classifiersBN.add(Pair.of(recJSON.getString("building_id"),bn));
}
if(i>-1){
classifiersNB.set(i,Pair.of(recJSON.getString("building_id"),nb));
} else {
classifiersNB.add(Pair.of(recJSON.getString("building_id"),nb));
}
pw.write("Done!");
} else if(recJSON.getString("command").equals("classify")){
int i = buildingClassifierBNInitialised(recJSON.getString("building_id"));
int j = buildingClassifierNBInitialised(recJSON.getString("building_id")); //same
if(i>-1){
String res ="BN:"+Learner.classify_BN(recJSON.getString("building_id"),
recJSON.getJSONArray("learning_set"),classifiersBN.get(i).getRight());
res += ",NB:"+Learner.classify_NB(recJSON.getString("building_id"),
recJSON.getJSONArray("learning_set"),classifiersNB.get(j).getRight());
pw.write(res);
System.out.println(res);
} else if(Files.exists(Paths.get(recJSON.getString("building_id")+".arff"))){
BayesNet bn = Learner.getClassifierBN(recJSON.getString("building_id"));
NaiveBayes nb = Learner.getClassifierNB(recJSON.getString("building_id"));
classifiersBN.add(Pair.of(recJSON.getString("building_id"),bn));
classifiersNB.add(Pair.of(recJSON.getString("building_id"),nb));
i = buildingClassifierBNInitialised(recJSON.getString("building_id"));
j = buildingClassifierNBInitialised(recJSON.getString("building_id")); //same
String res ="BN:"+Learner.classify_BN(recJSON.getString("building_id"),
recJSON.getJSONArray("learning_set"),classifiersBN.get(i).getRight());
res += ",NB:"+Learner.classify_NB(recJSON.getString("building_id"),
recJSON.getJSONArray("learning_set"),classifiersNB.get(j).getRight());
pw.write(res);
} else {
pw.write("Weird classify request!");
}
} else if(recJSON.getString("command").equals("route")){
pw.write(Planner.route(recJSON.getJSONArray("request_set"),recJSON.getInt("deadline"),
recJSON.getString("building_id")));
}
pw.flush();
} catch(IOException exception) {
System.out.println("Error: " + exception);
}
}
}
public int buildingClassifierBNInitialised(String building_id){
for(Pair<String,BayesNet> p:classifiersBN){
if(p.getLeft().equals(building_id))
return classifiersBN.indexOf(p);
}
return -1;
}
public int buildingClassifierNBInitialised(String building_id){
for(Pair<String,NaiveBayes> p:classifiersNB){
if(p.getLeft().equals(building_id))
return classifiersNB.indexOf(p);
}
return -1;
}
}
\end{lstlisting}
\newpage
\section{backend/MLServer/src/com/Main.java}
\begin{lstlisting}package com.company;
public class Main {
public static void main(String[] args) {
new Server();
}
}
\end{lstlisting}
\newpage
\section{backend/MLServer/src/com/Learner.java}
\begin{lstlisting}package com.company;
import org.json.JSONArray;
import org.json.JSONObject;
import weka.classifiers.bayes.BayesNet;
import weka.classifiers.bayes.NaiveBayes;
import weka.core.Instances;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;
public class Learner {
public static NaiveBayes learnFromJSON_NB(String building_id, JSONArray JSONData){
makeLearnerARFFfromJSON(building_id,JSONData);
NaiveBayes nb = new NaiveBayes();
try {
Instances instances = new Instances(new BufferedReader(new FileReader(building_id+".arff")));
instances.setClassIndex(instances.numAttributes()-1);
nb.buildClassifier(instances);
} catch (Exception e) {
e.printStackTrace();
}
return nb;
}
public static BayesNet learnFromJSON_BN(String building_id, JSONArray JSONData){
makeLearnerARFFfromJSON(building_id,JSONData);
BayesNet bn = new BayesNet();
try {
Instances instances = new Instances(new BufferedReader(new FileReader(building_id+".arff")));
instances.setClassIndex(instances.numAttributes()-1);
bn.buildClassifier(instances);
} catch (Exception e) {
e.printStackTrace();
}
return bn;
}
public static BayesNet getClassifierBN(String building_id){
BayesNet bn = new BayesNet();
try {
Instances instances = new Instances(new BufferedReader(new FileReader(building_id+".arff")));
instances.setClassIndex(instances.numAttributes()-1);
bn.buildClassifier(instances);
} catch (Exception e) {
e.printStackTrace();
}
return bn;
}
public static NaiveBayes getClassifierNB(String building_id){
NaiveBayes nb = new NaiveBayes();
try {
Instances instances = new Instances(new BufferedReader(new FileReader(building_id+".arff")));
instances.setClassIndex(instances.numAttributes()-1);
nb.buildClassifier(instances);
} catch (Exception e) {
e.printStackTrace();
}
return nb;
}
public static String classify_NB(String building_id, JSONArray JSONdata, NaiveBayes nb){
makeClassifierARFF(building_id,JSONdata);
Instances unlabeled;
Instances labeled = null;
double clsLabel = 0;
try {
unlabeled = new Instances(new BufferedReader(new FileReader(building_id+"_temp.arff")));
unlabeled.setClassIndex(unlabeled.numAttributes()-1);
labeled = new Instances(unlabeled);
clsLabel = nb.classifyInstance(unlabeled.firstInstance());
labeled.firstInstance().setClassValue(clsLabel);
Files.deleteIfExists(Paths.get(building_id+"_temp.arff"));
} catch (Exception e) {
e.printStackTrace();
}
if (labeled != null) {
return labeled.instance(0).classAttribute().value((int)clsLabel);
}
return "na";
}
public static String classify_BN(String building_id, JSONArray JSONdata, BayesNet bn){
makeClassifierARFF(building_id,JSONdata);
Instances unlabeled;
Instances labeled = null;
double clsLabel = 0;
try {
unlabeled = new Instances(new BufferedReader(new FileReader(building_id+"_temp.arff")));
unlabeled.setClassIndex(unlabeled.numAttributes()-1);
labeled = new Instances(unlabeled);
clsLabel = bn.classifyInstance(unlabeled.firstInstance());
labeled.firstInstance().setClassValue(clsLabel);
Files.deleteIfExists(Paths.get(building_id+"_temp.arff"));
} catch (Exception e) {
e.printStackTrace();
}
if (labeled != null) {
return labeled.instance(0).classAttribute().value((int)clsLabel);
}
return "na";
}
public static void makeLearnerARFFfromJSON(String building_id, JSONArray JSONData){
Path arffPath = Paths.get(building_id+".arff");
Path roomsPath = Paths.get(building_id+"_rooms.data");
Path rpPath = Paths.get(building_id+"_RPs.data");
ArrayList<String> arffData = new ArrayList<>();
ArrayList<String> rpids = getRPs(JSONData);
ArrayList<String> rooms = getRooms(JSONData);
arffData.add("@relation room"); arffData.add("");
for(String s:rpids){
arffData.add("@attribute "+s+" NUMERIC");
}
arffData.add("@attribute class {");
for(int i=0;i<rooms.size()-1;++i){
arffData.set(arffData.size()-1,arffData.get(arffData.size()-1)+rooms.get(i)+",");
}
arffData.set(arffData.size()-1,arffData.get(arffData.size()-1)+rooms.get(rooms.size()-1)+"}");
arffData.add("@data");
arffData.addAll(getOrderedReadings(JSONData,rpids));
try {
Files.write(arffPath,arffData, Charset.forName("UTF-8"));
Files.write(roomsPath,rooms, Charset.forName("UTF-8"));
Files.write(rpPath,rpids, Charset.forName("UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void makeClassifierARFF(String building_id,JSONArray JSONData){
Path tempPath = Paths.get(building_id+"_temp.arff");
Path roomsPath = Paths.get(building_id+"_rooms.data");
Path rpPath = Paths.get(building_id+"_RPs.data");
ArrayList<String> arffData = new ArrayList<>();
ArrayList<String> rpids = new ArrayList<>();
ArrayList<String> rooms = new ArrayList<>();
try {
BufferedReader br = Files.newBufferedReader(roomsPath,Charset.forName("UTF-8"));
String line;
while((line = br.readLine()) != null){
rooms.add(line);
}
br = Files.newBufferedReader(rpPath,Charset.forName("UTF-8"));
while((line = br.readLine()) != null){
rpids.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
arffData.add("@relation room"); arffData.add("");
for(String s:rpids){
arffData.add("@attribute "+s+" NUMERIC");
}
arffData.add("@attribute class {");
for(int i=0;i<rooms.size()-1;++i){
arffData.set(arffData.size()-1,arffData.get(arffData.size()-1)+rooms.get(i)+",");
}
arffData.set(arffData.size()-1,arffData.get(arffData.size()-1)+rooms.get(rooms.size()-1)+"}");
arffData.add("@data");
arffData.addAll(getOrderedReadings(JSONData,rpids));
try {
Files.write(tempPath,arffData, Charset.forName("UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static ArrayList<String> getRPs(JSONArray JSONdata){
ArrayList<String> rpids = new ArrayList<>();
for(Object reading: JSONdata){
for(Object pair: ((JSONObject)reading).getJSONArray("rpv_pair")){
rpids.add(((JSONObject) pair).getString("RPID"));
}
}
Set<String> noDuplicates = new LinkedHashSet<>(rpids);
rpids.clear();
rpids.addAll(noDuplicates);
return rpids;
}
public static ArrayList<String> getRooms(JSONArray JSONdata){
ArrayList<String> rooms = new ArrayList<>();
for(Object reading: JSONdata){
rooms.add(((JSONObject) reading).getString("room_id"));
}
Set<String> noDuplicates = new LinkedHashSet<>(rooms);
rooms.clear();
rooms.addAll(noDuplicates);
return rooms;
}
public static ArrayList<String> getOrderedReadings(JSONArray jsonData, ArrayList<String> rpids){
ArrayList<String> toReturn= new ArrayList<>();
for(Object o:jsonData){
String curLine = "";
for(String rp:rpids){
curLine += Double.toString(getValueByRPID(((JSONObject) o),rp))+",";