From d28d73781cb58a4c3f5dc3482ac77eaaa48134d3 Mon Sep 17 00:00:00 2001 From: aristidecod <sie.ouattara@etu.univ-amu.fr> Date: Wed, 22 Jan 2025 00:43:30 +0100 Subject: [PATCH 1/5] implementation de la fonction explore_from --- .idea/misc.xml | 2 +- out/production/aroTP2/App.class | Bin 1265 -> 1210 bytes out/production/aroTP2/InstanceReader.class | Bin 2335 -> 2335 bytes out/production/aroTP2/Loot.class | Bin 1160 -> 1160 bytes src/App.java | 7 ++- src/Backpack.java | 56 +++++++++++++++++++-- src/Loot.java | 4 +- 7 files changed, 58 insertions(+), 11 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 38eefb8..e778be1 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 GIT binary patch delta 430 zcmey!xr>wQ)W2Q(7#J9w7`!HO`Eqg;7bWW#Cng(CO!g6GEzZv=OXXw`V-RO&kl<mE zWRRNp%2ZW`he4J>j*)>UJ+;IswYVfKF{d<@k%3u5(^H6nfkA<XL6JdevNNLzw+e$Q zJA)bzgF1u8<R(TdMy<(v8J9C!OipET*0g105Xnu=%*jbDEmA1X&nYd*%+FJ>V&rFI zu;*fMU~pt-Z~~d+Joy^aWk$EjtC+P|JQx||Ca-0dt7l|j^U2T8E-heCWYA<_VgLaq z21W)}1~vvp25klg27Lxb21W)323D=@42&BY7#NrsbQl=G(hLmD47v;q3_K7840;Uu z3=9mM3<eB_3`}6ni406&Llt*3ut#oZ;1uH8#=zsFy^TR&6N9MsHii=ET@3OJ4BHr# z5he>Uuz;*%;ALQA5M*FyP+;Hy+ad-riNT1$n1O+TmqCQVgu#@7k-?0Cfq{jAk-?mS onZXk5J}U-m1_lNW21W)u21W)O1{Vef1}+9h23H1m22Tb_00^u=O#lD@ delta 485 zcmdnR`H_?B)W2Q(7#J9w8GI*l`SNfV7bWW#Cnkra7MDy+@UiAD&Mzu)stigj$;{_s z5M&VJVGw6vV`LC5&d(_=$;{7lD@sfT(RrykIjNisk_=Mp4AMLdG7Pd4_nWH7^Drnd zC^9l|r>B+#r-dcvl%}RKGB9gsdI~WxFevjds4%EbmSi;HR%g&)XVBzf&|=V@9KmSC zs5^Nw<8nsp$<|EHdJc>Xk}&HO(va;_uwvwAV{qbPaAt5}XK)1>=Qepc(`81_$<@qS zEZ&R^a+B+r<?0z3*nINyvr7vY6d80Fm>58SiGh)Um4S_ckwK4vfkB^vk%5tcfq_+P zI|JiJ1_lNu27Lwwurvb$GlKyG0|O640fQlf5d#AQCxbDA2?G;Yb0z~5*ihBo4D6BH z890TwwlVPdXm4W>(B8%%x{0AiLVFv7+%5(s28L}6Y6#QC7+678GVn66F$glSGl(*9 zFsLwaf^8Fnn8;wtV8+0}z{?=PV9sE{z{p_9z`(%5z{p_5z|3F+cBd_a9RmXc2LmI6 ZBLgFYJ%c*~0|OTWBZCKn7lRLjBmlJ(PoMw* diff --git a/out/production/aroTP2/InstanceReader.class b/out/production/aroTP2/InstanceReader.class index 5e8faaeb63c791acfcbc457ec4e45f20eb8b6eee..9a1d5b11a0914dac9a5b1a1a0bdda4d0958bdced 100644 GIT binary patch delta 17 ZcmbO)G+&70)W2Q(7#J9wHgbq_0suWG1;zjX delta 17 ZcmbO)G+&70)W2Q(7#J9wH*$z`0suWL1;+pY diff --git a/out/production/aroTP2/Loot.class b/out/production/aroTP2/Loot.class index dcc24c07e6b496357fea863d1d9cdfe408d4b69a..88c549853e6b352a7f562efc7945c9306b874007 100644 GIT binary patch delta 40 scmeC+?BL`$^>5cc1_lPFjU1<#1eqAP8F&~N7?>FN!L-ok$4pBZ0oIHN$p8QV delta 40 pcmeC+?BL`$^>5cc1_lP_jU1<#1Q{5(8F&~#fFD9{e$2F#5dhWn2($nI diff --git a/src/App.java b/src/App.java index a70ffd2..9d57db8 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 c1b8321..92622d5 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 d7dc032..e0996e4 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; -- GitLab From 567d1f6a44c71c28090229a96e7ffc29c26e623a Mon Sep 17 00:00:00 2001 From: Boubacar <gounouseroboubacar@gmail.com> Date: Wed, 22 Jan 2025 01:15:32 +0100 Subject: [PATCH 2/5] =?UTF-8?q?sortByRatio()=20supprim=C3=A9=20dans=20solv?= =?UTF-8?q?e()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Backpack.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Backpack.java b/src/Backpack.java index 92622d5..f897ab6 100644 --- a/src/Backpack.java +++ b/src/Backpack.java @@ -20,7 +20,6 @@ public class Backpack { public void solve() { bestSolution = new boolean[loots.size()]; currentSolution = new boolean[loots.size()]; - sortByRatio(); solutionFractionnelle(); explore_from(0); } @@ -38,7 +37,6 @@ public class Backpack { public void solutionFractionnelle(){ this.sortByRatio(); - for(Loot loot : loots){ if(sfWeight + loot.getWeight() <= this.capacity){ // Si l'objet entier peut entrer dans le sac -- GitLab From 2b35d3538bdcb293e9eb6e11eb698517fd616b6d Mon Sep 17 00:00:00 2001 From: Boubacar <gounouseroboubacar@gmail.com> Date: Wed, 22 Jan 2025 01:15:42 +0100 Subject: [PATCH 3/5] =?UTF-8?q?sortByRatio()=20supprim=C3=A9=20dans=20solv?= =?UTF-8?q?e()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App.java b/src/App.java index 9d57db8..386a74c 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/sac1"); + Backpack backpack= new InstanceReader().read("src/sac2"); backpack.solve(); System.out.println("meilleur solution :" + backpack.getBestValue()); } -- GitLab From 5400343c7c41aa2a8bcaab6d20b0282410b138e1 Mon Sep 17 00:00:00 2001 From: aristidecod <sie.ouattara@etu.univ-amu.fr> Date: Thu, 23 Jan 2025 11:46:53 +0100 Subject: [PATCH 4/5] correction de la methode solutionFractionnelle et explore_from --- out/production/aroTP2/App.class | Bin 1210 -> 1210 bytes src/App.java | 2 +- src/Backpack.java | 48 +++++++++++++++----------------- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/out/production/aroTP2/App.class b/out/production/aroTP2/App.class index 7e761d15e68921d253e7cdab4896d378c50a9ba7..95f21adb36e1b15508b3395cdc31c93038417156 100644 GIT binary patch delta 12 TcmdnRxr=i`Jfq3RgeVpO9eV^C delta 12 TcmdnRxr=i`Jfq>pgeVpO9c%;_ diff --git a/src/App.java b/src/App.java index 386a74c..10a0593 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 f897ab6..dbcf2ac 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; } -- GitLab From f62f4c9882f9dd50535a46b882b89f777177bab2 Mon Sep 17 00:00:00 2001 From: g24020457 <boubacar.gounou@etu.univ-amu.fr> Date: Thu, 23 Jan 2025 12:08:31 +0100 Subject: [PATCH 5/5] =?UTF-8?q?Mise=20=C3=A0=20jout=20des=20objets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- out/production/aroTP2/App.class | Bin 1210 -> 1386 bytes src/App.java | 2 ++ src/Backpack.java | 13 +++++++++++++ 3 files changed, 15 insertions(+) diff --git a/out/production/aroTP2/App.class b/out/production/aroTP2/App.class index 95f21adb36e1b15508b3395cdc31c93038417156..ddc92ab5174f71e7f1974260537408a5c4b0286f 100644 GIT binary patch delta 490 zcmdnR`HG9{)W2Q(7#JAr7{WJlwK3LnF$gkf@i1sJ=rA(yr>B-Ur52Y2=jW7`Waj5F zGO%c9Mn`cm=rQQCGZ^qN7%~_!G6-ZPmL=+!mSpDWI~ElsRu(fdaFpZ+mlS2@r86>! zX+%e9`hb<>B<7{-L&U9x7#J8#co-BJOc@!}G!XJ~Fa=ojurruXp3JzM(RH#llcOL% z8-o`YgExZ@JA*ICAiv4WnGP{?Fa%7lXA&1;WZ?14%S$bC&PgmTPAz6<2%cQcti=+_ z$RIblj#(~)k%7%8KR>&)fI*SLjDd*(1eh2Y8CV(E7#JBW7#JAz85kKD85kH?wYD=b zZe(C!U}CUjU;s-qFt9LKF)%RjKol@oGuSXNFmN*1GT1RNfi;&iFo6v<o~+NJP``;m zRC^nP<_-qkT@1zy4BHr#5$1_8u!2qHWngCzWZ+;>VBlmhV&DRsF9tD#!Jff^fq{XS zL5;zY!HI#9!I=T%0!9WG24)5~u*=*TJQx@lI2afiJQ)}n{22ln7#O%17#V^XLKwoB G7$gDJ%vGKM delta 298 zcmaFGwTqMM)W2Q(7#J9w7`!%ewJ|cYGiXiLWm?W?G5IQ!qc#VFEhB?SZfa&uPHJh9 zLUDdhX-Q^&o`Mx4KO2KR7lQ+XBRhi=4}&6u^JI1ALqd!UJf3-ZsYT8?iN(dK#q11j zlSNsy7(FICu;>bFGB7cK022cv11kd?10#d><OUWoV;u$tFpq(OnL(F<fq{pCk%57M zl|hd|pMimalfi(&kb#Lon}LBLk%0+phT`OtEDG5h85kIt7(lv(7+AnMcp2Cj1R2;F z6c{+bCW%2*GZ--#GcYjlGKeskFqkqhGMF(iFt9K%GMF<kGgyL6v0|`hU|`^2U}Ugk YU}UgiaA9Cz;9_88aAk03@MMq#0MH&M*Z=?k diff --git a/src/App.java b/src/App.java index 10a0593..9fbf355 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 dbcf2ac..7f57281 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; + } } -- GitLab