package MyPackage;
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.text.DecimalFormat;
import javax.swing.JTable;
import org.jgraph.JGraph;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphLayoutCache;
import org.jgraph.graph.GraphModel;
public class ArbreResultat {
public String[][] matrice;
float[] prob;
float[] profit;
float[][]payoff;
float[][] probfav;
PayoffTableModel model;
int i,j;
int cols,rows,actions,outcome,nbpayoff;
float flo,maxprofit;
float pfav=0;
float pdefav=0;
float[] fav;
float[] maxcols;
float parfaite;
float[][] choix3;
float[] decision3;
float expert;
ArbreResultat(JTable Table)
{
cols = Table.getModel().getColumnCount();
rows = Table.getModel().getRowCount();
matrice=new String[rows][cols];
prob=new float[cols];
payoff=new float[rows][cols];
probfav=new float[5][cols];
profit=new float [rows];
//r馗up駻ation des donn馥s saisie
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
matrice[i][j]=(String) Table.getModel().getValueAt(i, j);
}
}
//remplissage du tableau de prob initiale
for(j=0;j<cols-1;j++)
{
try {
flo = Float.parseFloat(matrice[0][j+1]);
prob[j]=flo;
} catch (NumberFormatException n) {
System.out.println("\nC弾st pas un float ce que t誕s saisis");
}
}
//remplissage du tableau des payoff
for(i=0;i<rows-2;i++)
{
for(j=0;j<cols-1;j++)
{
try {
flo = Float.parseFloat(matrice[i+2][j+1]);
payoff[i][j]=flo;
} catch (NumberFormatException n) {
System.out.println("\nC弾st pas un float ce que t誕s saisis");
}
}
}
//remplissage du tableau du profit
for(i=0;i<rows-2;i++)
{
flo=0;
for(j=0;j<cols-1;j++)
{
flo=flo+payoff[i][j]*prob[j];
}
profit[i]=flo;
}
//calcul de max profit
maxprofit=profit[0];
for(i=1;i<rows;i++)
{
if(profit[i]>profit[0])
maxprofit=profit[i];
}
//remplissage du tableau de prob fav/defav
fav=new float[2];
for(j=0;j<cols-1;j++)
{
try {
flo = Float.parseFloat(matrice[1][j+1]);
probfav[0][j]=flo;
pfav=pfav+(probfav[0][j]*prob[j]);
} catch (NumberFormatException n) {
System.out.println("\nC弾st pas un float ce que t誕s saisis");
}
probfav[1][j]=1-probfav[0][j];
pdefav=pdefav+(probfav[1][j]*prob[j]);
}
fav[0]=pfav;
fav[1]=pdefav;
for(j=0;j<cols-1;j++)
{
probfav[2][j]=probfav[0][j]*prob[j]/pfav;
probfav[3][j]=probfav[1][j]*prob[j]/pdefav;
}
//Calcul de l'information parfaite
maxcols=new float[cols-1];
float max2;
parfaite=0;
for(j=0;j<cols-1;j++)
{ max2=payoff[0][j];
for(i=1;i<rows-2;i++){
if(payoff[i][j]>max2)
max2=payoff[i][j];
}
maxcols[j]=max2;
parfaite=parfaite+maxcols[j]*prob[j];
}
//calcul des valeurs des noeuds choix3
choix3=new float[2][rows-2];
float sommefav=0;
float sommedefav=0;
for(j=0;j<rows-2;j++)
{
for(i=0;i<cols-1;i++){
sommefav=sommefav+(probfav[2][i]*payoff[j][i]);
sommedefav=sommedefav+(probfav[3][i]*payoff[j][i]);
}
choix3[0][j]=sommefav;
choix3[1][j]=sommedefav;
sommefav=0;
sommedefav=0;
}
//Calcul de l'information expert
decision3= new float[2];
float max3;
expert=0;
for(j=0;j<2;j++)
{ max3=choix3[j][0];
for(i=1;i<rows-2;i++){
if(choix3[j][i]>max3)
max3=choix3[j][i];
}
decision3[j]=max3;
expert=expert+decision3[j]*fav[j];
}
}
public JGraph getGraph()
{
//D馗larations des variables n馗essaires
actions=rows-2;
outcome=cols-1;
int height=20;
int i,j,k;
DecimalFormat df = new DecimalFormat("0.00");
//D馗laration du graphe
GraphModel model = new DefaultGraphModel();
GraphLayoutCache view = new GraphLayoutCache(model,new DefaultCellViewFactory());
JGraph graph = new JGraph(model, view);
/*----------------------C'est la partie uncertaine de larbre-------------------------------*/
//D馗laration des variables n馗essaires
String max=String.valueOf(df.format(maxprofit));
//Construction des noeud n馗essaires
/*Construction du noeud principale*/
DefaultGraphCell[] cellsdecision1 = new DefaultGraphCell[1];
DefaultPort[] portdecision1 = new DefaultPort[actions];
cellsdecision1[0] = new DefaultGraphCell(new String(max));
GraphConstants.setBounds(cellsdecision1[0].getAttributes(), new Rectangle2D.Double(200,200,60,20));
GraphConstants.setGradientColor(cellsdecision1[0].getAttributes(),Color.ORANGE);
GraphConstants.setOpaque(cellsdecision1[0].getAttributes(), true);
for(i=0;i<actions;i++){
portdecision1[i] = new DefaultPort();
cellsdecision1[0].add(portdecision1[i]);
}
System.out.println("cell0"+cellsdecision1[0].getChildCount());
graph.getGraphLayoutCache().insert(cellsdecision1);
/*Construction des noeud de choix*/
nbpayoff=actions*outcome;
DefaultGraphCell[] cellschoix1 = new DefaultGraphCell[actions];
DefaultPort[] portchoix1 = new DefaultPort[nbpayoff+actions];
int x=0;
for(i=0;i<actions;i++)
{
try{
cellschoix1[i] = new DefaultGraphCell(new String(String.valueOf(df.format(profit[i]))));
GraphConstants.setBounds(cellschoix1[i].getAttributes(), new Rectangle2D.Double(600,height,40,20));
GraphConstants.setGradientColor(cellschoix1[i].getAttributes(),Color.ORANGE);
GraphConstants.setOpaque(cellschoix1[i].getAttributes(), true);
for(j=0;j<=outcome;j++){
portchoix1[x] = new DefaultPort();
cellschoix1[i].add(portchoix1[x]);
x=x+1;
height=height+80;
}
}catch(Exception e){e.printStackTrace();}}
graph.getGraphLayoutCache().insert(cellschoix1);
//Cr饌tion des feuilles
DefaultGraphCell[] cellsfeuille1 = new DefaultGraphCell[(actions*outcome)];
DefaultPort[] portfeuille1 = new DefaultPort[actions*outcome];
int height1=1;
int Y=0;
for(k=0;k<actions;k++)
{
for(j=0;j<outcome;j++)
{try{
cellsfeuille1[Y] = new DefaultGraphCell(new String(String.valueOf(payoff[k][j])));
GraphConstants.setBounds(cellsfeuille1[Y].getAttributes(), new Rectangle2D.Double(800,height1,40,20));
GraphConstants.setGradientColor(cellsfeuille1[Y].getAttributes(),Color.green);
GraphConstants.setOpaque(cellsfeuille1[Y].getAttributes(), true);
portfeuille1[Y] = new DefaultPort();
cellsfeuille1[Y].add(portfeuille1[Y]);
Y=Y+1;
height1=height1+40;
}catch(Exception e){e.printStackTrace();}}}
graph.getGraphLayoutCache().insert(cellsfeuille1);
//Construction des liens de choix
DefaultGraphCell[] cellsedgechoix1 = new DefaultGraphCell[actions];
DefaultEdge edge[] = new DefaultEdge[actions];
x=0;
for(i=0;i<actions;i++)
{
edge[i] = new DefaultEdge(new String("Action"+(i+1)));
edge[i].setSource(cellsdecision1[0].getChildAt(0));
edge[i].setTarget(cellschoix1[i].getChildAt(x));
cellsedgechoix1[i] = edge[i];
int arrow = GraphConstants.ARROW_CLASSIC;
GraphConstants.setLineEnd(edge[i].getAttributes(), arrow);
GraphConstants.setEndFill(edge[i].getAttributes(), true);
GraphConstants.setLabelAlongEdge(edge[i].getAttributes(), true);
x=x+i;
}
graph.getGraphLayoutCache().insert(cellsedgechoix1);
//Construction de lien de feuille
DefaultGraphCell[] cellsedgefeuille1 = new DefaultGraphCell[actions*outcome];
DefaultEdge edgefeuille[] = new DefaultEdge[actions*outcome];
评论0