From f72ba39b1a5e74c52a719c8f992df22530c44fbb Mon Sep 17 00:00:00 2001 From: sebasgomezj Date: Fri, 17 Jun 2016 13:32:11 +0200 Subject: [PATCH 1/4] Ring Based --- .../Scheduler/Ring-based-v1/AuxClass.java | 78 +++++ .../Scheduler/Ring-based-v1/Satellite.java | 210 +++++++++++++ src/Other/Scheduler/Ring-based-v1/Task.java | 91 ++++++ .../Scheduler/Ring-based-v1/ringBasedV1.java | 49 ++++ .../Ring-based-v1/ringBasedV1Test.java | 68 +++++ .../Scheduler/Ring-based-v2/AuxClass.java | 67 +++++ .../Scheduler/Ring-based-v2/Satellite.java | 234 +++++++++++++++ src/Other/Scheduler/Ring-based-v2/Task.java | 91 ++++++ .../Scheduler/Ring-based-v2/ringBasedV2.java | 49 ++++ .../Ring-based-v2/ringBasedV2Test.java | 71 +++++ .../Scheduler/Ring-based-v3/AuxClass.java | 87 ++++++ .../Scheduler/Ring-based-v3/Satellite.java | 275 ++++++++++++++++++ src/Other/Scheduler/Ring-based-v3/Task.java | 104 +++++++ .../Scheduler/Ring-based-v3/ringBasedV3.java | 49 ++++ .../Ring-based-v3/ringBasedV3Test.java | 79 +++++ 15 files changed, 1602 insertions(+) create mode 100644 src/Other/Scheduler/Ring-based-v1/AuxClass.java create mode 100644 src/Other/Scheduler/Ring-based-v1/Satellite.java create mode 100644 src/Other/Scheduler/Ring-based-v1/Task.java create mode 100644 src/Other/Scheduler/Ring-based-v1/ringBasedV1.java create mode 100644 src/Other/Scheduler/Ring-based-v1/ringBasedV1Test.java create mode 100644 src/Other/Scheduler/Ring-based-v2/AuxClass.java create mode 100644 src/Other/Scheduler/Ring-based-v2/Satellite.java create mode 100644 src/Other/Scheduler/Ring-based-v2/Task.java create mode 100644 src/Other/Scheduler/Ring-based-v2/ringBasedV2.java create mode 100644 src/Other/Scheduler/Ring-based-v2/ringBasedV2Test.java create mode 100644 src/Other/Scheduler/Ring-based-v3/AuxClass.java create mode 100644 src/Other/Scheduler/Ring-based-v3/Satellite.java create mode 100644 src/Other/Scheduler/Ring-based-v3/Task.java create mode 100644 src/Other/Scheduler/Ring-based-v3/ringBasedV3.java create mode 100644 src/Other/Scheduler/Ring-based-v3/ringBasedV3Test.java diff --git a/src/Other/Scheduler/Ring-based-v1/AuxClass.java b/src/Other/Scheduler/Ring-based-v1/AuxClass.java new file mode 100644 index 0000000..79dcb60 --- /dev/null +++ b/src/Other/Scheduler/Ring-based-v1/AuxClass.java @@ -0,0 +1,78 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package ring.based.v1; + +/** + * + * @author SebAs + * Version 1 + */ +public class AuxClass{ + private int it,turn,nsat; + private boolean[] changed; + public boolean breakThread; + + public AuxClass(int nsat){ + this.nsat = nsat; + it = 1; + turn = 1; + changed = new boolean[nsat]; + for(int i=0; i tasksAssigned; + private boolean finalVotes; + + public Satellite(int id, int ntask, int nsat, AuxClass aux, Task task){ + this.id = id; + this.ntask = ntask; + this.nsat = nsat; + idTable = new double[ntask]; + auxTable = new double[ntask]; + comTable = new double[ntask][2]; + this.task = task; + this.aux = aux; + finalVotes = false; + tasksAssigned = new ArrayList(); + } + + public void setNeighbour(Satellite nextSat){ + this.nextSat = nextSat; + } + + public void getVotes(){ + idTable = task.getVotes(enMax,sMax,id); + } + + public void checkVotes(){ + if(!aux.votesChanged()){ + finalVotes = true; + } + } + + public void vote(){ + int index = 0; + int tmp = 0; + //get the maximum + double max = 0; + for (int k=0; k max){ + max = auxTable[k]; + index = k; + } + } + while(tmp < ntask){ + if(comTable[index][0] < max){ + comTable[index][0] = max; + comTable[index][1] = id; + aux.setChangeTo(true,id); + break; + } else if ((comTable[index][0] == max) && (comTable[index][1] == id)){ + aux.setChangeTo(false,id); + break; + } else { + auxTable[index] = 0; + max=0; + for (int j=0; j max)){ + max = auxTable[j]; + index = j; + } + } + if(max == 0){ + aux.setChangeTo(false,id); + break; + } + } + tmp++; + } + } + + public void giveTable(double[][] t) { + comTable = t.clone(); + } + + @Override + public synchronized void run(){ + Tstart = System.currentTimeMillis(); + try { + while(true){ + while(aux.getTurn()!= id){ + aux.waitSat(); + } + if(aux.getIt() == 1){ + enMax = task.getEnergyCapacity(); + sMax = task.getStorageCapacity(); + getVotes(); + auxTable = idTable.clone(); + } else { + idTable = task.getVotesAgain(id);//per alguna raó després de la primera iteració + //els vots dels satelits es barrejen, aleshores torno + //a copiar-los. + } + if(aux.breakThread){ + aux.incTurn(); + aux.notifySat(); + break; + } + if(id == 1){ + checkVotes(); + if(finalVotes){ + aux.breakThread = true; + aux.incTurn(); + aux.notifySat(); + break; + } + System.out.println("ITERATION "+(aux.getIt())); + } + //vote and give table to the next satellite + vote(); + nextSat.giveTable(comTable); + //change turn + if(id != nsat){ + aux.incTurn(); + aux.notifySat(); + } else { + nTasksAssig = 0; + for(int i=0; i otro voto, modificar tabla + sino, pasar a votar por tu siguiente mejor tarea + si no puedes votar por ninguna tarea, indicar que tu voto es definitivo: votoDefinitivo[id] = true; + DUDA: quién comprueba si todos los votos son definitivos (el primer satelite o el actual?) + si no son definitivos: pasar la tabla al siguiente + si son definitivos: break + 4)pasar la tabla al siguiente + + * Si no es primera vuelta: + 1)comprobar si tu voto ha sido cambiado + si no hay cambio: votoDefinitivo[id] = true; + si hay cambio: votar por tu siguiente mejor tarea + si no puedes votar, votoDefinitivo[id] = true + DUDA: quién comprueba si todos los votos son definitivos (el primer satelite o el actual?) + si no son definitivos: pasar la tabla al siguiente + si son definitivos: break + 2)pasar la tabla al siguiente + +*/ \ No newline at end of file diff --git a/src/Other/Scheduler/Ring-based-v1/Task.java b/src/Other/Scheduler/Ring-based-v1/Task.java new file mode 100644 index 0000000..2e3cc70 --- /dev/null +++ b/src/Other/Scheduler/Ring-based-v1/Task.java @@ -0,0 +1,91 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package ring.based.v1; + +import java.util.Random; + +/** + * + * @author SebAs + * Version 1 + */ +public final class Task { + private int[] taskStorage, taskEnergy; + private int ntask; + private double votes[], satVotes[][]; + + public Task(int ntask, int nsat){ + this.ntask = ntask; + taskStorage = new int[ntask]; + taskEnergy = new int[ntask]; + votes = new double[ntask]; + satVotes = new double[nsat][ntask]; + } + + public void createTaskInfo(){ + for(int i=0; i sMax || taskEnergy[i] > enMax){ + votes[i] = 0.0; + } + else{ + votes[i] = (taskStorage[i]/sMax + taskEnergy[i]/enMax)/2; + } + } + for(i=0;i aEnd) { + throw new IllegalArgumentException("Start cannot exceed End."); + } + long range = (long)aEnd - (long)aStart + 1; + long fraction = (long)(range * aRandom.nextDouble()); + int randomNumber = (int)(fraction + aStart); + return randomNumber; + } +} diff --git a/src/Other/Scheduler/Ring-based-v1/ringBasedV1.java b/src/Other/Scheduler/Ring-based-v1/ringBasedV1.java new file mode 100644 index 0000000..13a0578 --- /dev/null +++ b/src/Other/Scheduler/Ring-based-v1/ringBasedV1.java @@ -0,0 +1,49 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package ring.based.v1; + +/** + * + * @author SebAs + * Version 1 + */ +public class ringBasedV1 { + + /** + * @param args the command line arguments + * @throws java.lang.InterruptedException + */ + public static void main(String[] args) throws InterruptedException{ + int ntask = 500; + int nsat = 500; + Task task = new Task(ntask,nsat); + task.createTaskInfo(); + AuxClass aux = new AuxClass(nsat); + Thread[] t = new Thread[nsat]; + Satellite[] sat = new Satellite[nsat]; + //create satellites + for(int j=0; j changes; + private ArrayList numChanges; + private ArrayList tasksAssigned; + private boolean noChanges; + + public Satellite(int id,int ntask, int nsat, AuxClass aux, Task task){ + this.id = id; + this.ntask = ntask; + this.nsat = nsat; + this.aux = aux; + this.task = task; + idTable = new double[ntask]; + auxTable = new double[ntask]; + comTable = new double[ntask][2]; + noChanges = false; + change = new Double[3]; + changes = new ArrayList(); + numChanges = new ArrayList(); + tasksAssigned = new ArrayList(); + } + + public void getVotes(){ + idTable = task.getVotes(enMax,sMax,id); + } + + public void passChanges(ArrayList previousChanges){ + int i, idx; + Double tmp[]; + i = 0; + if(!previousChanges.isEmpty()){ + while(i max){ + max = auxTable[k]; + index = k; + } + } + //put the best possible vote + while(tmp < ntask){ + if(comTable[index][0] < max){ + comTable[index][0] = max; + comTable[index][1] = id; + change[0] = (double) index+1; + change[1] = max; + change[2] = (double) id; + changes.add(change); + break; + }else if((comTable[index][0] == max) && (comTable[index][1] == id)){ + break; + } + else{ + auxTable[index] = 0; + max=0; + for (int j=0; j max){ + max = auxTable[j]; + index = j; + } + } + if(max == 0) break; + } + tmp++; + } + + } + + public void setNeighbour(Satellite nextSat){ + this.nextSat = nextSat; + } + + public void checkChanges(){ + if(changes.isEmpty()){ + noChanges = true; + } + } + + public void removeNextSatChanges(){ + int i; + Double tmp[]; + i = 0; + if(!changes.isEmpty()){ + while(i1){//satellite 1 checks changes minimum after the first iteration + checkChanges(); + if(noChanges){ + aux.setBreakThread(true); + aux.incTurn(); + aux.notifySat(); + break; + } + System.out.println("ITERATION "+(aux.getIt())); + } + vote(); + removeNextSatChanges(); + nextSat.passChanges(changes); + if(id != nsat){//change turn + aux.incTurn(); + aux.notifySat(); + } else { + nTasksAssig = 0; + for(int i=0; i sMax || taskEnergy[i] > enMax){ + votes[i] = 0.0; + } + else{ + votes[i] = (taskStorage[i]/sMax + taskEnergy[i]/enMax)/2; + } + } + for(i=0;i aEnd) { + throw new IllegalArgumentException("Start cannot exceed End."); + } + long range = (long)aEnd - (long)aStart + 1; + long fraction = (long)(range * aRandom.nextDouble()); + int randomNumber = (int)(fraction + aStart); + return randomNumber; + } +} diff --git a/src/Other/Scheduler/Ring-based-v2/ringBasedV2.java b/src/Other/Scheduler/Ring-based-v2/ringBasedV2.java new file mode 100644 index 0000000..136905b --- /dev/null +++ b/src/Other/Scheduler/Ring-based-v2/ringBasedV2.java @@ -0,0 +1,49 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package ringbased; + +/** + * + * @author Sebas + * Version 2 + */ +public class ringBasedV2 { + + /** + * @param args the command line arguments + * @throws java.lang.InterruptedException + */ + public static void main(String[] args) throws InterruptedException{ + int ntask = Integer.parseInt(args[1]); + int nsat = Integer.parseInt(args[0]); + Task task = new Task(ntask,nsat); + task.createTaskInfo(); + AuxClass aux = new AuxClass(nsat); + Thread[] t = new Thread[nsat]; + Satellite[] sat = new Satellite[nsat]; + //create satellites + for(int j=0; j changes; + private ArrayList tasksAssigned,numChanges,totalFreeStorage, totalFreeEnergy; + private boolean noChanges, tasksVoted[], taskAlreadyVoted[]; + + public Satellite(int id, int ntask, int nsat, AuxClass aux, Task task) { + this.id = id; + this.ntask = ntask; + this.nsat = nsat; + this.aux = aux; + this.task = task; + idTable = new double[ntask]; + auxTable = new double[ntask]; + comTable = new double[ntask][2]; + orderedVotes = new double[ntask][2]; + tasksVoted = new boolean[ntask]; + noChanges = false; + change = new Double[3]; + auxChange = new Double[3]; + changes = new ArrayList(); + tasksAssigned = new ArrayList(); + numChanges = new ArrayList(); + totalFreeStorage = new ArrayList(); + totalFreeEnergy = new ArrayList(); + taskAlreadyVoted = new boolean[ntask]; + } + + public void getVotes() { + idTable = task.getVotes(enMax,sMax,id); + } + + public void passChanges(ArrayList previousChanges) { + int i, idx; + Double tmp[]; + i = 0; + if (!previousChanges.isEmpty()) { + while (i < previousChanges.size()) { + tmp = previousChanges.get(i); + idx = (int) tmp[0].longValue(); + comTable[idx - 1][0] = tmp[1]; + comTable[idx - 1][1] = tmp[2]; + i++; + } + } + changes = previousChanges; + } + + public void orderVotes(){ + int k = 0; + double max = 0; + int index = 0; + while(k < ntask){ + for (int i = 0; i < ntask; i++) { + if (auxTable[i] > max) { + max = auxTable[i]; + index = i; + } + } + orderedVotes[k][0] = max; + orderedVotes[k][1] = index+1;//index + 1 = task number + auxTable[index] = 0; + max = 0; + k++; + } + } + + public void vote() { + int index; + int j,k; + if(aux.getIt() == 1){ + orderVotes(); + } + /*put the best possible votes*/ + k = 0; + while (k < ntask) { + if(k == 0 && aux.getIt() > 1) {//restore capacity if other satellite assigned + j = 0; //itself a task that was voted previously by this satellite + while(j < ntask){ + index = (int) orderedVotes[j][1] - 1; + if(tasksVoted[index] && (comTable[index][0] > orderedVotes[j][0])){ + sMax = sMax + task.storage(index); + enMax = enMax + task.energy(index); + tasksVoted[index] = false; + } + j++; + } + } + index = (int) orderedVotes[k][1] - 1; + if(orderedVotes[k][0] == 0){ + break; + } + else if((comTable[index][0] < orderedVotes[k][0]) && (task.storage(index) <= sMax && task.energy(index) <= enMax)){ + comTable[index][0] = orderedVotes[k][0]; + comTable[index][1] = id; + auxChange[0] = (double) (index + 1); + auxChange[1] = orderedVotes[k][0]; + auxChange[2] = (double) id; + change = auxChange.clone(); + changes.add(change); + tasksVoted[index] = true; + sMax = sMax - task.storage(index); + enMax = enMax - task.energy(index); + } + k++; + } + } + + public void setNeighbour(Satellite nextSat) { + this.nextSat = nextSat; + } + + public void checkChanges() { + if (changes.isEmpty()){ + noChanges = true; + } + } + + public void removeNextSatChanges() { + int i = 0; + Double tmp[]; + if (!changes.isEmpty()) { + while (i < changes.size()) { + tmp = changes.get(i); + if (tmp[2] == 1 && id == nsat) { + changes.remove(i); + i--; + } else if (tmp[2] == id + 1) { + changes.remove(i); + i--; + } + i++; + } + } + } + + @Override + public void run() { + Tstart = System.currentTimeMillis(); + try { + while (true) { + while (aux.getTurn() != id) { + aux.waitSat(); + } + if (aux.breakThread()) { //Thread breaks if breakThread = true + aux.incTurn(); + aux.notifySat(); + break; + } + if (aux.getIt() == 1) {//get storage and energy capacity and get votes + enMax = task.getEnergyCapacity(); + sMax = task.getStorageCapacity(); + aux.passCapacity(id, sMax, enMax); + getVotes(); + auxTable = idTable.clone(); + } else { + idTable = task.getVotesAgain(id);//per alguna raó després de la primera iteració + //els vots dels satelits es barrejen, aleshores torno + //a copiar-los. + } + if (id == 1 && aux.getIt()>1) {//satellite 1 checks changes + checkChanges(); + if (noChanges) { + aux.setBreakThread(true); + aux.incTurn(); + aux.notifySat(); + break; + } + } + vote();//vote and pass changes to the next satellite + removeNextSatChanges(); + nextSat.passChanges(changes); + if (id != nsat){//change turn + aux.incTurn(); + aux.notifySat(); + } else { + if(aux.getIt() == 1){ + eMaxTotal = aux.getMaxCapacities()[0]; + sMaxTotal = aux.getMaxCapacities()[1]; + totalFreeEnergy.add(eMaxTotal); + totalFreeStorage.add(sMaxTotal); + } + nTasksAssig = 0; + for(int i=0; i sMax || taskEnergy[i] > enMax){ + votes[i] = 0; + } + else{ + votes[i] = (taskStorage[i]/sMax + taskEnergy[i]/enMax)/2; + } + } + for(i=0;i aEnd) { + throw new IllegalArgumentException("Start cannot exceed End."); + } + long range = (long)aEnd - (long)aStart + 1; + long fraction = (long)(range * aRandom.nextDouble()); + int randomNumber = (int)(fraction + aStart); + return randomNumber; + } + + + +} diff --git a/src/Other/Scheduler/Ring-based-v3/ringBasedV3.java b/src/Other/Scheduler/Ring-based-v3/ringBasedV3.java new file mode 100644 index 0000000..82d429a --- /dev/null +++ b/src/Other/Scheduler/Ring-based-v3/ringBasedV3.java @@ -0,0 +1,49 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package ring.based.v3; + +/** + * + * @author Sebas + * Version 3 + */ +public class ringBasedV3 { + /** + * @param args the command line arguments + * @throws java.lang.InterruptedException + */ + public static void main(String[] args) throws InterruptedException { + int ntask = Integer.parseInt(args[1]); + int nsat = Integer.parseInt(args[0]); + Task task = new Task(ntask,nsat); + task.createTaskInfo();//pasar datos de args (taskStorage y taskEnergy) + AuxClass aux = new AuxClass(nsat); + Thread[] t = new Thread[nsat]; + Satellite[] sat = new Satellite[nsat]; + //create satellites + for(int j=0; j Date: Fri, 17 Jun 2016 13:40:07 +0200 Subject: [PATCH 2/4] Revert "Ring Based" This reverts commit f72ba39b1a5e74c52a719c8f992df22530c44fbb. --- .../Scheduler/Ring-based-v1/AuxClass.java | 78 ----- .../Scheduler/Ring-based-v1/Satellite.java | 210 ------------- src/Other/Scheduler/Ring-based-v1/Task.java | 91 ------ .../Scheduler/Ring-based-v1/ringBasedV1.java | 49 ---- .../Ring-based-v1/ringBasedV1Test.java | 68 ----- .../Scheduler/Ring-based-v2/AuxClass.java | 67 ----- .../Scheduler/Ring-based-v2/Satellite.java | 234 --------------- src/Other/Scheduler/Ring-based-v2/Task.java | 91 ------ .../Scheduler/Ring-based-v2/ringBasedV2.java | 49 ---- .../Ring-based-v2/ringBasedV2Test.java | 71 ----- .../Scheduler/Ring-based-v3/AuxClass.java | 87 ------ .../Scheduler/Ring-based-v3/Satellite.java | 275 ------------------ src/Other/Scheduler/Ring-based-v3/Task.java | 104 ------- .../Scheduler/Ring-based-v3/ringBasedV3.java | 49 ---- .../Ring-based-v3/ringBasedV3Test.java | 79 ----- 15 files changed, 1602 deletions(-) delete mode 100644 src/Other/Scheduler/Ring-based-v1/AuxClass.java delete mode 100644 src/Other/Scheduler/Ring-based-v1/Satellite.java delete mode 100644 src/Other/Scheduler/Ring-based-v1/Task.java delete mode 100644 src/Other/Scheduler/Ring-based-v1/ringBasedV1.java delete mode 100644 src/Other/Scheduler/Ring-based-v1/ringBasedV1Test.java delete mode 100644 src/Other/Scheduler/Ring-based-v2/AuxClass.java delete mode 100644 src/Other/Scheduler/Ring-based-v2/Satellite.java delete mode 100644 src/Other/Scheduler/Ring-based-v2/Task.java delete mode 100644 src/Other/Scheduler/Ring-based-v2/ringBasedV2.java delete mode 100644 src/Other/Scheduler/Ring-based-v2/ringBasedV2Test.java delete mode 100644 src/Other/Scheduler/Ring-based-v3/AuxClass.java delete mode 100644 src/Other/Scheduler/Ring-based-v3/Satellite.java delete mode 100644 src/Other/Scheduler/Ring-based-v3/Task.java delete mode 100644 src/Other/Scheduler/Ring-based-v3/ringBasedV3.java delete mode 100644 src/Other/Scheduler/Ring-based-v3/ringBasedV3Test.java diff --git a/src/Other/Scheduler/Ring-based-v1/AuxClass.java b/src/Other/Scheduler/Ring-based-v1/AuxClass.java deleted file mode 100644 index 79dcb60..0000000 --- a/src/Other/Scheduler/Ring-based-v1/AuxClass.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - -package ring.based.v1; - -/** - * - * @author SebAs - * Version 1 - */ -public class AuxClass{ - private int it,turn,nsat; - private boolean[] changed; - public boolean breakThread; - - public AuxClass(int nsat){ - this.nsat = nsat; - it = 1; - turn = 1; - changed = new boolean[nsat]; - for(int i=0; i tasksAssigned; - private boolean finalVotes; - - public Satellite(int id, int ntask, int nsat, AuxClass aux, Task task){ - this.id = id; - this.ntask = ntask; - this.nsat = nsat; - idTable = new double[ntask]; - auxTable = new double[ntask]; - comTable = new double[ntask][2]; - this.task = task; - this.aux = aux; - finalVotes = false; - tasksAssigned = new ArrayList(); - } - - public void setNeighbour(Satellite nextSat){ - this.nextSat = nextSat; - } - - public void getVotes(){ - idTable = task.getVotes(enMax,sMax,id); - } - - public void checkVotes(){ - if(!aux.votesChanged()){ - finalVotes = true; - } - } - - public void vote(){ - int index = 0; - int tmp = 0; - //get the maximum - double max = 0; - for (int k=0; k max){ - max = auxTable[k]; - index = k; - } - } - while(tmp < ntask){ - if(comTable[index][0] < max){ - comTable[index][0] = max; - comTable[index][1] = id; - aux.setChangeTo(true,id); - break; - } else if ((comTable[index][0] == max) && (comTable[index][1] == id)){ - aux.setChangeTo(false,id); - break; - } else { - auxTable[index] = 0; - max=0; - for (int j=0; j max)){ - max = auxTable[j]; - index = j; - } - } - if(max == 0){ - aux.setChangeTo(false,id); - break; - } - } - tmp++; - } - } - - public void giveTable(double[][] t) { - comTable = t.clone(); - } - - @Override - public synchronized void run(){ - Tstart = System.currentTimeMillis(); - try { - while(true){ - while(aux.getTurn()!= id){ - aux.waitSat(); - } - if(aux.getIt() == 1){ - enMax = task.getEnergyCapacity(); - sMax = task.getStorageCapacity(); - getVotes(); - auxTable = idTable.clone(); - } else { - idTable = task.getVotesAgain(id);//per alguna raó després de la primera iteració - //els vots dels satelits es barrejen, aleshores torno - //a copiar-los. - } - if(aux.breakThread){ - aux.incTurn(); - aux.notifySat(); - break; - } - if(id == 1){ - checkVotes(); - if(finalVotes){ - aux.breakThread = true; - aux.incTurn(); - aux.notifySat(); - break; - } - System.out.println("ITERATION "+(aux.getIt())); - } - //vote and give table to the next satellite - vote(); - nextSat.giveTable(comTable); - //change turn - if(id != nsat){ - aux.incTurn(); - aux.notifySat(); - } else { - nTasksAssig = 0; - for(int i=0; i otro voto, modificar tabla - sino, pasar a votar por tu siguiente mejor tarea - si no puedes votar por ninguna tarea, indicar que tu voto es definitivo: votoDefinitivo[id] = true; - DUDA: quién comprueba si todos los votos son definitivos (el primer satelite o el actual?) - si no son definitivos: pasar la tabla al siguiente - si son definitivos: break - 4)pasar la tabla al siguiente - - * Si no es primera vuelta: - 1)comprobar si tu voto ha sido cambiado - si no hay cambio: votoDefinitivo[id] = true; - si hay cambio: votar por tu siguiente mejor tarea - si no puedes votar, votoDefinitivo[id] = true - DUDA: quién comprueba si todos los votos son definitivos (el primer satelite o el actual?) - si no son definitivos: pasar la tabla al siguiente - si son definitivos: break - 2)pasar la tabla al siguiente - -*/ \ No newline at end of file diff --git a/src/Other/Scheduler/Ring-based-v1/Task.java b/src/Other/Scheduler/Ring-based-v1/Task.java deleted file mode 100644 index 2e3cc70..0000000 --- a/src/Other/Scheduler/Ring-based-v1/Task.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - -package ring.based.v1; - -import java.util.Random; - -/** - * - * @author SebAs - * Version 1 - */ -public final class Task { - private int[] taskStorage, taskEnergy; - private int ntask; - private double votes[], satVotes[][]; - - public Task(int ntask, int nsat){ - this.ntask = ntask; - taskStorage = new int[ntask]; - taskEnergy = new int[ntask]; - votes = new double[ntask]; - satVotes = new double[nsat][ntask]; - } - - public void createTaskInfo(){ - for(int i=0; i sMax || taskEnergy[i] > enMax){ - votes[i] = 0.0; - } - else{ - votes[i] = (taskStorage[i]/sMax + taskEnergy[i]/enMax)/2; - } - } - for(i=0;i aEnd) { - throw new IllegalArgumentException("Start cannot exceed End."); - } - long range = (long)aEnd - (long)aStart + 1; - long fraction = (long)(range * aRandom.nextDouble()); - int randomNumber = (int)(fraction + aStart); - return randomNumber; - } -} diff --git a/src/Other/Scheduler/Ring-based-v1/ringBasedV1.java b/src/Other/Scheduler/Ring-based-v1/ringBasedV1.java deleted file mode 100644 index 13a0578..0000000 --- a/src/Other/Scheduler/Ring-based-v1/ringBasedV1.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - -package ring.based.v1; - -/** - * - * @author SebAs - * Version 1 - */ -public class ringBasedV1 { - - /** - * @param args the command line arguments - * @throws java.lang.InterruptedException - */ - public static void main(String[] args) throws InterruptedException{ - int ntask = 500; - int nsat = 500; - Task task = new Task(ntask,nsat); - task.createTaskInfo(); - AuxClass aux = new AuxClass(nsat); - Thread[] t = new Thread[nsat]; - Satellite[] sat = new Satellite[nsat]; - //create satellites - for(int j=0; j changes; - private ArrayList numChanges; - private ArrayList tasksAssigned; - private boolean noChanges; - - public Satellite(int id,int ntask, int nsat, AuxClass aux, Task task){ - this.id = id; - this.ntask = ntask; - this.nsat = nsat; - this.aux = aux; - this.task = task; - idTable = new double[ntask]; - auxTable = new double[ntask]; - comTable = new double[ntask][2]; - noChanges = false; - change = new Double[3]; - changes = new ArrayList(); - numChanges = new ArrayList(); - tasksAssigned = new ArrayList(); - } - - public void getVotes(){ - idTable = task.getVotes(enMax,sMax,id); - } - - public void passChanges(ArrayList previousChanges){ - int i, idx; - Double tmp[]; - i = 0; - if(!previousChanges.isEmpty()){ - while(i max){ - max = auxTable[k]; - index = k; - } - } - //put the best possible vote - while(tmp < ntask){ - if(comTable[index][0] < max){ - comTable[index][0] = max; - comTable[index][1] = id; - change[0] = (double) index+1; - change[1] = max; - change[2] = (double) id; - changes.add(change); - break; - }else if((comTable[index][0] == max) && (comTable[index][1] == id)){ - break; - } - else{ - auxTable[index] = 0; - max=0; - for (int j=0; j max){ - max = auxTable[j]; - index = j; - } - } - if(max == 0) break; - } - tmp++; - } - - } - - public void setNeighbour(Satellite nextSat){ - this.nextSat = nextSat; - } - - public void checkChanges(){ - if(changes.isEmpty()){ - noChanges = true; - } - } - - public void removeNextSatChanges(){ - int i; - Double tmp[]; - i = 0; - if(!changes.isEmpty()){ - while(i1){//satellite 1 checks changes minimum after the first iteration - checkChanges(); - if(noChanges){ - aux.setBreakThread(true); - aux.incTurn(); - aux.notifySat(); - break; - } - System.out.println("ITERATION "+(aux.getIt())); - } - vote(); - removeNextSatChanges(); - nextSat.passChanges(changes); - if(id != nsat){//change turn - aux.incTurn(); - aux.notifySat(); - } else { - nTasksAssig = 0; - for(int i=0; i sMax || taskEnergy[i] > enMax){ - votes[i] = 0.0; - } - else{ - votes[i] = (taskStorage[i]/sMax + taskEnergy[i]/enMax)/2; - } - } - for(i=0;i aEnd) { - throw new IllegalArgumentException("Start cannot exceed End."); - } - long range = (long)aEnd - (long)aStart + 1; - long fraction = (long)(range * aRandom.nextDouble()); - int randomNumber = (int)(fraction + aStart); - return randomNumber; - } -} diff --git a/src/Other/Scheduler/Ring-based-v2/ringBasedV2.java b/src/Other/Scheduler/Ring-based-v2/ringBasedV2.java deleted file mode 100644 index 136905b..0000000 --- a/src/Other/Scheduler/Ring-based-v2/ringBasedV2.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - -package ringbased; - -/** - * - * @author Sebas - * Version 2 - */ -public class ringBasedV2 { - - /** - * @param args the command line arguments - * @throws java.lang.InterruptedException - */ - public static void main(String[] args) throws InterruptedException{ - int ntask = Integer.parseInt(args[1]); - int nsat = Integer.parseInt(args[0]); - Task task = new Task(ntask,nsat); - task.createTaskInfo(); - AuxClass aux = new AuxClass(nsat); - Thread[] t = new Thread[nsat]; - Satellite[] sat = new Satellite[nsat]; - //create satellites - for(int j=0; j changes; - private ArrayList tasksAssigned,numChanges,totalFreeStorage, totalFreeEnergy; - private boolean noChanges, tasksVoted[], taskAlreadyVoted[]; - - public Satellite(int id, int ntask, int nsat, AuxClass aux, Task task) { - this.id = id; - this.ntask = ntask; - this.nsat = nsat; - this.aux = aux; - this.task = task; - idTable = new double[ntask]; - auxTable = new double[ntask]; - comTable = new double[ntask][2]; - orderedVotes = new double[ntask][2]; - tasksVoted = new boolean[ntask]; - noChanges = false; - change = new Double[3]; - auxChange = new Double[3]; - changes = new ArrayList(); - tasksAssigned = new ArrayList(); - numChanges = new ArrayList(); - totalFreeStorage = new ArrayList(); - totalFreeEnergy = new ArrayList(); - taskAlreadyVoted = new boolean[ntask]; - } - - public void getVotes() { - idTable = task.getVotes(enMax,sMax,id); - } - - public void passChanges(ArrayList previousChanges) { - int i, idx; - Double tmp[]; - i = 0; - if (!previousChanges.isEmpty()) { - while (i < previousChanges.size()) { - tmp = previousChanges.get(i); - idx = (int) tmp[0].longValue(); - comTable[idx - 1][0] = tmp[1]; - comTable[idx - 1][1] = tmp[2]; - i++; - } - } - changes = previousChanges; - } - - public void orderVotes(){ - int k = 0; - double max = 0; - int index = 0; - while(k < ntask){ - for (int i = 0; i < ntask; i++) { - if (auxTable[i] > max) { - max = auxTable[i]; - index = i; - } - } - orderedVotes[k][0] = max; - orderedVotes[k][1] = index+1;//index + 1 = task number - auxTable[index] = 0; - max = 0; - k++; - } - } - - public void vote() { - int index; - int j,k; - if(aux.getIt() == 1){ - orderVotes(); - } - /*put the best possible votes*/ - k = 0; - while (k < ntask) { - if(k == 0 && aux.getIt() > 1) {//restore capacity if other satellite assigned - j = 0; //itself a task that was voted previously by this satellite - while(j < ntask){ - index = (int) orderedVotes[j][1] - 1; - if(tasksVoted[index] && (comTable[index][0] > orderedVotes[j][0])){ - sMax = sMax + task.storage(index); - enMax = enMax + task.energy(index); - tasksVoted[index] = false; - } - j++; - } - } - index = (int) orderedVotes[k][1] - 1; - if(orderedVotes[k][0] == 0){ - break; - } - else if((comTable[index][0] < orderedVotes[k][0]) && (task.storage(index) <= sMax && task.energy(index) <= enMax)){ - comTable[index][0] = orderedVotes[k][0]; - comTable[index][1] = id; - auxChange[0] = (double) (index + 1); - auxChange[1] = orderedVotes[k][0]; - auxChange[2] = (double) id; - change = auxChange.clone(); - changes.add(change); - tasksVoted[index] = true; - sMax = sMax - task.storage(index); - enMax = enMax - task.energy(index); - } - k++; - } - } - - public void setNeighbour(Satellite nextSat) { - this.nextSat = nextSat; - } - - public void checkChanges() { - if (changes.isEmpty()){ - noChanges = true; - } - } - - public void removeNextSatChanges() { - int i = 0; - Double tmp[]; - if (!changes.isEmpty()) { - while (i < changes.size()) { - tmp = changes.get(i); - if (tmp[2] == 1 && id == nsat) { - changes.remove(i); - i--; - } else if (tmp[2] == id + 1) { - changes.remove(i); - i--; - } - i++; - } - } - } - - @Override - public void run() { - Tstart = System.currentTimeMillis(); - try { - while (true) { - while (aux.getTurn() != id) { - aux.waitSat(); - } - if (aux.breakThread()) { //Thread breaks if breakThread = true - aux.incTurn(); - aux.notifySat(); - break; - } - if (aux.getIt() == 1) {//get storage and energy capacity and get votes - enMax = task.getEnergyCapacity(); - sMax = task.getStorageCapacity(); - aux.passCapacity(id, sMax, enMax); - getVotes(); - auxTable = idTable.clone(); - } else { - idTable = task.getVotesAgain(id);//per alguna raó després de la primera iteració - //els vots dels satelits es barrejen, aleshores torno - //a copiar-los. - } - if (id == 1 && aux.getIt()>1) {//satellite 1 checks changes - checkChanges(); - if (noChanges) { - aux.setBreakThread(true); - aux.incTurn(); - aux.notifySat(); - break; - } - } - vote();//vote and pass changes to the next satellite - removeNextSatChanges(); - nextSat.passChanges(changes); - if (id != nsat){//change turn - aux.incTurn(); - aux.notifySat(); - } else { - if(aux.getIt() == 1){ - eMaxTotal = aux.getMaxCapacities()[0]; - sMaxTotal = aux.getMaxCapacities()[1]; - totalFreeEnergy.add(eMaxTotal); - totalFreeStorage.add(sMaxTotal); - } - nTasksAssig = 0; - for(int i=0; i sMax || taskEnergy[i] > enMax){ - votes[i] = 0; - } - else{ - votes[i] = (taskStorage[i]/sMax + taskEnergy[i]/enMax)/2; - } - } - for(i=0;i aEnd) { - throw new IllegalArgumentException("Start cannot exceed End."); - } - long range = (long)aEnd - (long)aStart + 1; - long fraction = (long)(range * aRandom.nextDouble()); - int randomNumber = (int)(fraction + aStart); - return randomNumber; - } - - - -} diff --git a/src/Other/Scheduler/Ring-based-v3/ringBasedV3.java b/src/Other/Scheduler/Ring-based-v3/ringBasedV3.java deleted file mode 100644 index 82d429a..0000000 --- a/src/Other/Scheduler/Ring-based-v3/ringBasedV3.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - -package ring.based.v3; - -/** - * - * @author Sebas - * Version 3 - */ -public class ringBasedV3 { - /** - * @param args the command line arguments - * @throws java.lang.InterruptedException - */ - public static void main(String[] args) throws InterruptedException { - int ntask = Integer.parseInt(args[1]); - int nsat = Integer.parseInt(args[0]); - Task task = new Task(ntask,nsat); - task.createTaskInfo();//pasar datos de args (taskStorage y taskEnergy) - AuxClass aux = new AuxClass(nsat); - Thread[] t = new Thread[nsat]; - Satellite[] sat = new Satellite[nsat]; - //create satellites - for(int j=0; j Date: Fri, 17 Jun 2016 13:52:45 +0200 Subject: [PATCH 3/4] Task Scheduler --- .../Ring-based/Version 1/AuxClass.java | 78 +++++ .../Ring-based/Version 1/Satellite.java | 210 +++++++++++++ .../Scheduler/Ring-based/Version 1/Task.java | 91 ++++++ .../Ring-based/Version 1/ringBasedV1.java | 49 ++++ .../Ring-based/Version 1/ringBasedV1Test.java | 68 +++++ .../Ring-based/Version 2/AuxClass.java | 67 +++++ .../Ring-based/Version 2/Satellite.java | 234 +++++++++++++++ .../Scheduler/Ring-based/Version 2/Task.java | 91 ++++++ .../Ring-based/Version 2/ringBasedV2.java | 49 ++++ .../Ring-based/Version 2/ringBasedV2Test.java | 71 +++++ .../Ring-based/Version 3/AuxClass.java | 87 ++++++ .../Ring-based/Version 3/Satellite.java | 275 ++++++++++++++++++ .../Scheduler/Ring-based/Version 3/Task.java | 104 +++++++ .../Ring-based/Version 3/ringBasedV3.java | 49 ++++ .../Ring-based/Version 3/ringBasedV3Test.java | 79 +++++ 15 files changed, 1602 insertions(+) create mode 100644 src/Architecture/Scheduler/Ring-based/Version 1/AuxClass.java create mode 100644 src/Architecture/Scheduler/Ring-based/Version 1/Satellite.java create mode 100644 src/Architecture/Scheduler/Ring-based/Version 1/Task.java create mode 100644 src/Architecture/Scheduler/Ring-based/Version 1/ringBasedV1.java create mode 100644 src/Architecture/Scheduler/Ring-based/Version 1/ringBasedV1Test.java create mode 100644 src/Architecture/Scheduler/Ring-based/Version 2/AuxClass.java create mode 100644 src/Architecture/Scheduler/Ring-based/Version 2/Satellite.java create mode 100644 src/Architecture/Scheduler/Ring-based/Version 2/Task.java create mode 100644 src/Architecture/Scheduler/Ring-based/Version 2/ringBasedV2.java create mode 100644 src/Architecture/Scheduler/Ring-based/Version 2/ringBasedV2Test.java create mode 100644 src/Architecture/Scheduler/Ring-based/Version 3/AuxClass.java create mode 100644 src/Architecture/Scheduler/Ring-based/Version 3/Satellite.java create mode 100644 src/Architecture/Scheduler/Ring-based/Version 3/Task.java create mode 100644 src/Architecture/Scheduler/Ring-based/Version 3/ringBasedV3.java create mode 100644 src/Architecture/Scheduler/Ring-based/Version 3/ringBasedV3Test.java diff --git a/src/Architecture/Scheduler/Ring-based/Version 1/AuxClass.java b/src/Architecture/Scheduler/Ring-based/Version 1/AuxClass.java new file mode 100644 index 0000000..79dcb60 --- /dev/null +++ b/src/Architecture/Scheduler/Ring-based/Version 1/AuxClass.java @@ -0,0 +1,78 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package ring.based.v1; + +/** + * + * @author SebAs + * Version 1 + */ +public class AuxClass{ + private int it,turn,nsat; + private boolean[] changed; + public boolean breakThread; + + public AuxClass(int nsat){ + this.nsat = nsat; + it = 1; + turn = 1; + changed = new boolean[nsat]; + for(int i=0; i tasksAssigned; + private boolean finalVotes; + + public Satellite(int id, int ntask, int nsat, AuxClass aux, Task task){ + this.id = id; + this.ntask = ntask; + this.nsat = nsat; + idTable = new double[ntask]; + auxTable = new double[ntask]; + comTable = new double[ntask][2]; + this.task = task; + this.aux = aux; + finalVotes = false; + tasksAssigned = new ArrayList(); + } + + public void setNeighbour(Satellite nextSat){ + this.nextSat = nextSat; + } + + public void getVotes(){ + idTable = task.getVotes(enMax,sMax,id); + } + + public void checkVotes(){ + if(!aux.votesChanged()){ + finalVotes = true; + } + } + + public void vote(){ + int index = 0; + int tmp = 0; + //get the maximum + double max = 0; + for (int k=0; k max){ + max = auxTable[k]; + index = k; + } + } + while(tmp < ntask){ + if(comTable[index][0] < max){ + comTable[index][0] = max; + comTable[index][1] = id; + aux.setChangeTo(true,id); + break; + } else if ((comTable[index][0] == max) && (comTable[index][1] == id)){ + aux.setChangeTo(false,id); + break; + } else { + auxTable[index] = 0; + max=0; + for (int j=0; j max)){ + max = auxTable[j]; + index = j; + } + } + if(max == 0){ + aux.setChangeTo(false,id); + break; + } + } + tmp++; + } + } + + public void giveTable(double[][] t) { + comTable = t.clone(); + } + + @Override + public synchronized void run(){ + Tstart = System.currentTimeMillis(); + try { + while(true){ + while(aux.getTurn()!= id){ + aux.waitSat(); + } + if(aux.getIt() == 1){ + enMax = task.getEnergyCapacity(); + sMax = task.getStorageCapacity(); + getVotes(); + auxTable = idTable.clone(); + } else { + idTable = task.getVotesAgain(id);//per alguna raó després de la primera iteració + //els vots dels satelits es barrejen, aleshores torno + //a copiar-los. + } + if(aux.breakThread){ + aux.incTurn(); + aux.notifySat(); + break; + } + if(id == 1){ + checkVotes(); + if(finalVotes){ + aux.breakThread = true; + aux.incTurn(); + aux.notifySat(); + break; + } + System.out.println("ITERATION "+(aux.getIt())); + } + //vote and give table to the next satellite + vote(); + nextSat.giveTable(comTable); + //change turn + if(id != nsat){ + aux.incTurn(); + aux.notifySat(); + } else { + nTasksAssig = 0; + for(int i=0; i otro voto, modificar tabla + sino, pasar a votar por tu siguiente mejor tarea + si no puedes votar por ninguna tarea, indicar que tu voto es definitivo: votoDefinitivo[id] = true; + DUDA: quién comprueba si todos los votos son definitivos (el primer satelite o el actual?) + si no son definitivos: pasar la tabla al siguiente + si son definitivos: break + 4)pasar la tabla al siguiente + + * Si no es primera vuelta: + 1)comprobar si tu voto ha sido cambiado + si no hay cambio: votoDefinitivo[id] = true; + si hay cambio: votar por tu siguiente mejor tarea + si no puedes votar, votoDefinitivo[id] = true + DUDA: quién comprueba si todos los votos son definitivos (el primer satelite o el actual?) + si no son definitivos: pasar la tabla al siguiente + si son definitivos: break + 2)pasar la tabla al siguiente + +*/ \ No newline at end of file diff --git a/src/Architecture/Scheduler/Ring-based/Version 1/Task.java b/src/Architecture/Scheduler/Ring-based/Version 1/Task.java new file mode 100644 index 0000000..2e3cc70 --- /dev/null +++ b/src/Architecture/Scheduler/Ring-based/Version 1/Task.java @@ -0,0 +1,91 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package ring.based.v1; + +import java.util.Random; + +/** + * + * @author SebAs + * Version 1 + */ +public final class Task { + private int[] taskStorage, taskEnergy; + private int ntask; + private double votes[], satVotes[][]; + + public Task(int ntask, int nsat){ + this.ntask = ntask; + taskStorage = new int[ntask]; + taskEnergy = new int[ntask]; + votes = new double[ntask]; + satVotes = new double[nsat][ntask]; + } + + public void createTaskInfo(){ + for(int i=0; i sMax || taskEnergy[i] > enMax){ + votes[i] = 0.0; + } + else{ + votes[i] = (taskStorage[i]/sMax + taskEnergy[i]/enMax)/2; + } + } + for(i=0;i aEnd) { + throw new IllegalArgumentException("Start cannot exceed End."); + } + long range = (long)aEnd - (long)aStart + 1; + long fraction = (long)(range * aRandom.nextDouble()); + int randomNumber = (int)(fraction + aStart); + return randomNumber; + } +} diff --git a/src/Architecture/Scheduler/Ring-based/Version 1/ringBasedV1.java b/src/Architecture/Scheduler/Ring-based/Version 1/ringBasedV1.java new file mode 100644 index 0000000..13a0578 --- /dev/null +++ b/src/Architecture/Scheduler/Ring-based/Version 1/ringBasedV1.java @@ -0,0 +1,49 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package ring.based.v1; + +/** + * + * @author SebAs + * Version 1 + */ +public class ringBasedV1 { + + /** + * @param args the command line arguments + * @throws java.lang.InterruptedException + */ + public static void main(String[] args) throws InterruptedException{ + int ntask = 500; + int nsat = 500; + Task task = new Task(ntask,nsat); + task.createTaskInfo(); + AuxClass aux = new AuxClass(nsat); + Thread[] t = new Thread[nsat]; + Satellite[] sat = new Satellite[nsat]; + //create satellites + for(int j=0; j changes; + private ArrayList numChanges; + private ArrayList tasksAssigned; + private boolean noChanges; + + public Satellite(int id,int ntask, int nsat, AuxClass aux, Task task){ + this.id = id; + this.ntask = ntask; + this.nsat = nsat; + this.aux = aux; + this.task = task; + idTable = new double[ntask]; + auxTable = new double[ntask]; + comTable = new double[ntask][2]; + noChanges = false; + change = new Double[3]; + changes = new ArrayList(); + numChanges = new ArrayList(); + tasksAssigned = new ArrayList(); + } + + public void getVotes(){ + idTable = task.getVotes(enMax,sMax,id); + } + + public void passChanges(ArrayList previousChanges){ + int i, idx; + Double tmp[]; + i = 0; + if(!previousChanges.isEmpty()){ + while(i max){ + max = auxTable[k]; + index = k; + } + } + //put the best possible vote + while(tmp < ntask){ + if(comTable[index][0] < max){ + comTable[index][0] = max; + comTable[index][1] = id; + change[0] = (double) index+1; + change[1] = max; + change[2] = (double) id; + changes.add(change); + break; + }else if((comTable[index][0] == max) && (comTable[index][1] == id)){ + break; + } + else{ + auxTable[index] = 0; + max=0; + for (int j=0; j max){ + max = auxTable[j]; + index = j; + } + } + if(max == 0) break; + } + tmp++; + } + + } + + public void setNeighbour(Satellite nextSat){ + this.nextSat = nextSat; + } + + public void checkChanges(){ + if(changes.isEmpty()){ + noChanges = true; + } + } + + public void removeNextSatChanges(){ + int i; + Double tmp[]; + i = 0; + if(!changes.isEmpty()){ + while(i1){//satellite 1 checks changes minimum after the first iteration + checkChanges(); + if(noChanges){ + aux.setBreakThread(true); + aux.incTurn(); + aux.notifySat(); + break; + } + System.out.println("ITERATION "+(aux.getIt())); + } + vote(); + removeNextSatChanges(); + nextSat.passChanges(changes); + if(id != nsat){//change turn + aux.incTurn(); + aux.notifySat(); + } else { + nTasksAssig = 0; + for(int i=0; i sMax || taskEnergy[i] > enMax){ + votes[i] = 0.0; + } + else{ + votes[i] = (taskStorage[i]/sMax + taskEnergy[i]/enMax)/2; + } + } + for(i=0;i aEnd) { + throw new IllegalArgumentException("Start cannot exceed End."); + } + long range = (long)aEnd - (long)aStart + 1; + long fraction = (long)(range * aRandom.nextDouble()); + int randomNumber = (int)(fraction + aStart); + return randomNumber; + } +} diff --git a/src/Architecture/Scheduler/Ring-based/Version 2/ringBasedV2.java b/src/Architecture/Scheduler/Ring-based/Version 2/ringBasedV2.java new file mode 100644 index 0000000..136905b --- /dev/null +++ b/src/Architecture/Scheduler/Ring-based/Version 2/ringBasedV2.java @@ -0,0 +1,49 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package ringbased; + +/** + * + * @author Sebas + * Version 2 + */ +public class ringBasedV2 { + + /** + * @param args the command line arguments + * @throws java.lang.InterruptedException + */ + public static void main(String[] args) throws InterruptedException{ + int ntask = Integer.parseInt(args[1]); + int nsat = Integer.parseInt(args[0]); + Task task = new Task(ntask,nsat); + task.createTaskInfo(); + AuxClass aux = new AuxClass(nsat); + Thread[] t = new Thread[nsat]; + Satellite[] sat = new Satellite[nsat]; + //create satellites + for(int j=0; j changes; + private ArrayList tasksAssigned,numChanges,totalFreeStorage, totalFreeEnergy; + private boolean noChanges, tasksVoted[], taskAlreadyVoted[]; + + public Satellite(int id, int ntask, int nsat, AuxClass aux, Task task) { + this.id = id; + this.ntask = ntask; + this.nsat = nsat; + this.aux = aux; + this.task = task; + idTable = new double[ntask]; + auxTable = new double[ntask]; + comTable = new double[ntask][2]; + orderedVotes = new double[ntask][2]; + tasksVoted = new boolean[ntask]; + noChanges = false; + change = new Double[3]; + auxChange = new Double[3]; + changes = new ArrayList(); + tasksAssigned = new ArrayList(); + numChanges = new ArrayList(); + totalFreeStorage = new ArrayList(); + totalFreeEnergy = new ArrayList(); + taskAlreadyVoted = new boolean[ntask]; + } + + public void getVotes() { + idTable = task.getVotes(enMax,sMax,id); + } + + public void passChanges(ArrayList previousChanges) { + int i, idx; + Double tmp[]; + i = 0; + if (!previousChanges.isEmpty()) { + while (i < previousChanges.size()) { + tmp = previousChanges.get(i); + idx = (int) tmp[0].longValue(); + comTable[idx - 1][0] = tmp[1]; + comTable[idx - 1][1] = tmp[2]; + i++; + } + } + changes = previousChanges; + } + + public void orderVotes(){ + int k = 0; + double max = 0; + int index = 0; + while(k < ntask){ + for (int i = 0; i < ntask; i++) { + if (auxTable[i] > max) { + max = auxTable[i]; + index = i; + } + } + orderedVotes[k][0] = max; + orderedVotes[k][1] = index+1;//index + 1 = task number + auxTable[index] = 0; + max = 0; + k++; + } + } + + public void vote() { + int index; + int j,k; + if(aux.getIt() == 1){ + orderVotes(); + } + /*put the best possible votes*/ + k = 0; + while (k < ntask) { + if(k == 0 && aux.getIt() > 1) {//restore capacity if other satellite assigned + j = 0; //itself a task that was voted previously by this satellite + while(j < ntask){ + index = (int) orderedVotes[j][1] - 1; + if(tasksVoted[index] && (comTable[index][0] > orderedVotes[j][0])){ + sMax = sMax + task.storage(index); + enMax = enMax + task.energy(index); + tasksVoted[index] = false; + } + j++; + } + } + index = (int) orderedVotes[k][1] - 1; + if(orderedVotes[k][0] == 0){ + break; + } + else if((comTable[index][0] < orderedVotes[k][0]) && (task.storage(index) <= sMax && task.energy(index) <= enMax)){ + comTable[index][0] = orderedVotes[k][0]; + comTable[index][1] = id; + auxChange[0] = (double) (index + 1); + auxChange[1] = orderedVotes[k][0]; + auxChange[2] = (double) id; + change = auxChange.clone(); + changes.add(change); + tasksVoted[index] = true; + sMax = sMax - task.storage(index); + enMax = enMax - task.energy(index); + } + k++; + } + } + + public void setNeighbour(Satellite nextSat) { + this.nextSat = nextSat; + } + + public void checkChanges() { + if (changes.isEmpty()){ + noChanges = true; + } + } + + public void removeNextSatChanges() { + int i = 0; + Double tmp[]; + if (!changes.isEmpty()) { + while (i < changes.size()) { + tmp = changes.get(i); + if (tmp[2] == 1 && id == nsat) { + changes.remove(i); + i--; + } else if (tmp[2] == id + 1) { + changes.remove(i); + i--; + } + i++; + } + } + } + + @Override + public void run() { + Tstart = System.currentTimeMillis(); + try { + while (true) { + while (aux.getTurn() != id) { + aux.waitSat(); + } + if (aux.breakThread()) { //Thread breaks if breakThread = true + aux.incTurn(); + aux.notifySat(); + break; + } + if (aux.getIt() == 1) {//get storage and energy capacity and get votes + enMax = task.getEnergyCapacity(); + sMax = task.getStorageCapacity(); + aux.passCapacity(id, sMax, enMax); + getVotes(); + auxTable = idTable.clone(); + } else { + idTable = task.getVotesAgain(id);//per alguna raó després de la primera iteració + //els vots dels satelits es barrejen, aleshores torno + //a copiar-los. + } + if (id == 1 && aux.getIt()>1) {//satellite 1 checks changes + checkChanges(); + if (noChanges) { + aux.setBreakThread(true); + aux.incTurn(); + aux.notifySat(); + break; + } + } + vote();//vote and pass changes to the next satellite + removeNextSatChanges(); + nextSat.passChanges(changes); + if (id != nsat){//change turn + aux.incTurn(); + aux.notifySat(); + } else { + if(aux.getIt() == 1){ + eMaxTotal = aux.getMaxCapacities()[0]; + sMaxTotal = aux.getMaxCapacities()[1]; + totalFreeEnergy.add(eMaxTotal); + totalFreeStorage.add(sMaxTotal); + } + nTasksAssig = 0; + for(int i=0; i sMax || taskEnergy[i] > enMax){ + votes[i] = 0; + } + else{ + votes[i] = (taskStorage[i]/sMax + taskEnergy[i]/enMax)/2; + } + } + for(i=0;i aEnd) { + throw new IllegalArgumentException("Start cannot exceed End."); + } + long range = (long)aEnd - (long)aStart + 1; + long fraction = (long)(range * aRandom.nextDouble()); + int randomNumber = (int)(fraction + aStart); + return randomNumber; + } + + + +} diff --git a/src/Architecture/Scheduler/Ring-based/Version 3/ringBasedV3.java b/src/Architecture/Scheduler/Ring-based/Version 3/ringBasedV3.java new file mode 100644 index 0000000..82d429a --- /dev/null +++ b/src/Architecture/Scheduler/Ring-based/Version 3/ringBasedV3.java @@ -0,0 +1,49 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package ring.based.v3; + +/** + * + * @author Sebas + * Version 3 + */ +public class ringBasedV3 { + /** + * @param args the command line arguments + * @throws java.lang.InterruptedException + */ + public static void main(String[] args) throws InterruptedException { + int ntask = Integer.parseInt(args[1]); + int nsat = Integer.parseInt(args[0]); + Task task = new Task(ntask,nsat); + task.createTaskInfo();//pasar datos de args (taskStorage y taskEnergy) + AuxClass aux = new AuxClass(nsat); + Thread[] t = new Thread[nsat]; + Satellite[] sat = new Satellite[nsat]; + //create satellites + for(int j=0; j Date: Fri, 17 Jun 2016 14:08:53 +0200 Subject: [PATCH 4/4] Scheduler --- .../Scheduler/Ring-based/Version 1/AuxClass.java | 0 .../Scheduler/Ring-based/Version 1/Satellite.java | 0 .../Scheduler/Ring-based/Version 1/Task.java | 0 .../Scheduler/Ring-based/Version 1/ringBasedV1.java | 0 .../Scheduler/Ring-based/Version 1/ringBasedV1Test.java | 0 .../Scheduler/Ring-based/Version 2/AuxClass.java | 0 .../Scheduler/Ring-based/Version 2/Satellite.java | 0 .../Scheduler/Ring-based/Version 2/Task.java | 0 .../Scheduler/Ring-based/Version 2/ringBasedV2.java | 0 .../Scheduler/Ring-based/Version 2/ringBasedV2Test.java | 0 .../Scheduler/Ring-based/Version 3/AuxClass.java | 0 .../Scheduler/Ring-based/Version 3/Satellite.java | 0 .../Scheduler/Ring-based/Version 3/Task.java | 0 .../Scheduler/Ring-based/Version 3/ringBasedV3.java | 0 .../Scheduler/Ring-based/Version 3/ringBasedV3Test.java | 0 15 files changed, 0 insertions(+), 0 deletions(-) rename src/{Architecture => Other}/Scheduler/Ring-based/Version 1/AuxClass.java (100%) rename src/{Architecture => Other}/Scheduler/Ring-based/Version 1/Satellite.java (100%) rename src/{Architecture => Other}/Scheduler/Ring-based/Version 1/Task.java (100%) rename src/{Architecture => Other}/Scheduler/Ring-based/Version 1/ringBasedV1.java (100%) rename src/{Architecture => Other}/Scheduler/Ring-based/Version 1/ringBasedV1Test.java (100%) rename src/{Architecture => Other}/Scheduler/Ring-based/Version 2/AuxClass.java (100%) rename src/{Architecture => Other}/Scheduler/Ring-based/Version 2/Satellite.java (100%) rename src/{Architecture => Other}/Scheduler/Ring-based/Version 2/Task.java (100%) rename src/{Architecture => Other}/Scheduler/Ring-based/Version 2/ringBasedV2.java (100%) rename src/{Architecture => Other}/Scheduler/Ring-based/Version 2/ringBasedV2Test.java (100%) rename src/{Architecture => Other}/Scheduler/Ring-based/Version 3/AuxClass.java (100%) rename src/{Architecture => Other}/Scheduler/Ring-based/Version 3/Satellite.java (100%) rename src/{Architecture => Other}/Scheduler/Ring-based/Version 3/Task.java (100%) rename src/{Architecture => Other}/Scheduler/Ring-based/Version 3/ringBasedV3.java (100%) rename src/{Architecture => Other}/Scheduler/Ring-based/Version 3/ringBasedV3Test.java (100%) diff --git a/src/Architecture/Scheduler/Ring-based/Version 1/AuxClass.java b/src/Other/Scheduler/Ring-based/Version 1/AuxClass.java similarity index 100% rename from src/Architecture/Scheduler/Ring-based/Version 1/AuxClass.java rename to src/Other/Scheduler/Ring-based/Version 1/AuxClass.java diff --git a/src/Architecture/Scheduler/Ring-based/Version 1/Satellite.java b/src/Other/Scheduler/Ring-based/Version 1/Satellite.java similarity index 100% rename from src/Architecture/Scheduler/Ring-based/Version 1/Satellite.java rename to src/Other/Scheduler/Ring-based/Version 1/Satellite.java diff --git a/src/Architecture/Scheduler/Ring-based/Version 1/Task.java b/src/Other/Scheduler/Ring-based/Version 1/Task.java similarity index 100% rename from src/Architecture/Scheduler/Ring-based/Version 1/Task.java rename to src/Other/Scheduler/Ring-based/Version 1/Task.java diff --git a/src/Architecture/Scheduler/Ring-based/Version 1/ringBasedV1.java b/src/Other/Scheduler/Ring-based/Version 1/ringBasedV1.java similarity index 100% rename from src/Architecture/Scheduler/Ring-based/Version 1/ringBasedV1.java rename to src/Other/Scheduler/Ring-based/Version 1/ringBasedV1.java diff --git a/src/Architecture/Scheduler/Ring-based/Version 1/ringBasedV1Test.java b/src/Other/Scheduler/Ring-based/Version 1/ringBasedV1Test.java similarity index 100% rename from src/Architecture/Scheduler/Ring-based/Version 1/ringBasedV1Test.java rename to src/Other/Scheduler/Ring-based/Version 1/ringBasedV1Test.java diff --git a/src/Architecture/Scheduler/Ring-based/Version 2/AuxClass.java b/src/Other/Scheduler/Ring-based/Version 2/AuxClass.java similarity index 100% rename from src/Architecture/Scheduler/Ring-based/Version 2/AuxClass.java rename to src/Other/Scheduler/Ring-based/Version 2/AuxClass.java diff --git a/src/Architecture/Scheduler/Ring-based/Version 2/Satellite.java b/src/Other/Scheduler/Ring-based/Version 2/Satellite.java similarity index 100% rename from src/Architecture/Scheduler/Ring-based/Version 2/Satellite.java rename to src/Other/Scheduler/Ring-based/Version 2/Satellite.java diff --git a/src/Architecture/Scheduler/Ring-based/Version 2/Task.java b/src/Other/Scheduler/Ring-based/Version 2/Task.java similarity index 100% rename from src/Architecture/Scheduler/Ring-based/Version 2/Task.java rename to src/Other/Scheduler/Ring-based/Version 2/Task.java diff --git a/src/Architecture/Scheduler/Ring-based/Version 2/ringBasedV2.java b/src/Other/Scheduler/Ring-based/Version 2/ringBasedV2.java similarity index 100% rename from src/Architecture/Scheduler/Ring-based/Version 2/ringBasedV2.java rename to src/Other/Scheduler/Ring-based/Version 2/ringBasedV2.java diff --git a/src/Architecture/Scheduler/Ring-based/Version 2/ringBasedV2Test.java b/src/Other/Scheduler/Ring-based/Version 2/ringBasedV2Test.java similarity index 100% rename from src/Architecture/Scheduler/Ring-based/Version 2/ringBasedV2Test.java rename to src/Other/Scheduler/Ring-based/Version 2/ringBasedV2Test.java diff --git a/src/Architecture/Scheduler/Ring-based/Version 3/AuxClass.java b/src/Other/Scheduler/Ring-based/Version 3/AuxClass.java similarity index 100% rename from src/Architecture/Scheduler/Ring-based/Version 3/AuxClass.java rename to src/Other/Scheduler/Ring-based/Version 3/AuxClass.java diff --git a/src/Architecture/Scheduler/Ring-based/Version 3/Satellite.java b/src/Other/Scheduler/Ring-based/Version 3/Satellite.java similarity index 100% rename from src/Architecture/Scheduler/Ring-based/Version 3/Satellite.java rename to src/Other/Scheduler/Ring-based/Version 3/Satellite.java diff --git a/src/Architecture/Scheduler/Ring-based/Version 3/Task.java b/src/Other/Scheduler/Ring-based/Version 3/Task.java similarity index 100% rename from src/Architecture/Scheduler/Ring-based/Version 3/Task.java rename to src/Other/Scheduler/Ring-based/Version 3/Task.java diff --git a/src/Architecture/Scheduler/Ring-based/Version 3/ringBasedV3.java b/src/Other/Scheduler/Ring-based/Version 3/ringBasedV3.java similarity index 100% rename from src/Architecture/Scheduler/Ring-based/Version 3/ringBasedV3.java rename to src/Other/Scheduler/Ring-based/Version 3/ringBasedV3.java diff --git a/src/Architecture/Scheduler/Ring-based/Version 3/ringBasedV3Test.java b/src/Other/Scheduler/Ring-based/Version 3/ringBasedV3Test.java similarity index 100% rename from src/Architecture/Scheduler/Ring-based/Version 3/ringBasedV3Test.java rename to src/Other/Scheduler/Ring-based/Version 3/ringBasedV3Test.java