- renamed LW to the correct name
- copied two nets as resources
- removed unnecessary code in NetworkNode
- Created MainGUI
- changed some code in SmileLib
This commit is contained in:
2024-01-04 23:37:46 +01:00
parent 42a54947a5
commit c2cd4e3fb2
18 changed files with 694 additions and 192 deletions

View File

@@ -1,38 +0,0 @@
package net.berack.upo.ai;
import java.util.Scanner;
import java.util.function.Function;
import net.berack.upo.ai.problem1.Puzzle8GUI;
import net.berack.upo.ai.problem2.TrisGUI;
public class Main {
public static void main(String[] args) {
var value = read("What do you want to play?\n1. Puzzle8\n2. Tris\n", new Scanner(System.in), num -> num > 0 && num < 2);
var window = switch (value) {
case 1 -> new Puzzle8GUI();
case 2 -> new TrisGUI();
default -> null;
};
if(window != null) {
window.toFront();
window.requestFocus();
}
}
private static int read(String out, Scanner in, Function<Integer, Boolean> control) {
var ret = 0;
do {
try {
System.out.print(out);
var str = in.nextLine();
ret = Integer.parseInt(str);
} catch (NumberFormatException ignore) {}
} while(!control.apply(ret));
return ret;
}
}

View File

@@ -0,0 +1,92 @@
package net.berack.upo.ai;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import net.berack.upo.ai.decision.PrototypeGUI;
import net.berack.upo.ai.decision.VehicleGUI;
import net.berack.upo.ai.problem1.Puzzle8GUI;
import net.berack.upo.ai.problem2.TrisGUI;
import net.berack.upo.ai.problem3.LikelihoodWeightingGUI;
/**
* Classe che rappresenta il main di tutto il progetto
* In essa si può navigare in tutti gli esercizi e testarli
* @author Berack
*/
public class MainGUI extends JFrame {
public final Puzzle8GUI Puzzle8GUI = new Puzzle8GUI();
public final TrisGUI TrisGUI = new TrisGUI();
public final LikelihoodWeightingGUI LikelihoodWeightingGUI = new LikelihoodWeightingGUI();
public final PrototypeGUI PrototypeGUI = new PrototypeGUI();
public final VehicleGUI VehicleGUI = new VehicleGUI();
public static void main(String[] args) {
new MainGUI();
}
private final JMenuBar menuBar = new JMenuBar();
/**
* Crea una finestra con nulla da mostrare, ma si può visualizzare uno degli esercizi tramite
* la barre dei menù che permette di cambiare da un esercizio all'altro
*/
private MainGUI() {
super("Progetto per AI");
this.buildMenu();
this.setJMenuBar(menuBar);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400, 400);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
/**
* Crea la barra dei menu aggiungendo tutti i menù passati in input
* @param menus una lista di menù da aggiungere
*/
private void buildMenu(JMenu...menus) {
menuBar.removeAll();
var menu = new JMenu("View");
var puzzle = new JMenuItem("Puzzle8 Game");
puzzle.addActionListener(action -> this.setPanel(Puzzle8GUI));
var tris = new JMenuItem("Tris Game");
tris.addActionListener(action -> this.setPanel(TrisGUI));
var lw = new JMenuItem("Likelihood Weighting");
lw.addActionListener(action -> this.setPanel(LikelihoodWeightingGUI));
var prototype = new JMenuItem("Prototype net");
prototype.addActionListener(action -> this.setPanel(PrototypeGUI));
var vehicle = new JMenuItem("Vehicle net");
vehicle.addActionListener(action -> this.setPanel(VehicleGUI));
menu.add(puzzle);
menu.add(tris);
menu.add(lw);
menu.add(new JSeparator());
menu.add(prototype);
menu.add(vehicle);
menuBar.add(menu);
for(var m : menus) menuBar.add(m);
}
/**
* Cambia il pannello principale con quello passato in input
* @param panel il pannello da mostrare
*/
private void setPanel(MyPanel panel) {
this.setContentPane(panel);
this.buildMenu(panel.getMenu());
this.pack();
this.invalidate();
this.validate();
this.repaint();
}
}

View File

@@ -0,0 +1,20 @@
package net.berack.upo.ai;
import javax.swing.JMenu;
import javax.swing.JPanel;
/**
* Classe utilizzata per far si che tutti i frame della finestra abbiano lo stesso metodo
* per la generazione dei menu.
* @author Berack
*/
public abstract class MyPanel extends JPanel {
/**
* Crea un menu da aggiungere alla lista, in modo che appaia solamente quando
* la finestra venga utilizzata.
* Questo metodo viene usato solo dalla finestra MainGUI
* @return il menu contestuale da aggiungere
*/
abstract public JMenu getMenu();
}

