Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • dev
  • dev1
  • main
3 results

Target

Select target project
  • couetoux.b/arotp1
  • a18004355/arotp1
  • o24014442/branch-and-bound
  • m21215674/aro-tp-1-mourih-amalya
4 results
Select Git revision
  • main
1 result
Show changes
Commits on Source (8)
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_18" 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
......@@ -2,7 +2,8 @@ import java.util.List;
public class App {
public static void main(String[] args) throws Exception {
Backpack backpack= new InstanceReader().read("src/sac0");
System.out.println(backpack);
Backpack backpack= new InstanceReader().read("src/sac4");
backpack.solve();
System.out.println("meilleur solution :" + backpack.getBestValue());
}
}
import java.util.Comparator;
import java.util.List;
public class Backpack {
int capacity;
List<Loot> loots;
private final int capacity;
private final List<Loot> loots;
private int bestValue = 0;
private int currentWeight = 0;
private int currentValue = 0;
public Backpack(int capacity, List<Loot> loots) {
this.capacity = capacity;
this.loots = loots;
}
public void solve() {
this.sortByRatio();
explore_from(0);
System.out.println("La valeur optimale est : " + bestValue);
}
public String toString(){
return capacity +"\n"+loots;
}
/**
Méthode pour trier les objets par ordre décroissant selon leur ratio
*/
public void sortByRatio(){
loots.sort(Comparator.comparing(Loot::getRatio).reversed());
}
public int solutionFractionnelle(int startIndex, int remainingCapacity) {
int sfValuee = 0;
int sfWeight = 0;
// 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 = remainingCapacity - sfWeight;
sfValuee += (int) (loot.getRatio() * remainingWeight);
break;
}
}
return sfValuee;
}
private void explore_from(int index) {
// Si on a exploré tous les objets
if (index >= loots.size()) {
if (currentValue > bestValue) {
bestValue = currentValue;
}
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();
explore_from(index + 1);
currentWeight -= currentLoot.getWeight();
currentValue -= currentLoot.getValue();
}
// on explore sans l'objet
explore_from(index + 1);
}
public int getBestValue() {
return bestValue;
}
}
......@@ -23,5 +23,4 @@ public class InstanceReader {
return new Backpack(capacity,loots);
}
}
public class Loot {
int weight, value;
private int weight, value;
private final float ratio;
public Loot(int weight, int value) {
this.weight = weight;
this.value = value;
this.ratio = (float) value/weight;
}
public String toString(){
return weight+" "+value;
return weight+" "+value+" "+ratio;
}
public float getRatio(){
return ratio;
}
public int getWeight(){
return weight;
}
public int getValue(){
return value;
}
}
15
5 40
8 48
4 36
\ No newline at end of file