diff --git a/out/production/aroTP2/App.class b/out/production/aroTP2/App.class index 7e761d15e68921d253e7cdab4896d378c50a9ba7..95f21adb36e1b15508b3395cdc31c93038417156 100644 Binary files a/out/production/aroTP2/App.class and b/out/production/aroTP2/App.class differ diff --git a/src/App.java b/src/App.java index 386a74c13680be0258a4391998e615566f98292a..10a05937e189f4c75a536095efc2063c174e2588 100644 --- a/src/App.java +++ b/src/App.java @@ -2,7 +2,7 @@ import java.util.List; public class App { public static void main(String[] args) throws Exception { - Backpack backpack= new InstanceReader().read("src/sac2"); + Backpack backpack= new InstanceReader().read("src/sac4"); backpack.solve(); System.out.println("meilleur solution :" + backpack.getBestValue()); } diff --git a/src/Backpack.java b/src/Backpack.java index f897ab6edd53eed2581c7865e1ca946bd5d8ebaa..dbcf2acd945d86e3af0815823a3ebd970253a4a4 100644 --- a/src/Backpack.java +++ b/src/Backpack.java @@ -2,13 +2,9 @@ import java.util.Comparator; import java.util.List; public class Backpack { - private int capacity; - private List<Loot> loots; - private int sfValuee = 0; - private int sfWeight = 0; + private final int capacity; + private final List<Loot> loots; private int bestValue = 0; - private boolean[] bestSolution; - private boolean[] currentSolution; private int currentWeight = 0; private int currentValue = 0; @@ -18,10 +14,9 @@ public class Backpack { } public void solve() { - bestSolution = new boolean[loots.size()]; - currentSolution = new boolean[loots.size()]; - solutionFractionnelle(); + this.sortByRatio(); explore_from(0); + System.out.println("La valeur optimale est : " + bestValue); } public String toString(){ @@ -35,21 +30,26 @@ public class Backpack { loots.sort(Comparator.comparing(Loot::getRatio).reversed()); } - public void solutionFractionnelle(){ - this.sortByRatio(); - for(Loot loot : loots){ - if(sfWeight + loot.getWeight() <= this.capacity){ + public int solutionFractionnelle(int startIndex, int remainingCapacity) { + int sfValuee = 0; + int sfWeight = 0; + + // On commence à partir de startIndex + for(int i = startIndex; i < loots.size(); i++) { + Loot loot = loots.get(i); + if(sfWeight + loot.getWeight() <= remainingCapacity){ // Si l'objet entier peut entrer dans le sac sfValuee += loot.getValue(); sfWeight += loot.getWeight(); } else { // Si l'objet ne peut pas entrer entièrement dans le sac - int remainingWeight = this.capacity - sfWeight; + int remainingWeight = remainingCapacity - sfWeight; sfValuee += (int) (loot.getRatio() * remainingWeight); break; } } + return sfValuee; } private void explore_from(int index) { @@ -57,37 +57,33 @@ public class Backpack { if (index >= loots.size()) { if (currentValue > bestValue) { bestValue = currentValue; - System.arraycopy(currentSolution, 0, bestSolution, 0, loots.size()); } return; } - // Si la valeur actuelle + la valeur restante possible (sfValuee - valeur déjà utilisée) - // est inférieure à la meilleure solution, on coupe la branche - if (currentValue + (sfValuee - currentValue) <= bestValue) { + // On calcule la borne supérieure pour ce nœud + int remainingCapacity = capacity - currentWeight; + int borneSuperieure = currentValue + solutionFractionnelle(index, remainingCapacity); + + // Si la borne supérieure est inférieure à la meilleure solution + if (borneSuperieure <= bestValue) { return; } - // Branche gauche - avec l'objet + // on essaye d'explorer avec l'objet Loot currentLoot = loots.get(index); if (currentWeight + currentLoot.getWeight() <= capacity) { - currentSolution[index] = true; currentWeight += currentLoot.getWeight(); currentValue += currentLoot.getValue(); explore_from(index + 1); currentWeight -= currentLoot.getWeight(); currentValue -= currentLoot.getValue(); - currentSolution[index] = false; } - // Branche droite - sans l'objet + // on explore sans l'objet explore_from(index + 1); } - public int getSfValuee() { - return sfValuee; - } - public int getBestValue() { return bestValue; }