diff --git a/out/production/aroTP2/App.class b/out/production/aroTP2/App.class index 95f21adb36e1b15508b3395cdc31c93038417156..ddc92ab5174f71e7f1974260537408a5c4b0286f 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 10a05937e189f4c75a536095efc2063c174e2588..9fbf35526ee85d8b1d2724389cd5a14fa4aa2c22 100644 --- a/src/App.java +++ b/src/App.java @@ -1,3 +1,4 @@ +import java.util.Arrays; import java.util.List; public class App { @@ -5,5 +6,6 @@ public class App { Backpack backpack= new InstanceReader().read("src/sac4"); backpack.solve(); System.out.println("meilleur solution :" + backpack.getBestValue()); + System.out.println("meilleur solution :" + Arrays.toString(backpack.getBestSolution())); } } diff --git a/src/Backpack.java b/src/Backpack.java index dbcf2acd945d86e3af0815823a3ebd970253a4a4..7f57281797ce5511872b4a54a982ddc9ee9c7c70 100644 --- a/src/Backpack.java +++ b/src/Backpack.java @@ -7,16 +7,22 @@ public class Backpack { private int bestValue = 0; private int currentWeight = 0; private int currentValue = 0; + private boolean[] currentSolution; + private boolean[] bestSolution; public Backpack(int capacity, List<Loot> loots) { this.capacity = capacity; this.loots = loots; + this.currentSolution = new boolean[loots.size()]; + this.bestSolution = new boolean[loots.size()]; } public void solve() { this.sortByRatio(); explore_from(0); System.out.println("La valeur optimale est : " + bestValue); + System.out.println("Objets inclus dans la solution optimale : " + bestSolution); + } public String toString(){ @@ -57,6 +63,7 @@ public class Backpack { if (index >= loots.size()) { if (currentValue > bestValue) { bestValue = currentValue; + System.arraycopy(currentSolution, 0, bestSolution, 0, loots.size()); } return; } @@ -75,9 +82,11 @@ public class Backpack { if (currentWeight + currentLoot.getWeight() <= capacity) { currentWeight += currentLoot.getWeight(); currentValue += currentLoot.getValue(); + currentSolution[index] = true; explore_from(index + 1); currentWeight -= currentLoot.getWeight(); currentValue -= currentLoot.getValue(); + currentSolution[index] = false; } // on explore sans l'objet @@ -87,4 +96,8 @@ public class Backpack { public int getBestValue() { return bestValue; } + + public boolean[] getBestSolution() { + return bestSolution; + } }