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

Merge branch 'dev1' into 'main'

Dev1

See merge request !4
parents 5bac2569 f62f4c98
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
import java.util.Arrays;
import java.util.List; 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/sac4");
backpack.sortByRatio(); backpack.solve();
backpack.solutionFractionnelle(); System.out.println("meilleur solution :" + backpack.getBestValue());
System.out.println("solution fractionnelle :" + backpack.getSfValuee()); System.out.println("meilleur solution :" + Arrays.toString(backpack.getBestSolution()));
} }
} }
...@@ -2,14 +2,27 @@ import java.util.Comparator; ...@@ -2,14 +2,27 @@ import java.util.Comparator;
import java.util.List; import java.util.List;
public class Backpack { public class Backpack {
int capacity; private final int capacity;
List<Loot> loots; private final List<Loot> loots;
int sfValuee = 0; private int bestValue = 0;
int sfWeight = 0; private int currentWeight = 0;
private int currentValue = 0;
private boolean[] currentSolution;
private boolean[] bestSolution;
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;
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(){ public String toString(){
...@@ -23,26 +36,68 @@ public class Backpack { ...@@ -23,26 +36,68 @@ public class Backpack {
loots.sort(Comparator.comparing(Loot::getRatio).reversed()); loots.sort(Comparator.comparing(Loot::getRatio).reversed());
} }
public void solutionFractionnelle(){ public int solutionFractionnelle(int startIndex, int remainingCapacity) {
this.sortByRatio(); int sfValuee = 0;
int sfWeight = 0;
for(Loot loot : loots){ // On commence à partir de startIndex
if(sfWeight + loot.getWeight() <= this.capacity){ 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 // Si l'objet entier peut entrer dans le sac
sfValuee += loot.getValue(); sfValuee += loot.getValue();
sfWeight += loot.getWeight(); sfWeight += loot.getWeight();
} }
else { else {
// Si l'objet ne peut pas entrer entièrement dans le sac // 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); sfValuee += (int) (loot.getRatio() * remainingWeight);
break; break;
} }
} }
return sfValuee;
} }
public int getSfValuee() { private void explore_from(int index) {
return sfValuee; // 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;
}
// 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;
}
// on essaye d'explorer avec l'objet
Loot currentLoot = loots.get(index);
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
explore_from(index + 1);
}
public int getBestValue() {
return bestValue;
}
public boolean[] getBestSolution() {
return bestSolution;
}
} }
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