Finished Model Logic (no Threads)

This commit is contained in:
samuele-brusegan
2026-05-14 14:36:56 +02:00
commit d731d21783
19 changed files with 505 additions and 0 deletions

39
.gitignore vendored Normal file
View File

@@ -0,0 +1,39 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
.kotlin
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

10
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,10 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Ignored default folder with query files
/queries/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

7
.idea/encodings.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

15
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="KubernetesApiProvider"><![CDATA[{}]]></component>
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" default="true" project-jdk-name="25" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

30
nbactions.xml Normal file
View File

@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<actionName>run</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>clean</goal>
<goal>javafx:run</goal>
</goals>
</action>
<action>
<actionName>jlink</actionName>
<goals>
<goal>clean</goal>
<goal>javafx:jlink</goal>
</goals>
</action>
<action>
<actionName>debug</actionName>
<goals>
<goal>clean</goal>
<goal>javafx:run@debug</goal>
</goals>
<properties>
<jpda.listen>true</jpda.listen>
</properties>
</action>
</actions>

54
pom.xml Normal file
View File

@@ -0,0 +1,54 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>brusegan_samuele_VETF04000T_secondaprova</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>25</maven.compiler.source>
<maven.compiler.target>25</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>21.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<release>25</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.4</version>
<executions>
<execution>
<id>default-cli</id>
<configuration>
<mainClass>org.example.App</mainClass>
</configuration>
</execution>
<execution>
<id>debug</id>
<configuration>
<options>
<!--suppress UnresolvedMavenProperty -->
<option>-agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address}</option>
</options>
<mainClass>org.example.App</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,4 @@
module org.example {
requires javafx.controls;
exports org.example;
}

View File

@@ -0,0 +1,24 @@
package org.example;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import org.example.views.MainUI;
/**
* JavaFX App
*/
public class App extends Application {
@Override
public void start(Stage stage) {
MainUI.start(stage);
}
public static void main(String[] args) {
launch();
}
}

View File

@@ -0,0 +1,13 @@
package org.example;
public class SystemInfo {
public static String javaVersion() {
return System.getProperty("java.version");
}
public static String javafxVersion() {
return System.getProperty("javafx.version");
}
}

View File

@@ -0,0 +1,4 @@
package org.example.controller;
public class Controller {
}

View File

@@ -0,0 +1,36 @@
package org.example.models;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Importer {
private static final String FILENAME_CSV_DATA = "./src/main/resources/punti_interesse.csv";
public static ArrayList<Node> importData() {
ArrayList<Node> nl = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(FILENAME_CSV_DATA))) {
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
nl.add(
new Node(
data[0],
Double.parseDouble(data[1]),
Integer.parseInt(data[2])
)
);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return nl;
}
}

View File

@@ -0,0 +1,84 @@
package org.example.models;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class MainLogic {
private static final HashMap<Set<Node>, List<Path>> cache = new HashMap<>();
public static final double MULTIPLIER = 1.5;
public static final int K = 4;
public static double LENGHT;
public static void main(String[] args) {
ArrayList<Node> nl = importData();
Node moncalieri = nl.getLast();
nl.remove(moncalieri);
LENGHT = nl.size();
ArrayList<Path> paths = getPaths(nl);
for (Path path : paths) path.add(moncalieri);
Path bestPath = null;
double bestPrestigio = Double.NEGATIVE_INFINITY;
for (Path p : paths) {
double prestigio = p.getPrestigio();
if (bestPrestigio < prestigio) {
bestPrestigio = prestigio;
bestPath = p;
}
}
System.out.println(bestPath);
}
public static ArrayList<Node> importData() {
return Importer.importData();
}
public static ArrayList<Path> getPaths(List<Node> nl) {
// Dynamic Programming: la chiave è l'insieme dei nodi rimanenti
// (l'ordine non conta, ciò che conta è quali nodi possiamo ancora usare).
Set<Node> key = new HashSet<>(nl);
if (cache.containsKey(key)) {
// Restituisco copie difensive: i chiamanti mutano i Path con addAsFirstNode.
ArrayList<Path> copy = new ArrayList<>();
for (Path p : cache.get(key)) copy.add(new Path(p));
return copy;
}
if (nl.size() == LENGHT - K - 1) {
ArrayList<Path> paths = new ArrayList<>();
paths.add(new Path()); // path vuoto: i K nodi verranno aggiunti dai livelli superiori
return paths;
}
ArrayList<Path> result = new ArrayList<>();
for (Node n : nl) {
ArrayList<Node> bak = new ArrayList<>(nl);
bak.remove(n);
ArrayList<Path> subPaths = getPaths(bak);
for (Path p : subPaths) p.addAsFirstNode(n);
result.addAll(subPaths);
}
// Salvo in cache copie immutate, così le mutazioni dei chiamanti non corrompono il cache.
ArrayList<Path> cached = new ArrayList<>();
for (Path p : result) cached.add(new Path(p));
cache.put(key, cached);
return result;
}
public static boolean needBonus(Node s, Node e){
return s != null && s.getType() != e.getType();
}
}

View File

@@ -0,0 +1,51 @@
package org.example.models;
import java.util.Objects;
public class Node {
private String name;
private double prestigio;
private boolean type;
public Node(String name, double prestigio, boolean type) {
this.name = name;
this.prestigio = prestigio;
this.type = type;
}
public Node(String name, double prestigio, int type) {
this.name = name;
this.prestigio = prestigio;
this.type = ( type == 1 );
}
public boolean getType() {
return type;
}
public double getPrestigio() {
return prestigio;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Node n)) return false;
return Double.compare(prestigio, n.prestigio) == 0
&& type == n.type
&& Objects.equals(name, n.name);
}
@Override
public int hashCode() {
return Objects.hash(name, prestigio, type);
}
@Override
public String toString() {
return "Node[" +
name +
", " + prestigio +
", " + type +
']';
}
}

