diff --git a/src/Other/Scheduler/Ring-based/Version 1/AuxClass.java b/src/Other/Scheduler/Ring-based/Version 1/AuxClass.java new file mode 100644 index 0000000..79dcb60 --- /dev/null +++ b/src/Other/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/Other/Scheduler/Ring-based/Version 1/Task.java b/src/Other/Scheduler/Ring-based/Version 1/Task.java new file mode 100644 index 0000000..2e3cc70 --- /dev/null +++ b/src/Other/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/Other/Scheduler/Ring-based/Version 1/ringBasedV1.java b/src/Other/Scheduler/Ring-based/Version 1/ringBasedV1.java new file mode 100644 index 0000000..13a0578 --- /dev/null +++ b/src/Other/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/Other/Scheduler/Ring-based/Version 2/ringBasedV2.java b/src/Other/Scheduler/Ring-based/Version 2/ringBasedV2.java new file mode 100644 index 0000000..136905b --- /dev/null +++ b/src/Other/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/Other/Scheduler/Ring-based/Version 3/ringBasedV3.java b/src/Other/Scheduler/Ring-based/Version 3/ringBasedV3.java new file mode 100644 index 0000000..82d429a --- /dev/null +++ b/src/Other/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