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

try to implement solutionFractionnelle

parent 179253d6
No related branches found
No related tags found
1 merge request!3Dev1
<?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_18" default="true" project-jdk-name="18" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="22" 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
...@@ -3,6 +3,7 @@ import java.util.List; ...@@ -3,6 +3,7 @@ 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/sac0"); Backpack backpack= new InstanceReader().read("src/sac0");
backpack.sortByRatio();
System.out.println(backpack); System.out.println(backpack);
} }
} }
import java.util.Comparator;
import java.util.List; import java.util.List;
public class Backpack { public class Backpack {
int capacity; int capacity;
List<Loot> loots; List<Loot> loots;
int sfValuee = 0;
int sfWeight = 0;
public Backpack(int capacity, List<Loot> loots) { public Backpack(int capacity, List<Loot> loots) {
this.capacity = capacity; this.capacity = capacity;
...@@ -12,4 +15,31 @@ public class Backpack { ...@@ -12,4 +15,31 @@ public class Backpack {
public String toString(){ public String toString(){
return capacity +"\n"+loots; 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 void solutionFractionnelle(){
this.sortByRatio();
for(Loot loot : loots){
if(loot.getWeight() > this.capacity){
sfValuee += (loot.getValue() * loot.getWeight()) / (capacity - sfWeight);
sfWeight += capacity;
}
else {
sfValuee += loot.getWeight();
sfWeight += loot.getWeight();
}
}
}
public int getSfValuee() {
return sfValuee;
}
} }
...@@ -23,5 +23,4 @@ public class InstanceReader { ...@@ -23,5 +23,4 @@ public class InstanceReader {
return new Backpack(capacity,loots); return new Backpack(capacity,loots);
} }
} }
public class Loot { public class Loot {
int weight, value; int weight, value;
float ratio;
public Loot(int weight, int value) { public Loot(int weight, int value) {
this.weight = weight; this.weight = weight;
this.value = value; this.value = value;
this.ratio = (float) weight/value;
} }
public String toString(){ 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;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment