Commit 0591ac83 authored by José Soares's avatar José Soares

Changed methods calculateR and calculateA from class Availability

parent e4e581be
package com.senception.contextualmanager.inference;
import android.util.Log;
import com.senception.contextualmanager.activities.ContextualManagerMainActivity;
import com.senception.contextualmanager.services.ContextualManagerCaptureService;
......@@ -28,12 +30,41 @@ public class ContextualManagerAvailability {
* @param rList list of all R's calculated so far.
* @return U the array
*/
public static ArrayList<Double> calculateA(ArrayList<ArrayList<Double>> rList){
ArrayList<Double> A = new ArrayList<>();
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());
}
}
}
}
return A;
}
/*
//in the beggining the A is equal to the first R in the list
for (int j = 0; j < rList.get(0).size(); j++) {
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.
......@@ -42,12 +73,13 @@ public class ContextualManagerAvailability {
}
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.
......@@ -91,10 +123,21 @@ public class ContextualManagerAvailability {
*/
public static ArrayList<Double> calculateR(ArrayList<Double> e, ArrayList<Double>
cpu, ArrayList<Double> mem, ArrayList<Double> storage){
Log.d("teste", "e = " + e.toString());
Log.d("teste", "cpu = " + cpu.toString());
Log.d("teste", "mem = " + mem.toString());
Log.d("teste", "s = " + storage.toString());
ArrayList<Double> e2 = multiplyArrays(e, e); // e square
ArrayList<Double> e2Cpu = multiplyArrays(e2, cpu); // e2 * cpu
ArrayList<Double> memStor = multiplyArrays(mem, storage); // mem * storage
ArrayList<Double> r = multiplyArrays(e2Cpu, memStor);
//todo resources between [0..100], but we need r between [0..1]
double d = Math.pow(100,5);
for (int i = 0; i < r.size(); i++) {
if(r.get(i) != -1){
r.set(i, r.get(i) / d);
}
}
return r;
}
......@@ -114,15 +157,15 @@ public class ContextualManagerAvailability {
}
//-1*30 = 30
else if ( usagePerHour1.get(i) == -1 && usagePerHour2.get(i) != -1){
res.add((usagePerHour2.get(i))/100);
res.add(usagePerHour2.get(i));
}
//30*-1 = 30
else if (usagePerHour1.get(i) != -1 && usagePerHour2.get(i) == -1 ){
res.add(( usagePerHour1.get(i))/100);
res.add(usagePerHour1.get(i));
}
//30*30 = 900
else {
res.add(( usagePerHour1.get(i)/100) * (usagePerHour2.get(i)/100));
res.add(usagePerHour1.get(i) * usagePerHour2.get(i));
}
}
return res;
......
......@@ -260,10 +260,10 @@ public class ContextualManagerCaptureService extends Service {
/*Availability Calculation:*/
// Captures the R (b*b*cpu*mem*storage) every hour (for tests: min)
rList.add(ContextualManagerAvailability.calculateR(energy.getUsagePerHour(), cpu.getUsagePerHour(), memory.getUsagePerHour(), storage.getUsagePerHour()));
//rList.add();
//Log.d(TAG, "RList: " + rList);
// Calculates the A - availability (sum of all Rs) every hour (for tests: min)
availability = ContextualManagerAvailability.calculateA(rList);
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);
......
......@@ -256,13 +256,13 @@ public class ContextualManagerService extends Service{
ap.setLongitude(longitude);
ap.setAvailability(0.0);
ap.setCentrality(0.0);
ap.setSimilarity(0.0);
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));
Log.d(TAG, "EndEncounter: " + ap.getEndEncounter());
ap.setAvgEncounterDuration(0);
ap.setAvgEncounterDuration(ap.getEndEncounter()-ap.getStartEncounter());
ap.setSimilarity(ap.getNumEncounters()*ap.getAvgEncounterDuration());
Log.d(TAG, "AvgDuration: " + ap.getAvgEncounterDuration());
ap.setIsConnected(1);
/*Average encounter calculation*/
......@@ -295,7 +295,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() / 1000));
peer.setAvgEncounterDuration((peerAvgEncDur + duration)/ (double) ((System.currentTimeMillis() - ContextualManagerCaptureService.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");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment