...
 
Commits (4)
......@@ -4,7 +4,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
classpath 'com.android.tools.build:gradle:2.3.3'
}
}
......
......@@ -9,11 +9,12 @@ import java.util.ArrayList;
/**
* Copyright (C) 2016 Senception Lda
* Author(s): Igor dos Santos - [email protected]-ception.com *
* Author(s): Igor dos Santos - [email protected] *
* José Soares - [email protected]
* Update to Contextual Manager 2017
* Update to Contextual Manager 2017/2018
* @author Igor dos Santos
* @author José Soares
* @author Rute Sofia
* @version 0.1
*
* @file Contains ContextualManagerAvailability. Class that handles the inference of the availability.
......@@ -21,65 +22,34 @@ import java.util.ArrayList;
public class ContextualManagerAvailability {
/*Formulas:
* 1) A(i) = Σ r(i) / T
* 1) A(i) = Σ r(i) / t, A(i) ⊂ [0,1]
* 2) R(i) = B(i) * B(i) * CPU(i) * MEM(i) * STORAGE(i), r(i) ⊂ [0,1]
*/
/**
* Calculates a number that measures if a device is viable to conduct communication with in a given time.
* @param rList list of all R's calculated so far.
* @return U the array
* @return A Availability
*/
public static ArrayList<Double> calculateA(ArrayList<Double> rList) {
ArrayList<Double> A = new ArrayList<>(rList.size());
Log.d("teste", "A size = " + A.size());
// initializes A to 0
for (int i = 0; i < rList.size(); i++) {
A.add(0d);
}
// starts to run over A positions, a is the index
for (int a = 0; a < A.size(); a++) {
// first position, equal to r only, while the others are a sum of all previous r values
if (a == 0) {
if (rList.get(0) != -1) {
A.set(a, rList.get(0));
}
} else {
// runs over rList to sum all prior values
for (int j = 0; j <= a; j++) {
if (rList.get(j) != -1) {
//A.set(a, (A.get(a) + rList.get(j)/(ContextualManagerCaptureService.TIMESTAMP - System.currentTimeMillis()/60*1000)));
A.set(a, A.get(a) + rList.get(j));
Log.d("teste", "A calculado: " + A.toString());
}
}
public static double calculateA(ArrayList<Double> rList) {
double Availability = 0;
int t=0;
for (int i =0; i < rList.size(); i++) {
if (rList.get(i) != -1) {
++t;
Availability = Availability + rList.get(i);
}
}
return A;
// Availability is an weighted average of all values of r
// @todo improve the average accuracy, by using another type of weighted average
Availability = Availability/t;
Log.d("teste", "r is:"+rList.toString());
Log.d("teste", "t is:"+t);
Log.d("teste", "Availability A = " + Availability);
return Availability;
}
/*
//in the beggining the A is equal to the first R in the list
for (int j = 0; j < rList.size(); j++) {
A.add(rList.get(0).get(j)); //if there's only 1 R -> A = that R converted to double
if(rList.get(0).get(j) != -1) {
Log.d("teste", "r[0] = " + rList.get(0).get(j));
}
}
//if there's only one R in the list, then A is completed.
if( rList.size() == 1){
return A;
}
else { //if there's more then one R -> A = A + R1 + R2...RN
for (int i = 1; i < rList.size(); i++) {
Log.d("teste", "ri = " + rList.get(i).toString());
A = sumArrays(A, rList.get(i)); //A = R0 //A = A + R2 + R3 --> R1+R2
}
}
return A;
}
*/
/**
* This method is an auxiliar method that sums array lists and for each sum,
* divides the result for the instant time.
......
......@@ -2,19 +2,22 @@ package com.senception.contextualmanager.inference;
import com.senception.contextualmanager.databases.ContextualManagerDataSource;
import com.senception.contextualmanager.modals.ContextualManagerAP;
import com.senception.contextualmanager.services.ContextualManagerCaptureService;
import java.util.ArrayList;
import static com.senception.contextualmanager.services.ContextualManagerService.checkWeek;
/**
* Copyright (C) 2016 Senception Lda
* Author(s): Igor dos Santos - [email protected]-ception.com *
* Author(s): Igor dos Santos - [email protected] *
* José Soares - [email protected]
* Update to Contextual Manager 2017
* @author Igor dos Santos
* @author José Soares
* @author Rute Sofia
* @version 0.1
*
* @file Contains ContextualManagerCentrality. Class that handles the inference of the centrality.
* @file Contains ContextualManagerCentrality. Class that handles the centrality computation.
*/
public class ContextualManagerCentrality {
......@@ -24,43 +27,58 @@ public class ContextualManagerCentrality {
p(j)= (encounter * average_encounter_duration)/ (d(i,j) + 1)
encounter: encounter between a pair
average_encounter_duration: avg duration of an encounter between a pair
d(i,j):
A(j) = adjacency's matrix (vector of 1s e 0s) -- considered allways as 1. 1-connected | 0-disconnected
d(i,j): distance in meters between nodes
A(j) = adjacency's matrix 1-connected | 0-disconnected
λ ∈ [0,1]
A(j): adjancency
λ ∈ [0,1] and is currently equal to the ratio between connected peers and all peers
A(j): adjacency
d(i,j) ∈ [0,100]
*/
/**
* Method that calculates the centrality of a device, using
* it's peer's average encounter duration and the number of encounters.
* (Eigenvector)
* Method that calculates the centrality of a device, based on an adaptation
* of Eigenvalue centrality.
* Formula considers, for node i and neighbors j, the numEncounters(i,j) and the
* avgEncounterDuration(i,j)
* it splits the numEncounters*avgEncounterDuration per d, which is the distance in meters
* @todo use the Haversine formula to compute the distance between the two nodes
* Currently, we have set distance to always be one.
* @param dataSource
* @return centrality - the centrality of the device
*/
//NOTE: In the beggining the calculated C will present often as 0.0
//NOTE: In the beginning the calculated C will present often as 0.0
public static double calculateC(ContextualManagerDataSource dataSource){
//double degree; --> to use later
int numEncounters;
double avgEncDur;
double distance = 1; // we're not taking into consideration the distance in this implementation -> only in the future
double lambda = 0.8; // should be the max of a lambda vector.
//get peer list
int numEncounters=0;
double avgEncDur=0;
double distance = 1; // we're not taking into consideration the distance in this implementation -> only in the future
double lambda = 1;
double i=0;
/* work on peer list
* and compute centrality
*/
ArrayList<ContextualManagerAP> peerList;
double centrality = 0;
if(!dataSource.isTableEmpty(checkWeek("peers"))) {
peerList = dataSource.getAllPeers(checkWeek("peers"));
// i corresponds to the number of peers. Starts at 1 (self)
i=1;
for (ContextualManagerAP peer : peerList) {
//if peer is connected (future) --> to use later
numEncounters = peer.getNumEncounters();
avgEncDur = peer.getAvgEncounterDuration();
centrality += numEncounters*avgEncDur/distance;
if (peer.getIsConnected() == 1) {
// @todo distance based on Haversive formula
centrality = centrality+(peer.getNumEncounters()*peer.getAvgEncounterDuration())/distance;
++i;
}
}
/* simplification of lambda, just the ratio of connected peers against all peers
* the more connected peers, the higher the centrality
*/
lambda = i/peerList.size();
}
centrality = centrality * (1/lambda);
centrality = centrality * 1/lambda;
// centrality = centrality/(System.currentTimeMillis()- ContextualManagerCaptureService.TIMESTAMP)/60000;
return centrality;
}
}
......@@ -42,17 +42,19 @@ import static com.senception.contextualmanager.services.ContextualManagerService
/**
* Copyright (C) 2016 Senception Lda
* Author(s): Igor dos Santos - [email protected]-ception.com *
* Author(s): Igor dos Santos - [email protected] *
* José Soares - [email protected]
* Update to Contextual Manager 2017
* @author Igor dos Santos
* @author José Soares
* @author Rute Sofia
* @version 0.1
*
* @file Contains ContextualManagerCaptureService. This service runs in the
* background, for each hour it gets the 4 resource usages (energy,
* cpu, memory and storage) and the apps usage, for each day it
* saves them into the database.
* for DEMO, it is sampling per MINUTE - @todo change later for hour
*/
public class ContextualManagerCaptureService extends Service {
......@@ -63,7 +65,7 @@ public class ContextualManagerCaptureService extends Service {
private static ContextualManagerPhysicalUsage memory;
private static ContextualManagerPhysicalUsage storage;
private static ArrayList<ArrayList<Double>> rList = new ArrayList();
private static ArrayList<Double> availability = new ArrayList<>();
//private static ArrayList<Double> availability = new ArrayList<>();
private static List<UsageStats> ustats;
private static List<ContextualManagerAppUsage> apps = new ArrayList<>();
private final IBinder mBinder = new LocalBinder();
......@@ -104,10 +106,10 @@ public class ContextualManagerCaptureService extends Service {
initializeAppsTable(app);
}
Log.d(TAG, "All the apps used by the device were initialized if they weren't already on the DB");
// Log.d(TAG, "All the apps used by the device were initialized if they weren't already on the DB");
/*
* TODO implement category of an app
* @todo implement category of an app
* Creates a thread where the categories of all apps in the device will be determined
* by getting it's category in the google apstore, to make this possible, internet
* connection is necessary, if there is no internet connection, categories will not be available.
......@@ -123,7 +125,7 @@ public class ContextualManagerCaptureService extends Service {
setAlarm();
Log.d(TAG, "Alarms setted to hourly and daily");
// Log.d(TAG, "Alarms setted to hourly and daily");
}
@Override
......@@ -256,28 +258,27 @@ public class ContextualManagerCaptureService extends Service {
capturePhysicalUsage(memory);
capturePhysicalUsage(storage);
Log.d(TAG, "Captured all the physical resources.");
// Log.d(TAG, "Captured all the physical resources.");
/*Availability Calculation:*/
// Captures the R (b*b*cpu*mem*storage) every hour (for tests: min)
//rList.add();
//Log.d(TAG, "RList: " + rList);
// Calculates the A - availability (sum of all Rs) every hour (for tests: min)
availability = ContextualManagerAvailability.calculateA(ContextualManagerAvailability.calculateR(energy.getUsagePerHour(), cpu.getUsagePerHour(), memory.getUsagePerHour(), storage.getUsagePerHour()));
Log.d(TAG, availability.toString());
int currentMinute = currentTime.get(Calendar.MINUTE); //todo change to hourly
double A = availability.get(currentMinute);
Log.d(TAG, "Calculated A: " + A);
double Availability = ContextualManagerAvailability.calculateA(ContextualManagerAvailability.calculateR(energy.getUsagePerHour(), cpu.getUsagePerHour(), memory.getUsagePerHour(), storage.getUsagePerHour()));
//int currentMinute = currentTime.get(Calendar.MINUTE); //todo change to hourly
// double A = availability.get(currentMinute);
// Log.d(TAG, "In CaptureService Calculated A: " + Availability);
/*Centrality Calculation:*/
double C = ContextualManagerCentrality.calculateC(dataSource);
Log.d(TAG, "Calculated C: " + C);
// Log.d(TAG, "In CaptureService Calculated C: " + C);
/* Saves A and C into the database */
ContextualManagerAP mySelf = new ContextualManagerAP();
mySelf.setSSID("self");
mySelf.setBSSID(MacSecurity.md5Hash("self"));
mySelf.setAvailability(A);
mySelf.setAvailability(Availability);
mySelf.setCentrality(C);
if(!dataSource.hasPeer(mySelf.getBSSID(), ContextualManagerService.checkWeek("peers"))) {
dataSource.registerNewPeers(mySelf, ContextualManagerService.checkWeek("peers"));
......@@ -286,9 +287,9 @@ public class ContextualManagerCaptureService extends Service {
dataSource.updatePeer(mySelf, ContextualManagerService.checkWeek("peers"));
}
Log.d(TAG, "A and C saved into the DB on the correspondent peer table of the current day of the week as the peer 'self'");
//Log.d(TAG, "A and C saved into the DB on the correspondent peer table of the current day of the week as the peer 'self'");
/*Similarity Calculation*/
/*Similarity computation*/
//get peer list, and foreach one updates its similarity
if(!dataSource.isTableEmpty(checkWeek("peers"))) {
ArrayList<ContextualManagerAP> peerList = dataSource.getAllPeers(checkWeek("peers"));
......@@ -296,21 +297,39 @@ public class ContextualManagerCaptureService extends Service {
for (ContextualManagerAP peer : peerList) {
double numEncounters = peer.getNumEncounters();
double avgEncDur = peer.getAvgEncounterDuration();
double similarity = numEncounters*avgEncDur;
/* similarity (I) should be computed as:
* similarity(a,b) = 1 - abs(a-b)/max(a,b)
* a: node's value and b peer value
* @todo cosine similarity, for array values (currently A and C are doubles)
* for the routing interface, similarity is computed simply based on
* the numEncounters and avgEncounter duration.
* Similarity A weights similarity between availability
* Similarity C weights similarity between centralities of the node
* @todo improve similarity C and A as well as similarity based on node interests (e.g., apps used)
*/
/* double similarityA = 1-(Math.abs(peer.getAvailability()-mySelf.getAvailability())/Math.max(peer.getAvailability(),mySelf.getAvailability()));
double similarityC = 1-(Math.abs(peer.getCentrality()-mySelf.getCentrality())/Math.max(peer.getCentrality(),mySelf.getCentrality()));
double similarity = (similarityA+similarityC)/2;
*/
// similarity for the routing
double similarity = (numEncounters*avgEncDur);
peer.setSimilarity(similarity);
dataSource.updatePeer(peer, checkWeek("peers"));
Log.d(TAG, "Calculated I: " + similarity + " and saved it on the DB" + "for peer: " + peer.getSSID());
Log.d(TAG, "Similarity with peer: " + peer.getSSID() + " is: "+similarity);
}
}
/* Captures the apps usage */
captureAppsUsage();
Log.d(TAG, "Captured the apps usage");
// Log.d(TAG, "Captured the apps usage");
ContextualManagerMainActivity.backupDB(context); //todo eliminate
}
else{ // if daily: Saves the usage percentage into the database
Log.d(TAG, "Alarm triggered after 2min");
// Log.d(TAG, "Alarm triggered after 2min");
/*Saves the 4 physical resource usage into the database*/
energy.setDayOfTheWeek(String.valueOf(newDayOfTheWeek));
dataSource.updateResourceUsage(energy);
......@@ -421,7 +440,7 @@ public class ContextualManagerCaptureService extends Service {
}
/**
* TODO category
* @todo category
* Updates the category of each app in the apps list if there is internet connection
* and the respective app has a category affiliated in google play store -- To-complete
* https://stackoverflow.com/questions/10710442/how-to-get-category-for-each-app-on-device-on-android
......
......@@ -58,6 +58,8 @@ import android.view.WindowManager;
import android.widget.TimePicker;
import android.widget.Toast;
import static com.senception.contextualmanager.services.ContextualManagerCaptureService.TIMESTAMP;
/**
* Copyright (C) 2016 Senception Lda
* Author(s): Igor dos Santos - [email protected] *
......@@ -259,7 +261,7 @@ public class ContextualManagerService extends Service{
ap.setNumEncounters(1);
ap.setStartEncounter((int)(System.currentTimeMillis()/1000)); //time in seconds System.currentTimeMillis()/1000
Log.d(TAG, "StartEncounter: " + ap.getStartEncounter());
ap.setEndEncounter((int)(System.currentTimeMillis()/1000));
ap.setEndEncounter((int)(System.currentTimeMillis()/1000)); //time in seconds
Log.d(TAG, "EndEncounter: " + ap.getEndEncounter());
ap.setAvgEncounterDuration(ap.getEndEncounter()-ap.getStartEncounter());
ap.setSimilarity(ap.getNumEncounters()*ap.getAvgEncounterDuration());
......@@ -269,7 +271,9 @@ public class ContextualManagerService extends Service{
double peerAvgEncDur = ap.getAvgEncounterDuration();
int peerEndEnc = ap.getEndEncounter();
int peerStartEnc = ap.getStartEncounter();
ap.setAvgEncounterDuration((peerAvgEncDur + (peerEndEnc-peerStartEnc))/ (double) (System.currentTimeMillis() / 1000));
//computes the average encounter duration based on an EMA, where alpha has been set as 0,3. We give more weight to prior values
// ap.setAvgEncounterDuration((peerAvgEncDur + (peerEndEnc-peerStartEnc))/ (double) (System.currentTimeMillis() / 1000));
ap.setAvgEncounterDuration(((0.7*peerAvgEncDur)+0.3*(peerEndEnc-peerStartEnc))/((System.currentTimeMillis()-TIMESTAMP)/1000));
dataSource.registerNewPeers(ap, checkWeek("peers"));
Log.d(TAG, "The peer " + ap.getSSID() + " was found and saved into the DB");
}
......@@ -295,7 +299,7 @@ public class ContextualManagerService extends Service{
Log.d(TAG, "startEnc = " + peerStartEnc);
int duration = (peerEndEnc-peerStartEnc);
Log.d(TAG, "duration = " + duration);
peer.setAvgEncounterDuration((peerAvgEncDur + duration)/ (double) ((System.currentTimeMillis() - ContextualManagerCaptureService.TIMESTAMP)/ 60*1000));
peer.setAvgEncounterDuration((peerAvgEncDur + duration)/ (double) ((System.currentTimeMillis() - TIMESTAMP)/ 60*1000));
Log.d(TAG, "The avgEncounterDuration of the peer " + peer.getSSID() + " is " + peer.getAvgEncounterDuration());
dataSource.updatePeer(peer, checkWeek("peers"));
Log.d(TAG, "The peer " + ap.getSSID() + " was found and updated into the DB");
......