diff --git a/.idea/misc.xml b/.idea/misc.xml index a346fd7433def0c6493cf110cc4b5a801a98776c..1b2d69351e8b58384566aba7da83f9e6f7d7b030 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_22" default="true" project-jdk-name="22" 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..f385ca79a766fbfe129b15dfa2d50392d0a867a1 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..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 677c206f5b43a39187728543b3333c45abeee666..07f1b7ef8b118f561e0965b6f117e4420a682f7c 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..7aa8157fa22c2a9c95db42062d190c2550098d8d 100644 --- a/src/App.java +++ b/src/App.java @@ -3,6 +3,7 @@ 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.sortByRatio(); + System.out.println(backpack); } } diff --git a/src/Backpack.java b/src/Backpack.java index 121d485ed18cc2096aaf45eb08d3c699ff125a14..10ba315ebebdb4b81946f0390e38cc9ab56a2d60 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,31 @@ 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(loot.getWeight() > this.capacity){ + sfValuee += (loot.getValue() * loot.getWeight()) / (capacity - sfWeight); + sfWeight += capacity; + } + else { + sfValuee += loot.getWeight(); + sfWeight += loot.getWeight(); + } + } + } + + 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..e2a85e00913b0890cf5461e66d69589ac99a4cce 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) weight/value; } 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; } }