Skip to content
Snippets Groups Projects
Commit d28d7378 authored by OUATTARA Sie's avatar OUATTARA Sie
Browse files

implementation de la fonction explore_from

parent 5bac2569
No related branches found
No related tags found
1 merge request!4Dev1
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <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" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>
\ No newline at end of file
No preview for this file type
No preview for this file type
No preview for this file type
...@@ -2,9 +2,8 @@ import java.util.List; ...@@ -2,9 +2,8 @@ import java.util.List;
public class App { public class App {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
Backpack backpack= new InstanceReader().read("src/sacTest"); Backpack backpack= new InstanceReader().read("src/sac1");
backpack.sortByRatio(); backpack.solve();
backpack.solutionFractionnelle(); System.out.println("meilleur solution :" + backpack.getBestValue());
System.out.println("solution fractionnelle :" + backpack.getSfValuee());
} }
} }
...@@ -2,16 +2,29 @@ import java.util.Comparator; ...@@ -2,16 +2,29 @@ import java.util.Comparator;
import java.util.List; import java.util.List;
public class Backpack { public class Backpack {
int capacity; private int capacity;
List<Loot> loots; private List<Loot> loots;
int sfValuee = 0; private int sfValuee = 0;
int sfWeight = 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) { public Backpack(int capacity, List<Loot> loots) {
this.capacity = capacity; this.capacity = capacity;
this.loots = loots; this.loots = loots;
} }
public void solve() {
bestSolution = new boolean[loots.size()];
currentSolution = new boolean[loots.size()];
sortByRatio();
solutionFractionnelle();
explore_from(0);
}
public String toString(){ public String toString(){
return capacity +"\n"+loots; return capacity +"\n"+loots;
} }
...@@ -41,8 +54,43 @@ public class Backpack { ...@@ -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() { public int getSfValuee() {
return sfValuee; return sfValuee;
} }
public int getBestValue() {
return bestValue;
}
} }
public class Loot { public class Loot {
int weight, value; private int weight, value;
float ratio; private final float ratio;
public Loot(int weight, int value) { public Loot(int weight, int value) {
this.weight = weight; this.weight = weight;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment