diff --git a/.idea/misc.xml b/.idea/misc.xml index 38eefb8d41bfcaa3b3c8cc6ce120644e508e2a6e..e778be18ce10327d4e6028c48a4ca77537a3f1da 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> - <component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="18" project-jdk-type="JavaSDK"> + <component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="18" project-jdk-type="JavaSDK"> <output url="file://$PROJECT_DIR$/out" /> </component> </project> \ No newline at end of file diff --git a/out/production/aroTP2/App.class b/out/production/aroTP2/App.class index 4ae707af27c19bbfc1bf784eb39242b2b1eb1ffe..7e761d15e68921d253e7cdab4896d378c50a9ba7 100644 Binary files a/out/production/aroTP2/App.class and b/out/production/aroTP2/App.class differ diff --git a/out/production/aroTP2/InstanceReader.class b/out/production/aroTP2/InstanceReader.class index 5e8faaeb63c791acfcbc457ec4e45f20eb8b6eee..9a1d5b11a0914dac9a5b1a1a0bdda4d0958bdced 100644 Binary files a/out/production/aroTP2/InstanceReader.class and b/out/production/aroTP2/InstanceReader.class differ diff --git a/out/production/aroTP2/Loot.class b/out/production/aroTP2/Loot.class index dcc24c07e6b496357fea863d1d9cdfe408d4b69a..88c549853e6b352a7f562efc7945c9306b874007 100644 Binary files a/out/production/aroTP2/Loot.class and b/out/production/aroTP2/Loot.class differ diff --git a/src/App.java b/src/App.java index a70ffd2bd6aee3fa0e1b931a9341f905b1d113ed..9d57db8c8b792076fe269164e87d70b7f4c12204 100644 --- a/src/App.java +++ b/src/App.java @@ -2,9 +2,8 @@ import java.util.List; public class App { public static void main(String[] args) throws Exception { - Backpack backpack= new InstanceReader().read("src/sacTest"); - backpack.sortByRatio(); - backpack.solutionFractionnelle(); - System.out.println("solution fractionnelle :" + backpack.getSfValuee()); + Backpack backpack= new InstanceReader().read("src/sac1"); + backpack.solve(); + System.out.println("meilleur solution :" + backpack.getBestValue()); } } diff --git a/src/Backpack.java b/src/Backpack.java index c1b83218929edff63d23363e88d7a2a4c658ee55..92622d5fffea8561e4210c11708a515c0f6ad21d 100644 --- a/src/Backpack.java +++ b/src/Backpack.java @@ -2,16 +2,29 @@ import java.util.Comparator; import java.util.List; public class Backpack { - int capacity; - List<Loot> loots; - int sfValuee = 0; - int sfWeight = 0; + private int capacity; + private List<Loot> loots; + private int sfValuee = 0; + private int sfWeight = 0; + private int bestValue = 0; + private boolean[] bestSolution; + private boolean[] currentSolution; + private int currentWeight = 0; + private int currentValue = 0; public Backpack(int capacity, List<Loot> loots) { this.capacity = capacity; this.loots = loots; } + public void solve() { + bestSolution = new boolean[loots.size()]; + currentSolution = new boolean[loots.size()]; + sortByRatio(); + solutionFractionnelle(); + explore_from(0); + } + public String toString(){ return capacity +"\n"+loots; } @@ -41,8 +54,43 @@ public class Backpack { } } + private void explore_from(int index) { + // Si on a exploré tous les objets + 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) { + return; + } + + // Branche gauche - 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 + explore_from(index + 1); + } + public int getSfValuee() { return sfValuee; } + public int getBestValue() { + return bestValue; + } } diff --git a/src/Loot.java b/src/Loot.java index d7dc0320d837cc34d2d2fbda4f41e65e6c8f3e3c..e0996e4bd577b524a3931ba00adf4b60f725c84e 100644 --- a/src/Loot.java +++ b/src/Loot.java @@ -1,6 +1,6 @@ public class Loot { - int weight, value; - float ratio; + private int weight, value; + private final float ratio; public Loot(int weight, int value) { this.weight = weight;