View File

@@ -1,5 +0,0 @@
package net.berack.upo.ai.decision;
public class ConsoleInterface {
}

View File

@@ -0,0 +1,20 @@
package net.berack.upo.ai.decision;
import javax.swing.JMenu;
import net.berack.upo.ai.MyPanel;
/**
* Classe che mostra le decisioni possibili, insieme ai guadagni
* per la rete Prototipo.xdsl
* @author Berack
*/
public class PrototypeGUI extends MyPanel {
@Override
public JMenu getMenu() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getMenu'");
}
}

View File

@@ -0,0 +1,19 @@
package net.berack.upo.ai.decision;
import javax.swing.JMenu;
import net.berack.upo.ai.MyPanel;
/**
* Classe che mostra le decisioni possibili, insieme ai guadagni
* per la rete Veicolo.xdsl
*/
public class VehicleGUI extends MyPanel {
@Override
public JMenu getMenu() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getMenu'");
}
}

View File

@@ -1,31 +1,25 @@
package net.berack.upo.ai.problem1;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import net.berack.upo.ai.problem1.Puzzle8.Move;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import net.berack.upo.ai.MyPanel;
import net.berack.upo.ai.problem1.Puzzle8.Move;
/**
* Classe che permette di visualizzare graficamente il gioco Puzzle8
* In questa classe si trova un main che crea una istanza di questa finestra
*
* @author Berack96
*/
public class Puzzle8GUI extends JFrame {
public static void main(String[] args) {
new Puzzle8GUI();
}
public class Puzzle8GUI extends MyPanel {
/**
* Carica staticamente le immagini necessarie per giocare al gioco
@@ -50,49 +44,20 @@ public class Puzzle8GUI extends JFrame {
* Come default viene creato il puzzle "GOAL" ovvero non mescolato.
*/
public Puzzle8GUI() {
super("Puzzle 8 game");
super();
var grid = new GridLayout(Puzzle8.LENGTH, Puzzle8.LENGTH);
grid.setHgap(6);
grid.setVgap(grid.getHgap());
var panel = new JPanel(grid);
this.setLayout(grid);
for(var i = 0; i < Puzzle8.LENGTH; i++) {
for(var j = 0; j < Puzzle8.LENGTH; j++) {
var comp = new MyComponent(Puzzle8.LENGTH, j, i);
panel.add(comp);
this.add(comp);
this.buttons[j][i] = comp;
}
}
this.add(panel);
this.attachMenu();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
/**
* Metodo utile per non mettere tutto nel costruttore.
* Qui viene creata la menubar
*/
private void attachMenu() {
var menuBar = new JMenuBar();
var menu1 = new JMenu("Game");
var item1 = new JMenuItem("Shuffle");
item1.addActionListener(action -> this.shuffleGame());
menu1.add(item1);
var item2 = new JMenuItem("Show solution");
item2.addActionListener(action -> this.solveGame());
menu1.add(item2);
menuBar.add(menu1);
this.setJMenuBar(menuBar);
}
/**
@@ -194,4 +159,18 @@ public class Puzzle8GUI extends JFrame {
});
}
}
@Override
public JMenu getMenu() {
var menu = new JMenu("Game");
var item1 = new JMenuItem("Shuffle");
item1.addActionListener(action -> this.shuffleGame());
menu.add(item1);
var item2 = new JMenuItem("Show solution");
item2.addActionListener(action -> this.solveGame());
menu.add(item2);
return menu;
}
}

View File

@@ -1,18 +1,17 @@
package net.berack.upo.ai.problem2;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import java.awt.Color;
import java.awt.GridLayout;
import net.berack.upo.ai.MyPanel;
/**
* Classe che permette di visualizzare graficamente il gioco Tris
@@ -20,11 +19,7 @@ import java.awt.GridLayout;
*
* @author Berack96
*/
public class TrisGUI extends JFrame {
public static void main(String[] args) {
new TrisGUI();
}
public class TrisGUI extends MyPanel {
/**
* Caricamento statico delle immagini
@@ -57,54 +52,17 @@ public class TrisGUI extends JFrame {
* Come default viene abilitata la AI come secondo giocatore.
*/
public TrisGUI() {
super("Tris");
super();
var grid = new GridLayout(Tris.LENGTH, Tris.LENGTH);
var panel = new JPanel(grid);
this.setLayout(grid);
for(var i = 0; i < Tris.LENGTH; i++) {
for(var j = 0; j < Tris.LENGTH; j++) {
var comp = new MyComponent(Tris.LENGTH, j, i);
panel.add(comp);
this.add(comp);
this.buttons[j][i] = comp;
}
}
this.add(panel);
this.attachMenu();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
/**
* Metodo utile per non mettere tutto nel costruttore.
* Qui viene creata la menubar
*/
private void attachMenu() {
var menuBar = new JMenuBar();
var menu1 = new JMenu("Game");
var item1 = new JMenuItem("Reset");
item1.addActionListener(action -> this.reset());
menu1.add(item1);
var separator = new JSeparator();
menu1.add(separator);
var item2 = new JCheckBoxMenuItem("AI Enabled");
item2.setSelected(this.ai != null);
item2.addChangeListener(action -> this.ai = item2.getState()? new TrisAi(this.tris):null);
menu1.add(item2);
this.aiFirst = new JCheckBoxMenuItem("AI First");
this.aiFirst.setSelected(false);
menu1.add(this.aiFirst);
menuBar.add(menu1);
this.setJMenuBar(menuBar);
}
/**
@@ -175,4 +133,26 @@ public class TrisGUI extends JFrame {
});
}
}
@Override
public JMenu getMenu() {
var menu = new JMenu("Game");
var item1 = new JMenuItem("Reset");
item1.addActionListener(action -> this.reset());
menu.add(item1);
var separator = new JSeparator();
menu.add(separator);
var item2 = new JCheckBoxMenuItem("AI Enabled");
item2.setSelected(this.ai != null);
item2.addChangeListener(action -> this.ai = item2.getState()? new TrisAi(this.tris):null);
menu.add(item2);
this.aiFirst = new JCheckBoxMenuItem("AI First");
this.aiFirst.setSelected(false);
menu.add(this.aiFirst);
return menu;
}
}

View File

@@ -1,16 +1,18 @@
package net.berack.upo.ai.problem3;
import java.security.SecureRandom;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import smile.Network;
/**
* Calcolo dei valori tramite l'algoritmo del Likelyhood Weighting
* Calcolo dei valori tramite l'algoritmo del Likelihood Weighting
* @author Berack
*/
public class LikelyhoodWeighting {
public class LikelihoodWeighting {
public final Network net;
private final Map<Integer, NetworkNode> nodes = new HashMap<>();
@@ -19,14 +21,14 @@ public class LikelyhoodWeighting {
* Inizializza un nuovo oggetto che calcolerà i valori per la rete inserita
* @param net la rete a cui calcolare i valori
*/
public LikelyhoodWeighting(Network net) {
public LikelihoodWeighting(Network net) {
this.net = Objects.requireNonNull(net);
}
/**
* Recupera i valori del nodo dopo averli calcolati
* Nel caso in cui non si abbia ancora fatto {@link #updateNetwork(int)} allora restituirà
* una eccezione di tiop UnsupportedOperationException
* una eccezione di tipo UnsupportedOperationException
* @param node il nodo da vedere
* @return l'array di valori da restituire
*/
@@ -35,6 +37,17 @@ public class LikelyhoodWeighting {
return nodes.get(node).values;
}
/**
* Permette di recuperare tutti i nodi.
* Nel caso in cui non si abbia ancora fatto {@link #updateNetwork(int)} allora restituirà
* una eccezione di tipo UnsupportedOperationException
* @return Una collezione di tutti i nodi.
*/
public Collection<NetworkNode> getAllNodes() {
if(nodes.size() == 0) throw new UnsupportedOperationException("You should run first updateNetwork method");
return this.nodes.values();
}
/**
* Calcola i valori possibili per la rete.
* Per poterli vedere utilizzare il metodo {@link #getNodeValue(int)}
@@ -74,4 +87,9 @@ public class LikelyhoodWeighting {
node.values[i] /= sum;
}
}
@Override
public String toString() {
return this.nodes.values().toString();
}
}

View File

@@ -0,0 +1,141 @@
package net.berack.upo.ai.problem3;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Font;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import net.berack.upo.ai.MyPanel;
/**
* Classe usata per far vedere il risultato di una run di lw su un network
* @author Berack
*/
public class LikelihoodWeightingGUI extends MyPanel {
private LikelihoodWeighting lw = null;
private final JPanel scroll = new JPanel();
private final int totalRuns = 1000;
/**
* Crea il pannello con gli elementi di default.
* Siccome non c'è nessun network, il pannello sarà vuoto finchè non si apriranno dei network
*/
public LikelihoodWeightingGUI() {
this.scroll.setPreferredSize(new Dimension(500, 400));
this.add(this.scroll);
}
/**
* Crea una finestra di dialog per la richiesta di un file.
* Il file richiesto è in forma .xdsl
*/
public void openFile() {
var parent = this.getParent();
while(parent.getParent() != null) parent = parent.getParent();
if(!(parent instanceof JFrame)) throw new IllegalArgumentException(parent.getClass().getName());
var dialog = new FileDialog((JFrame) parent, "Select net");
dialog.setLocationRelativeTo(null);
dialog.setFile("*.xdsl");
dialog.setMode(FileDialog.LOAD);
dialog.setVisible(true);
if(dialog.getFile() != null) this.openFile(dialog.getDirectory() + dialog.getFile());
}
/**
* Carica il file indicato nella finestra, esegue l'algoritmo e mostra i risultati
* @param fileName il nome del file
*/
public void openFile(String fileName) {
try {
var net = SmileLib.getNetworkFrom(fileName);
this.lw = new LikelihoodWeighting(net);
this.updateLW();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Esegue l'algoritmo sul network corrente e mostra i risultati
*/
private void updateLW() {
this.lw.updateNetwork(totalRuns);
var nodes = this.lw.getAllNodes();
var panel = new JPanel();
var layout = new GroupLayout(panel);
panel.setLayout(layout);
layout.setAutoCreateGaps(true);
var gLabel = layout.createParallelGroup();
var gBarch = layout.createParallelGroup();
var vGroup = layout.createSequentialGroup();
for(var node : nodes) {
var label = new JLabel(node.name);
var barch = new OutcomeChartGUI(node, i -> {
if(node.evidence == i) node.net.clearEvidence(node.handle);
else node.net.setEvidence(node.handle, i);
this.updateLW();
});
var font = label.getFont();
label.setFont(new Font(font.getName(), font.getStyle(), font.getSize() + 4));
gLabel.addComponent(label);
gBarch.addComponent(barch);
vGroup.addGroup(layout.createParallelGroup()
.addComponent(label)
.addComponent(barch));
}
var hGroup = layout.createSequentialGroup();
hGroup.addGroup(gLabel).addGroup(gBarch);
layout.setVerticalGroup(vGroup);
layout.setHorizontalGroup(hGroup);
this.scroll.removeAll();
this.scroll.add(panel);
this.repaint();
this.invalidate();
this.validate();
this.repaint();
}
@Override
public JMenu getMenu() {
var menu = new JMenu("File");
var open = new JMenuItem("Open");
open.addActionListener(action -> this.openFile());
var net1 = new JMenuItem("WetGrass net");
net1.addActionListener(action -> this.openFile("lw/WetGrass.xdsl"));
var net2 = new JMenuItem("Malaria net");
net2.addActionListener(action -> this.openFile("lw/Malaria.xdsl"));
menu.add(open);
menu.add(new JSeparator());
menu.add(net1);
menu.add(net2);
return menu;
}
}

View File

@@ -16,6 +16,7 @@ import smile.Network;
public class NetworkNode {
final int handle;
final String name;
final String[] outcomes;
final double[] definition;
final int evidence;
@@ -36,12 +37,16 @@ public class NetworkNode {
NetworkNode(Network net, int handle, Map<Integer, NetworkNode> nodes) {
this.handle = handle;
this.type = net.getNodeType(handle);
this.name = net.getNodeId(handle);
this.net = net;
this.outcomes = net.getOutcomeIds(handle);
this.values = new double[this.outcomes.length];
this.evidence = net.isEvidence(handle)? net.getEvidence(handle) : -1;
if(this.isEvidence()) this.values[this.evidence] = 1.0d;
if(this.isEvidence()) {
this.values[this.evidence] = 1.0d;
this.sample = this.evidence;
}
var parentsHandle = net.getParents(handle);
this.parents = new NetworkNode[parentsHandle.length];
@@ -111,11 +116,10 @@ public class NetworkNode {
var tot = this.definition.length;
for(var p : this.parents) {
var pIndex = p.isEvidence()? p.evidence : p.sample;
if(pIndex < 0) throw new IllegalArgumentException("Parent"); // in theory impossible since Topological sorted
if(p.sample < 0) throw new IllegalArgumentException("Parent"); // in theory impossible since Topological sorted
tot /= p.outcomes.length;
init += tot * pIndex;
init += tot * p.sample;
}
return init;
@@ -132,4 +136,9 @@ public class NetworkNode {
return true;
}
@Override
public String toString() {
return this.net.getNodeId(this.handle) + "->" + Arrays.toString(this.values);
}
}

View File

@@ -0,0 +1,79 @@
package net.berack.upo.ai.problem3;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Label;
import java.util.function.Consumer;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
/**
* Classe che rappresenta gli outcome di un nodo di un network.
* I valori deli outcome vengono visualizzati con un grafico.
* @author Berack
*/
public class OutcomeChartGUI extends JPanel {
public static final Color[] COLORS = {
new Color(255,127,0),
new Color(0,127,255),
new Color(0,255,127),
new Color(255,0,127),
new Color(127,0,255),
new Color(127,255,0),
};
/**
* Crea il JPanel da visualizzare a partire da un NetworkNode appropriamente inizializzato.
* Quando verrà visualizzato, il nodo avrà il nome degli output e i suoi valori in %
* con una barra colorata per indicare la grandezza visivamente.
*
* @param node il nodo di cui si vogliono visualizzare gli outcome
* @param action una azione da fare nel caso in cui venga premuto su un outcome
*/
public OutcomeChartGUI(NetworkNode node, Consumer<Integer> action) {
var labels = node.outcomes;
var values = node.values;
if(labels.length != values.length) throw new IllegalArgumentException("Arrays length myst be equals!");
this.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(1, 0, 0, 0), BorderFactory.createLineBorder(Color.GRAY)));
var layout = new GridLayout(labels.length, 2);
this.setLayout(layout);
for(var i = 0; i < labels.length; i++) {
var value = values[i] * 100;
var lName = new JButton(labels[i]);
var lValue = new Label(String.format("% 4.2f%%", value));
var barchart = new Label();
var size = barchart.getPreferredSize();
final var index = i;
lName.setContentAreaFilled(false);
lName.setFocusable(false);
lName.addActionListener(a -> action.accept(index));
if(node.evidence == i) lName.setForeground(Color.RED);
size.width = (int) (value * 1.5);
barchart.setPreferredSize(size);
barchart.setBackground(COLORS[i % COLORS.length]);
var panel1 = new JPanel();
panel1.setLayout(new GridLayout(1, 2));
panel1.add(lName);
panel1.add(lValue);
var panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(barchart, BorderLayout.LINE_START);
this.add(panel1);
this.add(panel2);
}
}
}

View File

@@ -1,9 +1,11 @@
package net.berack.upo.ai.problem3;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import smile.Network;
/**
@@ -18,18 +20,19 @@ import smile.Network;
*/
public class SmileLib {
public static final String RESOURCE_PATH;
static {
var loader = SmileLib.class.getClassLoader();
var wrongPath = loader.getResource("").getFile();
var path = wrongPath.substring(1);
try {
RESOURCE_PATH = URLDecoder.decode(path, "ASCII");
var jsmile = "jsmile";
var loader = SmileLib.class.getClassLoader();
var resource = loader.getResource(jsmile);
var uri = resource.toURI();
var path = Path.of(uri).toString();
System.setProperty("jsmile.native.library", path);
} catch (Exception e) {
throw new RuntimeException("Decodification of path failed!\n" + e.getMessage());
e.printStackTrace();
System.exit(0);
}
System.setProperty("jsmile.native.library", RESOURCE_PATH + "jsmile");
new smile.License(
"SMILE LICENSE 02a07eb5 5c5fa64a 2a276459 " +
"THIS IS AN ACADEMIC LICENSE AND CAN BE USED " +
@@ -60,8 +63,12 @@ public class SmileLib {
public static Network getNetworkFrom(String file) {
var net = new Network();
try {
net.readFile(RESOURCE_PATH + file);
} catch (smile.SMILEException e) {
var loader = SmileLib.class.getClassLoader();
var in = loader.getResourceAsStream(file);
var str = new String(in.readAllBytes(), StandardCharsets.UTF_8);
net.readString(str);
} catch (Exception e) {
net.readFile(file);
}
@@ -78,15 +85,15 @@ public class SmileLib {
* @return una lista ordinata di nodi
*/
public static List<NetworkNode> buildListFrom(Network net) {
var nodes = new HashMap<Integer, NetworkNode>();
var list = new ArrayList<NetworkNode>();
var nodes = new HashMap<Integer, NetworkNode>();
var list = new ArrayList<NetworkNode>();
for(var handle : net.getAllNodes()) {
var node = new NetworkNode(net, handle, nodes);
list.add(node);
nodes.put(handle, node);
}
return list;
for(var handle : net.getAllNodes()) {
var node = new NetworkNode(net, handle, nodes);
list.add(node);
nodes.put(handle, node);
}
return list;
}
}

View File

@@ -35,18 +35,27 @@
<state id="Basso" />
<state id="Alto" />
<parents>Domanda_Mercato Qualità_Prodotto</parents>
<probabilities>0.9 0.09 0.01 0.5 0.35 0.15 0.1 0.2 0.7 0.01 0.09 0.9</probabilities>
<probabilities>0.99 0.008999999999999999 0.001 0.85 0.145 0.005 0.1 0.8 0.1 0.001 0.85 0.149</probabilities>
</cpt>
<decision id="Produzione">
<state id="Si" />
<state id="No" />
<parents>Migliorare_la_Qualià Effettuare_la_Ricerca</parents>
</decision>
<utility id="Valore_Profitto">
<parents>Profitto</parents>
<utilities>-2500 10000 50000</utilities>
<parents>Produzione Profitto</parents>
<utilities>0 10000 50000 0 0 0</utilities>
</utility>
<cpt id="Ricerca_di_Mercato">
<state id="Bassa" />
<state id="Alta" />
<parents>Effettuare_la_Ricerca Domanda_Mercato</parents>
<probabilities>0.9 0.09999999999999998 0.1 0.9 0.5 0.5 0.5 0.5</probabilities>
<probabilities>0.9 0.1 0.1 0.9 0.5 0.5 0.5 0.5</probabilities>
</cpt>
<utility id="Costo_Produzione">
<parents>Produzione</parents>
<utilities>-2500 0</utilities>
</utility>
</nodes>
<extensions>
<genie version="1.0" app="GeNIe 4.1.3402.0 ACADEMIC" name="Prototipo">
@@ -64,7 +73,7 @@
<interior color="e5f6f7" />
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>577 90 674 150</position>
<position>577 91 674 151</position>
<barchart active="true" width="128" height="64" />
</node>
<node id="Effettuare_la_Ricerca">
@@ -72,7 +81,7 @@
<interior color="e5f6f7" />
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>210 84 291 150</position>
<position>209 88 290 154</position>
<barchart active="true" width="128" height="64" />
</node>
<node id="Migliorare_la_Qualià">
@@ -89,7 +98,7 @@
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>40 104 118 140</position>
<barchart active="true" />
<barchart active="true" width="128" height="72" />
</node>
<node id="Costo_Prototipo">
<name>Costo Prototipo</name>
@@ -115,13 +124,29 @@
<position>589 347 661 379</position>
<barchart active="true" width="128" height="64" />
</node>
<node id="Produzione">
<name>Produzione</name>
<interior color="e5f6f7" />
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>221 350 287 376</position>
<barchart active="true" width="128" height="64" />
</node>
<node id="Ricerca_di_Mercato">
<name>Ricerca di Mercato</name>
<interior color="e5f6f7" />
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>385 90 472 150</position>
<barchart active="true" width="128" height="64" />
<position>383 90 484 153</position>
<barchart active="true" />
</node>
<node id="Costo_Produzione">
<name>Costo Produzione</name>
<interior color="e5f6f7" />
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>44 337 132 390</position>
<barchart active="true" />
</node>
</genie>
</extensions>

View File

@@ -11,12 +11,12 @@
<cpt id="Condizioni_Meteo" dynamic="plate">
<state id="Normale" />
<state id="Umido" />
<probabilities>0.9 0.1</probabilities>
<probabilities>0.5 0.5</probabilities>
</cpt>
<cpt id="Stato_Terreno" dynamic="plate">
<state id="Normale" />
<state id="Sconnesso" />
<probabilities>1 0</probabilities>
<probabilities>0.5 0.5</probabilities>
</cpt>
<noisymax id="Guasto" dynamic="plate">
<state id="Si" />
@@ -31,7 +31,7 @@
<state id="Ottima" />
<parents>Guasto Condizioni_Meteo Stato_Terreno</parents>
<strengths>0 1 1 0 1 0</strengths>
<parameters>1 0 0 0 0 1 0.3 0.15 0.5499999999999999 0 0 1 0.6 0.3 0.1 0 0 1 0 0 1</parameters>
<parameters>1 0 0 0 0 1 0.3 0.15 0.5499999999999999 0 0 1 0.6 0.3 0.1 0 0 1 0.000299999999999967 0.0007 0.999</parameters>
</noisymax>
<cpt id="Sensore_Posizione" dynamic="plate">
<state id="Left" />
@@ -50,11 +50,22 @@
<parents>Posizione</parents>
<utilities>-100 100 -100</utilities>
</utility>
<cpt id="Posizione_Finale" dynamic="terminal">
<state id="Left" />
<state id="Middle" />
<state id="Right" />
<parents>Comando Posizione</parents>
<probabilities>0.9 0.05 0.04999999999999998 0.9 0.05 0.05 0.05 0.9 0.05 0.9 0.05 0.05 0.04999999999999998 0.9 0.05 0.04999999999999993 0.05 0.9 0.05 0.9 0.05 0.05 0.05 0.9 0.05 0.05 0.9</probabilities>
</cpt>
<utility id="Utilità_2" dynamic="terminal">
<parents>Posizione_Finale</parents>
<utilities>-100 100 -100</utilities>
</utility>
</nodes>
<dynamic numslices="5">
<cpt id="Posizione" order="1">
<parents>Posizione Comando</parents>
<probabilities>0.9 0.1 0 0.9 0.1 0 0.1 0.9 0 0.9 0.05 0.05 0.04999999999999998 0.9 0.05 0.04999999999999993 0.05 0.9 0 0.9 0.1 0 0.1 0.9 0 0.1 0.9</probabilities>
<probabilities>0.9 0.05 0.04999999999999998 0.9 0.05 0.05 0.05 0.9 0.05 0.9 0.05 0.05 0.04999999999999998 0.9 0.05 0.04999999999999993 0.05 0.9 0.05 0.9 0.05 0.05 0.05 0.9 0.05 0.05 0.9</probabilities>
</cpt>
<cpt id="Condizioni_Meteo" order="1">
<parents>Condizioni_Meteo</parents>
@@ -64,6 +75,14 @@
<parents>Stato_Terreno</parents>
<probabilities>0.8 0.2 0.7 0.3</probabilities>
</cpt>
<noisymax id="Guasto" order="1">
<parents>Guasto</parents>
<strengths>1 0 1 0 0 1</strengths>
<parameters>0.1 0.9 0 1 0.5 0.5 0 1 1 0 0 1 0.001 0.999</parameters>
</noisymax>
<decision id="Comando" order="1">
<parents>Comando</parents>
</decision>
</dynamic>
<extensions>
<genie version="1.0" app="GeNIe 4.1.3402.0 ACADEMIC" name="Veicolo">
@@ -74,7 +93,7 @@
<interior color="e5f6f7" />
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>312 320 402 362</position>
<position>308 292 398 334</position>
<barchart width="128" height="108" />
</node>
<node id="Posizione">
@@ -82,16 +101,16 @@
<interior color="e5f6f7" />
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>231 460 307 506</position>
<barchart active="true" width="128" height="108" />
<position>232 386 308 432</position>
<barchart width="128" height="108" />
</node>
<node id="Sensore_Posizione">
<name>Sensore Posizione</name>
<interior color="e5f6f7" />
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>148 318 223 370</position>
<barchart active="true" width="128" height="108" />
<position>148 285 223 337</position>
<barchart width="128" height="108" />
</node>
<node id="Accuratezza_Sensore">
<name>Accuratezza Sensore</name>
@@ -99,7 +118,7 @@
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>142 179 228 229</position>
<barchart active="true" width="128" height="108" />
<barchart width="128" height="108" />
</node>
<node id="Condizioni_Meteo">
<name>Condizioni Meteo</name>
@@ -107,7 +126,7 @@
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>140 47 231 103</position>
<barchart active="true" width="128" height="78" />
<barchart width="128" height="78" />
</node>
<node id="Stato_Terreno">
<name>Stato Terreno</name>
@@ -115,7 +134,7 @@
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>314 180 392 228</position>
<barchart active="true" width="128" height="78" />
<barchart width="128" height="78" />
</node>
<node id="Guasto">
<name>Guasto</name>
@@ -123,14 +142,30 @@
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>313 51 393 100</position>
<barchart active="true" width="128" height="78" />
<barchart width="128" height="78" />
</node>
<node id="Utilità">
<name>Utilità</name>
<interior color="e5f6f7" />
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>382 406 453 450</position>
<position>234 507 305 551</position>
<barchart width="128" height="64" />
</node>
<node id="Posizione_Finale">
<name>Posizione Finale</name>
<interior color="e5f6f7" />
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>625 289 701 335</position>
<barchart width="128" height="108" />
</node>
<node id="Utilità_2">
<name>Utilità Finale</name>
<interior color="e5f6f7" />
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>629 386 700 430</position>
<barchart width="128" height="64" />
</node>
</genie>

View File

@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- This network was created in GeNIe Academic, which can be used for academic teaching and research purposes only -->
<smile version="1.0" id="Malaria" numsamples="10000" discsamples="10000">
<nodes>
<cpt id="Infliuenza">
<state id="forte" />
<state id="leggera" />
<state id="assente" />
<probabilities>0.3333333333333333 0.3333333333333333 0.3333333333333334</probabilities>
<property id="DSL_OUTCOME_ORDER">-1</property>
</cpt>
<cpt id="Malaria">
<state id="presente" />
<state id="assente" />
<probabilities>0.5 0.5</probabilities>
<property id="DSL_OUTCOME_ORDER">-1</property>
</cpt>
<noisymax id="Febbre">
<state id="alta" />
<state id="bassa" />
<state id="assente" />
<parents>Infliuenza Malaria</parents>
<strengths>0 1 2 0 1</strengths>
<parameters>0.99 0.01 0 0.4 0.55 0.04999999999999993 0 0 1 0.999 0.001 0 0 0 1 0.01000000000000001 0.09999999999999998 0.89</parameters>
<property id="DSL_OUTCOME_ORDER">-1</property>
</noisymax>
</nodes>
<extensions>
<genie version="1.0" app="GeNIe 4.1.3402.0 ACADEMIC" name="Malaria">
<node id="Infliuenza">
<name>Infliuenza</name>
<interior color="e5f6f7" />
<outline color="000080" />
<font color="000000" name="Arial" size="14" />
<position>338 118 434 178</position>
<barchart active="true" width="179" height="104" />
</node>
<node id="Malaria">
<name>Malaria</name>
<interior color="e5f6f7" />
<outline color="000080" />
<font color="000000" name="Arial" size="14" />
<position>645 111 723 159</position>
<barchart active="true" width="177" height="78" />
</node>
<node id="Febbre">
<name>Febbre</name>
<interior color="e5f6f7" />
<outline color="000080" />
<font color="000000" name="Arial" size="14" />
<position>524 310 605 360</position>
<barchart active="true" width="183" height="110" />
</node>
</genie>
</extensions>
</smile>

View File

@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- This network was created in GeNIe Academic, which can be used for academic teaching and research purposes only -->
<smile version="1.0" id="WetGrass" numsamples="10000" discsamples="10000">
<nodes>
<cpt id="Cloudy">
<state id="Si" />
<state id="No" />
<probabilities>0.4 0.6</probabilities>
</cpt>
<cpt id="Rain">
<state id="Si" />
<state id="No" />
<parents>Cloudy</parents>
<probabilities>0.7000000000000001 0.3 0.4 0.6</probabilities>
</cpt>
<cpt id="Sprinkler">
<state id="On" />
<state id="Off" />
<parents>Cloudy</parents>
<probabilities>0.1 0.9 0.5 0.5</probabilities>
</cpt>
<cpt id="Wet_Grass">
<state id="Bagnata" />
<state id="Asciutta" />
<parents>Sprinkler Rain</parents>
<probabilities>0.99 0.01000000000000001 0.85 0.15 0.9 0.09999999999999998 0.1 0.9</probabilities>
</cpt>
</nodes>
<extensions>
<genie version="1.0" app="GeNIe 4.1.3402.0 ACADEMIC" name="WetGrass">
<node id="Cloudy">
<name>Cloudy</name>
<interior color="e5f6f7" />
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>189 22 271 83</position>
<barchart active="true" width="128" height="64" />
</node>
<node id="Rain">
<name>Rain</name>
<interior color="e5f6f7" />
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>337 157 412 213</position>
<barchart active="true" width="128" height="64" />
</node>
<node id="Sprinkler">
<name>Sprinkler</name>
<interior color="e5f6f7" />
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>43 156 134 222</position>
<barchart active="true" width="128" height="64" />
</node>
<node id="Wet_Grass">
<name>Wet Grass</name>
<interior color="e5f6f7" />
<outline color="000080" />
<font color="000000" name="Arial" size="8" />
<position>186 277 284 348</position>
<barchart active="true" width="128" height="64" />
</node>
</genie>
</extensions>
</smile>

View File

@@ -8,7 +8,7 @@ import smile.Network;
public class LWTest {
// 3% difference max (it is a lot but is fair)
// 5% difference max (it is a lot but is fair)
public static final float DELTA = 0.05f;
@Test
@@ -44,7 +44,7 @@ public class LWTest {
}
private void checkNodesValues(Network net) {
var lw = new LikelyhoodWeighting(net);
var lw = new LikelihoodWeighting(net);
net.updateBeliefs();
lw.updateNetwork(1000);