Skip to content

Commit e5ceb97

Browse files
committed
Remove the Distance.VECTOR_SIZE variable, it should be dynamic based on floor map
1 parent 54e8ceb commit e5ceb97

10 files changed

Lines changed: 93 additions & 132 deletions

File tree

REUServer/src/main/java/edu/fiu/adwise/fingerprint_localization/LocalizationThread.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import edu.fiu.adwise.fingerprint_localization.structs.SendLocalizationData;
2525
import edu.fiu.adwise.fingerprint_localization.structs.SendTrainingData;
2626
import edu.fiu.adwise.fingerprint_localization.database.LocalizationLUT;
27-
import edu.fiu.adwise.fingerprint_localization.distance_computation.Distance;
2827

2928
import edu.fiu.adwise.homomorphic_encryption.dgk.DGKPublicKey;
3029
import edu.fiu.adwise.homomorphic_encryption.elgamal.ElGamalPublicKey;
@@ -119,7 +118,6 @@ public void run() {
119118
} else if (command.equalsIgnoreCase("RESET")) {
120119
logger.info("Command acquired: RESET");
121120
toClient.writeBoolean(LocalizationLUT.reset());
122-
server.preprocessed = false;
123121
return;
124122
} else if (command.equalsIgnoreCase("Acquire all current training points")) {
125123
logger.info("Command acquired: Obtain all Fingerprints!");
@@ -345,8 +343,8 @@ else if (command.equalsIgnoreCase("Process LUT")) {
345343
*
346344
* @return true if lookup table was successfully created and updated, false otherwise
347345
*/
348-
public static boolean process() {
349-
if(!server.preprocessed) {
346+
public static boolean process() throws SQLException, ClassNotFoundException {
347+
if(!LocalizationLUT.isProcessed()) {
350348
if(FingerprintDbUtils.createTables()) {
351349
logger.info("Created new Lookup Table!");
352350
}
@@ -355,12 +353,9 @@ public static boolean process() {
355353
return false;
356354
}
357355
logger.info("Computing Lookup Table Data...");
358-
Distance.VECTOR_SIZE = LocalizationLUT.getVectorSize(Distance.FSF);
359-
logger.info("NEW VECTOR SIZE: {}", Distance.VECTOR_SIZE);
360356

361357
if(LocalizationLUT.UpdatePlainLUT()) {
362358
logger.info("Successfully created Lookup table!");
363-
server.preprocessed = true;
364359
return true;
365360
} else {
366361
logger.info("Failed to create Lookup table!");

REUServer/src/main/java/edu/fiu/adwise/fingerprint_localization/database/FingerprintDbUtils.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* Licensed under the MIT License. See LICENSE file in the project root for details.
66
*/
77

8-
import edu.fiu.adwise.fingerprint_localization.distance_computation.Distance;
98
import org.apache.logging.log4j.LogManager;
109
import org.apache.logging.log4j.Logger;
1110

@@ -36,7 +35,7 @@ public class FingerprintDbUtils {
3635
/** MySQL username, loaded from environment variable MYSQL_USER. */
3736
public static String username = System.getenv("MYSQL_USER") != null ? System.getenv("MYSQL_USER") : "";
3837

39-
/** MySQL password, loaded from environment variable MYSQL_PASSWORD. */
38+
/** MySQL password, loaded from the environment variable MYSQL_PASSWORD. */
4039
public static String password = System.getenv("MYSQL_PASSWORD") != null ? System.getenv("MYSQL_PASSWORD") : "";
4140

4241
/** Database name, loaded from environment variable DATABASE or defaults to "fiu". */
@@ -54,6 +53,9 @@ public class FingerprintDbUtils {
5453
/** Logger instance for FingerprintDbUtils. */
5554
private static final Logger logger = LogManager.getLogger(FingerprintDbUtils.class);
5655

56+
/** Feature Selection Factor: fraction of APs to filter (default 0.9). */
57+
public static double FSF = 0.9;
58+
5759
/**
5860
* Retrieves the list of MAC addresses (as column names) used for lookup tables for a given map.
5961
* <p>
@@ -71,7 +73,6 @@ public static String[] getColumnMAC(String map) {
7173
try (Connection conn = DriverManager.getConnection(URL, username, password);
7274
Statement st = conn.createStatement()) {
7375

74-
ResultSet rs;
7576
if (isProcessed()) {
7677
try (ResultSet columns = st.executeQuery("SHOW COLUMNS FROM " + DB + "." + map + " ;")) {
7778
int counter = 1;
@@ -86,14 +87,12 @@ public static String[] getColumnMAC(String map) {
8687
}
8788
common_aps.replaceAll(FingerprintDbUtils::getColumnName);
8889
} else {
89-
if (Distance.VECTOR_SIZE == -1) {
90-
Distance.VECTOR_SIZE = getVectorSize(Distance.FSF);
91-
}
90+
int vector_size = getVectorSize(FSF, map);
9291
String sql = "SELECT MACADDRESS, Count(MACADDRESS) as count "
9392
+ "from " + DB + "." + TRAININGDATA + " "
9493
+ "Where Map= ? "
9594
+ "group by MACADDRESS "
96-
+ "ORDER BY count DESC LIMIT " + Distance.VECTOR_SIZE + ";";
95+
+ "ORDER BY count DESC LIMIT " + vector_size + ";";
9796
try (PreparedStatement state = conn.prepareStatement(sql)) {
9897
state.setString(1, map);
9998
try (ResultSet result = state.executeQuery()) {
@@ -123,7 +122,7 @@ public static String[] getColumnMAC(String map) {
123122
* @param percentile the percentile (between 0 and 1) used to filter access points
124123
* @return the number of access points to keep after filtering, or -1 if the percentile is out of range
125124
*/
126-
public static int getVectorSize(double percentile) {
125+
public static int getVectorSize(double percentile, String map) throws ClassNotFoundException {
127126
if(percentile < 0 || percentile > 1) {
128127
logger.fatal("Invalid percentile value: {}. Must be between 0 and 1.", percentile);
129128
return -1;
@@ -133,21 +132,18 @@ public static int getVectorSize(double percentile) {
133132
}
134133

135134
List<Integer> AP_count = new ArrayList<>();
136-
try {
137-
Class.forName(myDriver);
138-
try (Connection conn = DriverManager.getConnection(URL, username, password);
139-
Statement stmt = conn.createStatement();
140-
ResultSet vec = stmt.executeQuery(
141-
"SELECT Count(MACADDRESS) as count from\n" +
142-
DB + "." + TRAININGDATA + "\n" +
143-
"group by MACADDRESS\n" +
144-
"ORDER BY count ASC\n")) {
145-
135+
Class.forName(myDriver);
136+
try (Connection conn = DriverManager.getConnection(URL, username, password);
137+
PreparedStatement stmt = conn.prepareStatement(
138+
"SELECT Count(MACADDRESS) as count FROM " + DB + "." + TRAININGDATA +
139+
" WHERE Map = ? GROUP BY MACADDRESS ORDER BY count ASC")) {
140+
stmt.setString(1, map);
141+
try (ResultSet vec = stmt.executeQuery()) {
146142
while (vec.next()) {
147143
AP_count.add(vec.getInt("count"));
148144
}
149145
}
150-
} catch (SQLException | ClassNotFoundException cnf) {
146+
} catch (SQLException cnf) {
151147
logger.warn("Failed to the new vector size: {}", cnf.getMessage());
152148
}
153149
// same as get IDX of Percentile, Note the int is already sorted!
@@ -173,14 +169,14 @@ public static boolean createTables() {
173169
Statement stmt = conn.createStatement()) {
174170
for (String map : maps) {
175171
String[] ColumnNames = getColumnMAC(map);
172+
assert ColumnNames != null : "Column names cannot be null for map: " + map;
176173
StringBuilder sql = new StringBuilder(
177174
"CREATE TABLE " + DB + "." + map + " ("
178175
+ "ID INTEGER not NULL, "
179176
+ "Xcoordinate DOUBLE not NULL, "
180177
+ "Ycoordinate DOUBLE not NULL, ");
181-
for (int i = 0; i < Distance.VECTOR_SIZE; i++) {
182-
assert ColumnNames != null;
183-
sql.append(makeColumnName(ColumnNames[i])).append(" INTEGER not NULL,");
178+
for (String columnName : ColumnNames) {
179+
sql.append(makeColumnName(columnName)).append(" INTEGER not NULL,");
184180
}
185181
sql.append(" PRIMARY KEY (ID));");
186182
stmt.executeUpdate(sql.toString());

REUServer/src/main/java/edu/fiu/adwise/fingerprint_localization/database/LocalizationLUT.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,6 @@ public static Double[] get_xy(String map, String column)
245245
*/
246246
public static boolean UpdatePlainLUT() {
247247
try {
248-
if (Distance.VECTOR_SIZE == -1) {
249-
logger.warn("Distance.VECTOR_SIZE is not set. Please set it before building the lookup tables.");
250-
return false;
251-
}
252-
253248
Class.forName(myDriver);
254249
try (Connection conn = DriverManager.getConnection(URL, username, password)) {
255250
conn.setAutoCommit(false);
@@ -258,11 +253,12 @@ public static boolean UpdatePlainLUT() {
258253
Double[] X = get_xy(map, "Xcoordinate");
259254
Double[] Y = get_xy(map, "Ycoordinate");
260255
String[] CommonMac = getColumnMAC(map);
261-
int[][] Pinsert = new int[X.length][Distance.VECTOR_SIZE];
256+
assert CommonMac != null;
257+
int[][] Pinsert = new int[X.length][CommonMac.length];
262258

263259
// This collects the information for the Lookup Table
264260
for (int x = 0; x < X.length; x++) {
265-
for (int currentCol = 0; currentCol < Distance.VECTOR_SIZE; currentCol++) {
261+
for (int currentCol = 0; currentCol < CommonMac.length; currentCol++) {
266262
String getRSS = "SELECT RSS FROM " + DB + "." + TRAININGDATA + " "
267263
+ "WHERE Xcoordinate = ? "
268264
+ "AND Ycoordinate = ? "
@@ -272,7 +268,6 @@ public static boolean UpdatePlainLUT() {
272268
try (PreparedStatement Plainst = conn.prepareStatement(getRSS)) {
273269
Plainst.setDouble(1, X[x]);
274270
Plainst.setDouble(2, Y[x]);
275-
assert CommonMac != null;
276271
Plainst.setString(3, CommonMac[currentCol]);
277272
Plainst.setString(4, map);
278273
try (ResultSet RSS = Plainst.executeQuery()) {
@@ -290,7 +285,7 @@ public static boolean UpdatePlainLUT() {
290285

291286
// This is the SQL Query to insert into the Lookup tables
292287
StringBuilder append = new StringBuilder();
293-
append.append(" ?,".repeat(Math.max(0, Distance.VECTOR_SIZE)));
288+
append.append(" ?,".repeat(CommonMac.length));
294289
append = new StringBuilder(append.substring(0, append.length() - 1));
295290
append.append(");");
296291

@@ -305,7 +300,7 @@ public static boolean UpdatePlainLUT() {
305300
Plain.setInt(1, PrimaryKey + 1);
306301
Plain.setDouble(2, X[PrimaryKey]);
307302
Plain.setDouble(3, Y[PrimaryKey]);
308-
for (int j = 0; j < Distance.VECTOR_SIZE; j++) {
303+
for (int j = 0; j < CommonMac.length; j++) {
309304
Plain.setInt((j + 4), Pinsert[PrimaryKey][j]);
310305
}
311306
Plain.execute();
@@ -343,7 +338,7 @@ public static boolean isNullTuple(int[] row) {
343338
++counter;
344339
}
345340
}
346-
return counter == Distance.VECTOR_SIZE;
341+
return counter == row.length;
347342
}
348343

349344
/**
@@ -521,22 +516,27 @@ public static void getAPManufacturer()
521516
public static void getPlainLookup(List<Long[]> SQLData, List<Double[]> coordinates, String map)
522517
throws ClassNotFoundException, SQLException {
523518
Class.forName(myDriver);
524-
Connection conn = DriverManager.getConnection(URL, username, password);
525-
PreparedStatement st = conn.prepareStatement("select * from " + DB + "." + map + " "
526-
+ "Order By Xcoordinate ASC;");
527-
ResultSet rs = st.executeQuery();
528-
529-
while(rs.next()) {
530-
Long [] RSS = new Long [Distance.VECTOR_SIZE];
531-
Double [] Location = new Double [2];
532-
Location[0] = rs.getDouble("Xcoordinate"); // 2
533-
Location[1] = rs.getDouble("Ycoordinate"); // 3
534-
// Start with 4....
535-
for (int i = 0; i < Distance.VECTOR_SIZE; i++) {
536-
RSS[i] = (long) rs.getInt(i + 4);
519+
try (Connection conn = DriverManager.getConnection(URL, username, password);
520+
PreparedStatement st = conn.prepareStatement(
521+
"select * from " + DB + "." + map + " Order By Xcoordinate ASC;");
522+
ResultSet rs = st.executeQuery()) {
523+
524+
ResultSetMetaData meta = rs.getMetaData();
525+
int totalColumns = meta.getColumnCount();
526+
int rssStartIndex = 4; // Assuming columns 1:ID, 2:X, 3:Y, 4...:RSS
527+
int rssCount = totalColumns - (rssStartIndex - 1);
528+
529+
while (rs.next()) {
530+
Long[] RSS = new Long[rssCount];
531+
Double[] Location = new Double[2];
532+
Location[0] = rs.getDouble("Xcoordinate");
533+
Location[1] = rs.getDouble("Ycoordinate");
534+
for (int i = 0; i < rssCount; i++) {
535+
RSS[i] = (long) rs.getInt(i + rssStartIndex);
536+
}
537+
SQLData.add(RSS);
538+
coordinates.add(Location);
537539
}
538-
SQLData.add(RSS);
539-
coordinates.add(Location);
540540
}
541541
}
542542
}

REUServer/src/main/java/edu/fiu/adwise/fingerprint_localization/database/PrintTables.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* Licensed under the MIT License. See LICENSE file in the project root for details.
66
*/
77

8-
import edu.fiu.adwise.fingerprint_localization.distance_computation.Distance;
98
import org.apache.logging.log4j.LogManager;
109
import org.apache.logging.log4j.Logger;
1110

@@ -83,11 +82,11 @@ public static void printLookupTables() {
8382
try {
8483
String[] all_maps = getMaps();
8584
for (String map : all_maps) {
86-
String[] ColumnMac = getColumnMAC(map);
85+
String [] ColumnMac = getColumnMAC(map);
8786
StringBuilder header = new StringBuilder("Xcoordinate,Ycoordinate,");
88-
for (int i = 0; i < Distance.VECTOR_SIZE; i++) {
89-
assert ColumnMac != null;
90-
if (i == Distance.VECTOR_SIZE - 1) {
87+
assert ColumnMac != null;
88+
for (int i = 0; i < ColumnMac.length; i++) {
89+
if (i == ColumnMac.length - 1) {
9190
header.append(ColumnMac[i]);
9291
} else {
9392
header.append(ColumnMac[i]).append(",");
@@ -115,7 +114,7 @@ public static void printLookupTables() {
115114
// Skip ID, 1
116115
tuple.append(PlainResult.getDouble(2)).append(",");
117116
tuple.append(PlainResult.getDouble(3)).append(",");
118-
for (int i = 0; i < Distance.VECTOR_SIZE; i++) {
117+
for (int i = 0; i < ColumnMac.length; i++) {
119118
String name = meta.getColumnName(i + 4);
120119
tuple.append(PlainResult.getInt(name)).append(",");
121120
}

REUServer/src/main/java/edu/fiu/adwise/fingerprint_localization/distance_computation/Distance.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,7 @@
2929
*/
3030
public abstract class Distance {
3131
/** Default RSS value for missing access points. */
32-
public static int v_c = -120;
33-
34-
/** Number of access points (vector size) used in computation. */
35-
public static int VECTOR_SIZE = -1;
36-
37-
/** Feature Selection Factor: fraction of APs to filter (default 0.9). */
38-
public static double FSF = 0.9;
32+
public static final int v_c = -120;
3933

4034
/** Minimum number of AP matches required for computation. */
4135
protected long MINIMUM_AP_MATCH;
@@ -56,7 +50,7 @@ public abstract class Distance {
5650
protected List<LocalizationResult> resultList = new ArrayList<>();
5751

5852
/** Array of column names (APs) from the database. */
59-
protected static String[] column = null;
53+
protected static String[] lookup_table_column = null;
6054

6155
/** Estimated location coordinates [x, y] (plaintext). */
6256
public Double[] location = new Double[2];
@@ -180,8 +174,8 @@ public List<LocalizationResult> MinimumDistance(alice Niu, boolean isREU2017)
180174
protected boolean has_sufficient_fsf() {
181175
// Step 1, Compute FSF
182176
int count = 0;
183-
for (int j = 0; j < VECTOR_SIZE; j++) {
184-
if(scanAPs[j].equals(column[j])) {
177+
for (int j = 0; j < lookup_table_column.length; j++) {
178+
if(scanAPs[j].equals(lookup_table_column[j])) {
185179
++count;
186180
}
187181
}

REUServer/src/main/java/edu/fiu/adwise/fingerprint_localization/distance_computation/DistanceDGK.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public DistanceDGK(SendLocalizationData in)
5555
S3 = in.S3;
5656
S3_comp = in.S3_comp;
5757
pk = in.pubKey;
58-
if(column == null) {
59-
column = LocalizationLUT.getColumnMAC(in.map);
58+
if(lookup_table_column == null) {
59+
lookup_table_column = LocalizationLUT.getColumnMAC(in.map);
6060
}
6161
// Read from Database
6262
LocalizationLUT.getPlainLookup(this.RSS_ij, this.coordinates, in.map);
@@ -93,8 +93,8 @@ public List<LocalizationResult> MissConstantAlgorithm()
9393
S1_Row = pk.ZERO();
9494
S2_Row = pk.ZERO();
9595

96-
for (int j = 0; j < VECTOR_SIZE;j++) {
97-
if(scanAPs[j].equals(column[j])) {
96+
for (int j = 0; j < lookup_table_column.length; j++) {
97+
if(scanAPs[j].equals(lookup_table_column[j])) {
9898
S1_Row = DGKOperations.add_plaintext(S1_Row, RSS_ij.get(i)[j] * RSS_ij.get(i)[j], pk);
9999
S2_Row = DGKOperations.add(S2_Row, DGKOperations.multiply(S2[j], RSS_ij.get(i)[j], pk), pk);
100100
}
@@ -132,8 +132,8 @@ public List<LocalizationResult> DynamicMatchingAlgorithm()
132132
for (int i = 0; i < RSS_ij.size();i++) {
133133
// Step 1, Compute FSF
134134
count = 0;
135-
for (int j = 0; j < VECTOR_SIZE; j++) {
136-
if(scanAPs[j].equals(column[j])) {
135+
for (int j = 0; j < lookup_table_column.length; j++) {
136+
if(scanAPs[j].equals(lookup_table_column[j])) {
137137
++count;
138138
}
139139
}
@@ -148,8 +148,8 @@ public List<LocalizationResult> DynamicMatchingAlgorithm()
148148
S2_Row = pk.ZERO();
149149
S3_Row = pk.ZERO();
150150

151-
for (int j = 0; j < VECTOR_SIZE;j++) {
152-
if(scanAPs[j].equals(column[j])) {
151+
for (int j = 0; j < lookup_table_column.length; j++) {
152+
if(scanAPs[j].equals(lookup_table_column[j])) {
153153
S1_Row = DGKOperations.add_plaintext(S1_Row, RSS_ij.get(i)[j] * RSS_ij.get(i)[j], pk);
154154
S2_Row = DGKOperations.add(S2_Row, DGKOperations.multiply(S2[j], RSS_ij.get(i)[j], pk), pk);
155155
S3_Row = DGKOperations.add(S3_comp[j], S3_Row, pk);

0 commit comments

Comments
 (0)