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] 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