Commit 6112d7b4 authored by Dumoulin Nicolas's avatar Dumoulin Nicolas
Browse files

bufixes and opinions initialized at random

parent 96d89097
No related merge requests found
Showing with 43 additions and 16 deletions
+43 -16
...@@ -39,12 +39,20 @@ public class Individual { ...@@ -39,12 +39,20 @@ public class Individual {
Arrays.fill(influences, 0); Arrays.fill(influences, 0);
} }
public Individual(int id, int popSize, double[] individualsOpinions, double[] subjectOpinions) {
this.id = id;
this.individualsOpinions = individualsOpinions;
this.subjectOpinions = subjectOpinions;
influences = new double[popSize];
Arrays.fill(influences, 0);
}
public double getOpinionOnIndividual(int ind) { public double getOpinionOnIndividual(int ind) {
return individualsOpinions[ind]; return individualsOpinions[ind];
} }
public double getOpinionOnSubject(int subject) { public double getOpinionOnSubject(int subject) {
return individualsOpinions[subject]; return subjectOpinions[subject];
} }
public void computeInfluences(Parameters parameters) { public void computeInfluences(Parameters parameters) {
...@@ -60,7 +68,7 @@ public class Individual { ...@@ -60,7 +68,7 @@ public class Individual {
public void argumentationImpact(Parameters parameters, Individual fromInd) { public void argumentationImpact(Parameters parameters, Individual fromInd) {
for (int k = 0; k < parameters.getNbSubject(); k++) { for (int k = 0; k < parameters.getNbSubject(); k++) {
subjectOpinions[k] += influences[fromInd.id] * parameters.getRho() * (subjectOpinions[fromInd.id] - subjectOpinions[id]); subjectOpinions[k] += influences[fromInd.id] * parameters.getRho() * (fromInd.subjectOpinions[k] - subjectOpinions[k]);
} }
} }
......
...@@ -42,7 +42,17 @@ public class Model { ...@@ -42,7 +42,17 @@ public class Model {
this.random = new Random(); this.random = new Random();
random.resetNextSubstream(parameters.getSeedIndex()); random.resetNextSubstream(parameters.getSeedIndex());
this.individuals = new Individual[parameters.getPopSize()]; this.individuals = new Individual[parameters.getPopSize()];
Arrays.setAll(individuals, i -> new Individual(i, parameters.getPopSize(), parameters.getNbSubject())); for (int i = 0; i < individuals.length; i++) {
double[] individualsOpinions = new double[parameters.getPopSize()];
for (int j = 0; j < individualsOpinions.length; j++) {
individualsOpinions[j] = random.nextDouble()*10-1;
}
double[] subjectOpinions = new double[parameters.getNbSubject()];
for (int j = 0; j < subjectOpinions.length; j++) {
subjectOpinions[j] = random.nextDouble()*2-1;
}
individuals[i] = new Individual(i, parameters.getPopSize(), individualsOpinions, subjectOpinions);
}
couples = new ArrayList<>(); couples = new ArrayList<>();
for (int i = 0; i < individuals.length; i++) { for (int i = 0; i < individuals.length; i++) {
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {
...@@ -107,7 +117,6 @@ public class Model { ...@@ -107,7 +117,6 @@ public class Model {
} }
public static void main(String[] args) throws ProcessingException, IllegalArgumentException, IllegalAccessException { public static void main(String[] args) throws ProcessingException, IllegalArgumentException, IllegalAccessException {
Parameters parameters = new Parameters(0, 50, 1, new DistorsionFromOpinions(), 1, 0.3); Parameters parameters = new Parameters(0, 50, 1, new DistorsionFromOpinions(), 1, 0.3);
Model model = new Model(parameters); Model model = new Model(parameters);
parameters.init(model); parameters.init(model);
......
...@@ -38,6 +38,14 @@ public class Random { ...@@ -38,6 +38,14 @@ public class Random {
return randomStream.nextInt(i, i1); return randomStream.nextInt(i, i1);
} }
/**
* This method returns a double picked out following a Uniform law between 0
* and 1 with 0 and 1 excluded (can't be picked out)
*/
public double nextDouble() {
return randomStream.nextDouble();
}
/** /**
* This method return a random order for a sequence of leng numbers. * This method return a random order for a sequence of leng numbers.
*/ */
......
...@@ -21,6 +21,7 @@ import fr.irstea.associatione.model.Parameters; ...@@ -21,6 +21,7 @@ import fr.irstea.associatione.model.Parameters;
import fr.irstea.associatione.model.ProcessingException; import fr.irstea.associatione.model.ProcessingException;
import fr.irstea.associatione.model.distorsion.DistorsionFromOpinions; import fr.irstea.associatione.model.distorsion.DistorsionFromOpinions;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.util.Arrays;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JPanel; import javax.swing.JPanel;
import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFactory;
...@@ -60,9 +61,9 @@ public class GUI { ...@@ -60,9 +61,9 @@ public class GUI {
opinionsMeanOnSubjects.addSeries(new XYSeries(i)); opinionsMeanOnSubjects.addSeries(new XYSeries(i));
} }
chartsPanel.removeAll(); chartsPanel.removeAll();
credibilityChartPanel = new ChartPanel(createHistogram(model.getCredibility())); credibilityChartPanel = new ChartPanel(createHistogram("Credibility", model.getCredibility()));
chartsPanel.add(credibilityChartPanel); chartsPanel.add(credibilityChartPanel);
chartsPanel.add(new ChartPanel(ChartFactory.createXYLineChart("Opinions means on subjects", "timesteps", "opinions", opinionsMeanOnSubjects, PlotOrientation.VERTICAL, true, true, false))); chartsPanel.add(new ChartPanel(ChartFactory.createXYLineChart("Opinions means on subjects", "timesteps", "opinions", opinionsMeanOnSubjects, PlotOrientation.VERTICAL, false, true, false)));
} }
public void init(Parameters parameters) throws ProcessingException, IllegalArgumentException, IllegalAccessException { public void init(Parameters parameters) throws ProcessingException, IllegalArgumentException, IllegalAccessException {
...@@ -71,7 +72,8 @@ public class GUI { ...@@ -71,7 +72,8 @@ public class GUI {
initCharts(); initCharts();
frame.pack(); frame.pack();
frame.setVisible(true); frame.setVisible(true);
for (int i = 0; i < 500; i++) { updatePlots(model.getTimestep());
for (int i = 0; i < 50; i++) {
model.iter(); model.iter();
updatePlots(model.getTimestep()); updatePlots(model.getTimestep());
} }
...@@ -81,26 +83,26 @@ public class GUI { ...@@ -81,26 +83,26 @@ public class GUI {
double[] data = new double[]{0.3, 0.3, 0.5, 0.6, 0.65}; double[] data = new double[]{0.3, 0.3, 0.5, 0.6, 0.65};
JFrame f = new JFrame("test"); JFrame f = new JFrame("test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new ChartPanel(createHistogram(data))); f.setContentPane(new ChartPanel(createHistogram("Credibility", data)));
f.pack(); f.pack();
f.setVisible(true); f.setVisible(true);
} }
public void updatePlots(int timestep) { public void updatePlots(int timestep) {
credibilityChartPanel.setChart(createHistogram(model.getCredibility())); credibilityChartPanel.setChart(createHistogram("Credibility", model.getCredibility()));
for (int i = 0; i < model.getParameters().getPopSize(); i++) { double[] data = model.getOpinionsMeanOnSubjects();
double[] data = model.getOpinionsMeanOnSubjects(); for (int i = 0; i < data.length; i++) {
opinionsMeanOnSubjects.getSeries(i).add(timestep, data[i]); opinionsMeanOnSubjects.getSeries(i).add(timestep, data[i]);
} }
} }
public static JFreeChart createHistogram(double[] data) { public static JFreeChart createHistogram(String title, double[] data) {
HistogramDataset dataset = new HistogramDataset(); HistogramDataset dataset = new HistogramDataset();
dataset.setType(HistogramType.SCALE_AREA_TO_1); dataset.setType(HistogramType.SCALE_AREA_TO_1);
dataset.addSeries("Histogram", data, 30); dataset.addSeries(title, data, 30);
String plotTitle = "Histogram"; String plotTitle = title;
String xaxis = "number"; String xaxis = "opinions";
String yaxis = "value"; String yaxis = "amount";
PlotOrientation orientation = PlotOrientation.VERTICAL; PlotOrientation orientation = PlotOrientation.VERTICAL;
boolean show = false; boolean show = false;
boolean toolTips = false; boolean toolTips = false;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment