Skip to content
Snippets Groups Projects
Commit ea215108 authored by Gianluca Mastrolonardo's avatar Gianluca Mastrolonardo
Browse files

es1

parent dd38697b
No related branches found
No related tags found
No related merge requests found
Showing
with 369 additions and 0 deletions
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="a31b5e32-c084-4369-aff9-c8f29c58e800" name="Changes" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="FormatOnSaveOptions">
<option name="myRunOnSave" value="true" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/../../.." />
</component>
<component name="ProjectColorInfo"><![CDATA[{
"associatedIndex": 0
}]]></component>
<component name="ProjectId" id="2g5W2DLevjbbNliwOwRtooQA3lb" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"ASKED_ADD_EXTERNAL_FILES": "true",
"Downloaded.Files.Path.Enabled": "false",
"Repository.Attach.Annotations": "false",
"Repository.Attach.JavaDocs": "false",
"Repository.Attach.Sources": "false",
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"git-widget-placeholder": "main",
"last_opened_file_path": "/home/20050114/Documents/appunti/Algoritmi_2/Laboratorio/graphLib.jar",
"project.structure.last.edited": "Modules",
"project.structure.proportion": "0.0",
"project.structure.side.proportion": "0.2",
"settings.editor.selected.configurable": "actions.on.save"
}
}]]></component>
<component name="RunManager" selected="JUnit.testPrim">
<configuration name="testPrim" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Lab7" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="testPrim" />
<option name="TEST_OBJECT" value="class" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="testPrim.testMST" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Lab7" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="testPrim" />
<option name="METHOD_NAME" value="testMST" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="JUnit.testPrim" />
<item itemvalue="JUnit.testPrim.testMST" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="a31b5e32-c084-4369-aff9-c8f29c58e800" name="Changes" comment="" />
<created>1714987711704</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1714987711704</updated>
</task>
<servers />
</component>
</project>
\ No newline at end of file
File added
File added
File added
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Lab9.iml" filepath="$PROJECT_DIR$/Lab9.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../../.." vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/Test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../graphLib.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MODULE_DIR$/../graphLib.jar!/doc" />
</JAVADOC>
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
\ No newline at end of file
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class testFracKnapsack {
@Test
void testCreate() {
double capacity = 10;
double[] volume = new double[(int) capacity];
double[] value = new double[(int) capacity];
for (int i = 0; i < capacity; ++i) {
volume[i] = i * 10;
value[i] = i * 2.2;
}
FracKnapsack knapsack = new FracKnapsack(capacity, volume, value);
Assertions.assertNotNull(knapsack);
}
@Test
void testOneElement() {
//capacità > volume elemento
double capacity = 7;
double[] volume = new double[(int) capacity];
double[] value = new double[(int) capacity];
value[0] = 10;
volume[0] = 3;
FracKnapsack knapsack = new FracKnapsack(capacity, volume, value);
Assertions.assertEquals(10, knapsack.maxVal());
Assertions.assertEquals(1, knapsack.dose(0));
//capacità < volume elemento
capacity = 2;
value[0] = 10;
volume[0] = 4;
FracKnapsack knapsack2 = new FracKnapsack(capacity, volume, value);
Assertions.assertEquals(5, knapsack2.maxVal());
Assertions.assertEquals(0.5, knapsack2.dose(0));
}
@Test
void testThreeElements() {
//1 test in cui somma di tutti i volumi < capacità
//es: valori = 2,5,1; volumi = 3,2,4; capacita = 10; quindi dosi= 1,1,1 valoreMassimo = 8
double capacity = 10;
double[] volume = new double[(int) capacity];
double[] value = new double[(int) capacity];
value[0] = 2;
value[1] = 5;
value[2] = 1;
volume[0] = 3;
volume[1] = 2;
volume[2] = 4;
FracKnapsack knapsack = new FracKnapsack(capacity, volume, value);
Assertions.assertEquals(8, knapsack.maxVal());
for (int i = 0; i < 3; ++i) {
Assertions.assertEquals(1, knapsack.dose(i));
}
//1 test in cui si prende solo parte di un elemento
//(es: valori = 10,2,0.5; volumi = 70,12,3; capacita = 3;
//le soluzioni ottimali sono due: (0, 0.25, 0) oppure (0, 0, 1); valoreMassimo = 0.5)
capacity = 3;
value[0] = 10;
value[1] = 2;
value[2] = 0.5;
volume[0] = 70;
volume[1] = 12;
volume[2] = 3;
FracKnapsack knapsack2 = new FracKnapsack(capacity, volume, value);
Assertions.assertEquals(0.5, knapsack2.maxVal());
Assertions.assertTrue((knapsack2.dose(0) == 0 && knapsack2.dose(1) == 0.25 && knapsack2.dose(2) == 0) || ((knapsack2.dose(0) == 0) && knapsack2.dose(1) == 0) && knapsack2.dose(2) == 1);
//es: valori = 5,3,1; volumi = 4,3,4; capacita = 10;
//quindi dosi= 1.0 1.0 0.75 valoreMassimo = 8.75)
capacity = 10;
value[0] = 5;
value[1] = 3;
value[2] = 1;
volume[0] = 4;
volume[1] = 3;
volume[2] = 4;
FracKnapsack knapsack3 = new FracKnapsack(capacity, volume, value);
Assertions.assertEquals(8.75, knapsack3.maxVal());
Assertions.assertTrue(knapsack3.dose(0) == 1.0 && knapsack3.dose(1) == 1.0 && knapsack3.dose(2) == 0.75);
}
}
import it.uniupo.algoTools.MaxHeap;
import java.util.Arrays;
public class FracKnapsack {
private final double capacity;
private final double[] volume;
private final double[] value;
public FracKnapsack(double capacity, double[] volume, double[] value) {
this.capacity = capacity;
this.volume = volume;
this.value = value;
}
private double fracKnapsackImpl(double[] dose, double[] quant) {
double valTot = 0;
double remaningSpace = capacity;
MaxHeap<Integer, Double> maxHeap = new MaxHeap<>();
for (int i = 0; i < dose.length; ++i) {
maxHeap.add(i, value[i] / volume[i]);
}
while (!maxHeap.isEmpty() && remaningSpace > 0) {
// Estraggo il materiale con valore unitario massimo
int extractedMaterial = maxHeap.extractMax();
//Se posso prendererlo tutto senza riempite lo zaiono lo prendo tutto
if (remaningSpace >= volume[extractedMaterial]) {
dose[extractedMaterial] = 1;
quant[extractedMaterial] = volume[extractedMaterial];
valTot += value[extractedMaterial];
}
//Altrimenti ne prendo una frazione
else {
dose[extractedMaterial] = remaningSpace / volume[extractedMaterial];
quant[extractedMaterial] = remaningSpace;
valTot += (value[extractedMaterial] * dose[extractedMaterial]);
}
remaningSpace -= quant[extractedMaterial];
}
return valTot;
}
public double maxVal() {
double[] dose = new double[volume.length];
double[] quant = new double[volume.length];
return fracKnapsackImpl(dose, quant);
}
public double dose(int i) {
if (i < 0 || i > volume.length) {
throw new IllegalArgumentException("Valore di dose inserito errato");
}
double[] dose = new double[volume.length];
double[] quant = new double[volume.length];
fracKnapsackImpl(dose, quant);
return dose[i];
}
public boolean more(int i, int j) {
if ((i < 0 || i > volume.length) || (j < 0 || j > volume.length)) {
throw new IllegalArgumentException("Valore di dose inserito errato");
}
double[] dose = new double[volume.length];
double[] quant = new double[volume.length];
fracKnapsackImpl(dose, quant);
return quant[i] > quant[j];
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment