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