View File

@@ -0,0 +1,67 @@
package org.example.models;
import java.util.ArrayList;
public class Path {
private ArrayList<Node> nodes;
private double prestigioTotale;
public Path() {
this.nodes = new ArrayList<>();
this.prestigioTotale = 0;
}
public Path(Path other) {
this.nodes = new ArrayList<>(other.nodes);
this.prestigioTotale = other.prestigioTotale;
}
public void add(Node n) {
if (nodes.isEmpty()) {
prestigioTotale = n.getPrestigio();
nodes.add(n);
return;
}
Node p = nodes.getLast();
prestigioTotale += calculateDeltaPrestigio(p, n);
nodes.add(n);
}
private double calculateDeltaPrestigio(Node p, Node n) {
return (MainLogic.needBonus(p,n)) ?
n.getPrestigio() * MainLogic.MULTIPLIER :
n.getPrestigio();
}
public void addAsFirstNode(Node n) {
if (nodes.isEmpty()) {
prestigioTotale = n.getPrestigio();
nodes.add(n);
return;
}
nodes.addFirst(n);
updatePrestigio();
}
private void updatePrestigio() {
Node p = null;
prestigioTotale = 0;
for (Node n : nodes) {
prestigioTotale += calculateDeltaPrestigio(p, n);
p = n;
}
}
@Override
public String toString() {
return "Path{" +
"nodes=" + nodes +
", prestigioTotale=" + prestigioTotale +
'}';
}
public double getPrestigio() {
return prestigioTotale;
}
}

View File

@@ -0,0 +1,8 @@
package org.example.models;
public class PathBuilder implements Runnable {
@Override
public void run() {
}
}

View File

@@ -0,0 +1,17 @@
package org.example.views;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class MainUI {
public static void start(Stage stage) {
var label = new Label("Hello, JavaFX " + "javafxVersion" + ", running on Java " + "javaVersion" + ".");
var scene = new Scene(new StackPane(label), 640, 480);
stage.setScene(scene);
stage.show();
}
}

View File

@@ -0,0 +1,15 @@
Palazzo Reale di Torino,55,0
Belvedere di Villa d'Este,42,1
Reggia di Caserta,54,0
Piazzale Michelangelo,50,1
Palazzo Pitti,53,0
Belvedere di Castelmola,38,1
Castello di Miramare,48,0
Rocca di Calascio,45,1
Palazzo Ducale di Urbino,50,0
Belvedere di San Vigilio,33,1
Castello di Racconigi,46,0
Belvedere della Silvana,28,1
Palazzo Madama,49,0
Punto Panoramico del Monte Baldo,40,1
Castello di Moncalieri,95,1
1 Palazzo Reale di Torino 55 0
2 Belvedere di Villa d'Este 42 1
3 Reggia di Caserta 54 0
4 Piazzale Michelangelo 50 1
5 Palazzo Pitti 53 0
6 Belvedere di Castelmola 38 1
7 Castello di Miramare 48 0
8 Rocca di Calascio 45 1
9 Palazzo Ducale di Urbino 50 0
10 Belvedere di San Vigilio 33 1
11 Castello di Racconigi 46 0
12 Belvedere della Silvana 28 1
13 Palazzo Madama 49 0
14 Punto Panoramico del Monte Baldo 40 1
15 Castello di Moncalieri 95 1

View File

@@ -0,0 +1,21 @@
Palazzo Reale di Torino,55,0
Belvedere di Villa d'Este,42,1
Reggia di Caserta,54,0
Piazzale Michelangelo,50,1
Palazzo Pitti,53,0
Belvedere di Castelmola,38,1
Castello di Miramare,48,0
Rocca di Calascio,45,1
Palazzo dei Normanni,52,0
Belvedere di Lanzo d'Intelvi,35,1
Villa Reale di Monza,47,0
Terrazza del Pincio,49,1
Castello del Buonconsiglio,44,0
Belvedere di Ravello,51,1
Palazzo Ducale di Urbino,50,0
Belvedere di San Vigilio,33,1
Castello di Racconigi,46,0
Belvedere della Silvana,28,1
Palazzo Madama,49,0
Punto Panoramico del Monte Baldo,40,1
Castello di Moncalieri,95,1
1 Palazzo Reale di Torino 55 0
2 Belvedere di Villa d'Este 42 1
3 Reggia di Caserta 54 0
4 Piazzale Michelangelo 50 1
5 Palazzo Pitti 53 0
6 Belvedere di Castelmola 38 1
7 Castello di Miramare 48 0
8 Rocca di Calascio 45 1
9 Palazzo dei Normanni 52 0
10 Belvedere di Lanzo d'Intelvi 35 1
11 Villa Reale di Monza 47 0
12 Terrazza del Pincio 49 1
13 Castello del Buonconsiglio 44 0
14 Belvedere di Ravello 51 1
15 Palazzo Ducale di Urbino 50 0
16 Belvedere di San Vigilio 33 1
17 Castello di Racconigi 46 0
18 Belvedere della Silvana 28 1
19 Palazzo Madama 49 0
20 Punto Panoramico del Monte Baldo 40 1
21 Castello di Moncalieri 95 1