diff --git a/.idea/misc.xml b/.idea/misc.xml index a346fd7433def0c6493cf110cc4b5a801a98776c..38eefb8d41bfcaa3b3c8cc6ce120644e508e2a6e 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_18" default="true" project-jdk-name="18" project-jdk-type="JavaSDK"> + <component name="ProjectRootManager" version="2" languageLevel="JDK_23" 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 f3dffbcc762d201aabffbb6ae573bd035da8db2d..4ae707af27c19bbfc1bf784eb39242b2b1eb1ffe 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 bcce837946a70025a3a851972fc22b8e964e1cc4..5e8faaeb63c791acfcbc457ec4e45f20eb8b6eee 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 677c206f5b43a39187728543b3333c45abeee666..dcc24c07e6b496357fea863d1d9cdfe408d4b69a 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 919c748040e30676e800217504bec9fc5dadd720..a70ffd2bd6aee3fa0e1b931a9341f905b1d113ed 100644 --- a/src/App.java +++ b/src/App.java @@ -2,7 +2,9 @@ import java.util.List; public class App { public static void main(String[] args) throws Exception { - Backpack backpack= new InstanceReader().read("src/sac0"); - System.out.println(backpack); + Backpack backpack= new InstanceReader().read("src/sacTest"); + backpack.sortByRatio(); + backpack.solutionFractionnelle(); + System.out.println("solution fractionnelle :" + backpack.getSfValuee()); } } diff --git a/src/Backpack.java b/src/Backpack.java index 121d485ed18cc2096aaf45eb08d3c699ff125a14..c1b83218929edff63d23363e88d7a2a4c658ee55 100644 --- a/src/Backpack.java +++ b/src/Backpack.java @@ -1,8 +1,11 @@ +import java.util.Comparator; import java.util.List; public class Backpack { int capacity; List<Loot> loots; + int sfValuee = 0; + int sfWeight = 0; public Backpack(int capacity, List<Loot> loots) { this.capacity = capacity; @@ -12,4 +15,34 @@ public class Backpack { public String toString(){ return capacity +"\n"+loots; } + + /** + Méthode pour trier les objets par ordre décroissant selon leur ratio + */ + public void sortByRatio(){ + loots.sort(Comparator.comparing(Loot::getRatio).reversed()); + } + + public void solutionFractionnelle(){ + this.sortByRatio(); + + for(Loot loot : loots){ + if(sfWeight + loot.getWeight() <= this.capacity){ + // 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; + sfValuee += (int) (loot.getRatio() * remainingWeight); + break; + } + } + } + + public int getSfValuee() { + return sfValuee; + } + } diff --git a/src/InstanceReader.java b/src/InstanceReader.java index 58e01aeeea4ff1d787bd9d70dba63e67f393f3c4..1724721c844fac11e361a9c3a65d0b14dc51da5d 100644 --- a/src/InstanceReader.java +++ b/src/InstanceReader.java @@ -23,5 +23,4 @@ public class InstanceReader { return new Backpack(capacity,loots); } - } diff --git a/src/Loot.java b/src/Loot.java index 46c20c07fc79c7d7b68cd9615108f3d48403fab1..d7dc0320d837cc34d2d2fbda4f41e65e6c8f3e3c 100644 --- a/src/Loot.java +++ b/src/Loot.java @@ -1,12 +1,26 @@ public class Loot { int weight, value; + float ratio; public Loot(int weight, int value) { this.weight = weight; this.value = value; + this.ratio = (float) value/weight; } public String toString(){ - return weight+" "+value; + return weight+" "+value+" "+ratio; + } + + public float getRatio(){ + return ratio; + } + + public int getWeight(){ + return weight; + } + + public int getValue(){ + return value; } } diff --git a/src/sacTest b/src/sacTest new file mode 100644 index 0000000000000000000000000000000000000000..25c99fbdaa22162309a1678f39c96731d7aedfa9 --- /dev/null +++ b/src/sacTest @@ -0,0 +1,4 @@ +15 +5 40 +8 48 +4 36 \ No newline at end of file