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"?>
<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
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;
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/sac4");
backpack.solve();
System.out.println("meilleur solution :" + backpack.getBestValue());
System.out.println("meilleur solution :" + Arrays.toString(backpack.getBestSolution()));
}
}
......@@ -2,14 +2,27 @@ import java.util.Comparator;
import java.util.List;
public class Backpack {
int capacity;
List<Loot> loots;
int sfValuee = 0;
int sfWeight = 0;
private final int capacity;
private final List<Loot> loots;
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(){
......@@ -23,26 +36,68 @@ public class Backpack {
loots.sort(Comparator.comparing(Loot::getRatio).reversed());
}
public void solutionFractionnelle(){
this.sortByRatio();
public int solutionFractionnelle(int startIndex, int remainingCapacity) {
int sfValuee = 0;
int sfWeight = 0;
for(Loot loot : loots){
if(sfWeight + loot.getWeight() <= this.capacity){
// 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;
}
public int getSfValuee() {
return sfValuee;
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;
}
// 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 {
int weight, value;
float ratio;
private int weight, value;
private final float ratio;
public Loot(int weight, int value) {
this.weight = weight;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment