TrustGrapher  r52
A playabale simulator for modelling trust between agents
D:/src/cu/repsystestbed/algorithms/EigenTrust.java
Go to the documentation of this file.
00001 package cu.repsystestbed.algorithms;
00002 
00003 import java.util.ArrayList;
00004 import java.util.Set;
00005 
00006 import org.apache.log4j.Logger;
00007 
00008 import cu.repsystestbed.data.Feedback;
00009 import cu.repsystestbed.entities.Agent;
00010 import cu.repsystestbed.graphs.FeedbackHistoryGraphEdge;
00011 import Jama.Matrix;
00012 
00013 public class EigenTrust extends ReputationAlgorithm
00014 {
00015 
00016 
00017 
00018         private int iterations;
00019         private Matrix trustScores;
00020         private double threshold2Satisfy;
00021         private double[][] cijMatrix;
00022         private boolean matrixFilled;
00023         
00024         static Logger logger = Logger.getLogger(EigenTrust.class.getName());
00025         
00026         public EigenTrust()
00027         {
00028                 super.setGlobal(true);
00029         }
00030         
00035         public EigenTrust(int iterations, double threshold2Satisfy)
00036         {
00037                 this.iterations = iterations;
00038                 this.threshold2Satisfy = threshold2Satisfy;
00039                 super.setGlobal(true);
00040         }
00041         
00042         public void setMatrixFilled(boolean filled){
00043             matrixFilled = filled;
00044         }
00045 
00046         
00047         @Override
00048         public double calculateTrustScore(Agent src, Agent sink) throws Exception
00049         {
00050                 
00051                 if(super.feedbackHistoryGraph==null || super.feedbackHistoryGraph.vertexSet().size()==0 
00052                                 || super.feedbackHistoryGraph.edgeSet().size()==0)
00053                 {
00054                         throw new Exception("Feedback History graph not initialized");
00055                 }
00056                 
00057                 if(this.iterations<1) throw new Exception("Number of iterations is less than 1");
00058 
00071                 if(!this.matrixFilled) 
00072                 {
00073                         int numVertices = super.feedbackHistoryGraph.vertexSet().size();
00074                         cijMatrix = new double[numVertices][numVertices];
00075                         
00076                         Set<Agent> agents = super.feedbackHistoryGraph.vertexSet();
00077                         
00078                         for(Agent source : agents)
00079                         {
00080                                 Set<FeedbackHistoryGraphEdge> allOutgoingEdges = super.feedbackHistoryGraph.outgoingEdgesOf(source); 
00081                                 
00082                                 //fill up cij matrix
00083                                 for(FeedbackHistoryGraphEdge edge : allOutgoingEdges)
00084                                 {
00085                                         ArrayList<Feedback> feedbacks = edge.feedbacks;
00086                                         double sij=0;
00087                                         for(Feedback feedback : feedbacks)
00088                                         {
00089                                                 if(feedback.value >= threshold2Satisfy) sij++;
00090                                                 else sij--;
00091                                         }
00092                                         
00093                                         if(sij<1) sij=0;
00094                                         Agent sinkTemp = (Agent)edge.sink;
00095                                         cijMatrix[source.id][sinkTemp.id] = sij;                
00096                                 }
00097                         }
00098                         
00099 //                      logger.info("cijMatrix before normalization = " + this.printMatrix(cijMatrix));
00100                         
00101                         //normalize cij matrix
00102                         for(int i=0;i<numVertices;i++)
00103                         {
00104                                 //row by row normalization
00105                                 double total = 0;
00106                                 for(int j=0;j<numVertices;j++)
00107                                 {
00108                                         total = total + cijMatrix[i][j];
00109                                 }
00110                                 for(int j=0;j<numVertices;j++)
00111                                 {
00112                                         if(total>0) cijMatrix[i][j] = cijMatrix[i][j] / total;
00113                                         //else cijMatrix[i][j]=0; //don't divide by 0
00114                                         
00115                                         //agent i doesnt trust anyone. make it trust everyone equally.
00116                                         else cijMatrix[i][j]=1.0/(double)numVertices;
00117                                 }
00118                         }
00119                 
00120 //                      logger.info("cijMatrix after normalization = " +  this.printMatrix(cijMatrix));
00121                         
00122                         //transpose and multiplication
00123                         trustScores = new Matrix(cijMatrix);
00124                         Matrix trans = trustScores.transpose();
00125                         
00126                         Matrix orig = new Matrix(trans.getArray());
00127 
00128                         double tempScore = 1.0/(double)numVertices;
00129                         double[][] tk = new double[numVertices][numVertices];
00130                         for(int i=0;i<numVertices;i++)
00131                         {
00132                                 for(int j=0;j<numVertices;j++) tk[i][j] = tempScore;
00133                         }
00134                         Matrix tkMatrix = new Matrix(tk);
00135                         
00136                         double[][] p = new double[numVertices][numVertices];
00137                         for(int i=0;i<numVertices;i++)
00138                         {
00139                                 for(int j=0;j<numVertices;j++) p[i][j] = tempScore;
00140                         }
00141                         Matrix pMatrix = new Matrix(p);
00142                         
00143                         double a=0.2; //TODO make this configurable
00144                         Matrix tkplus1Matrix = null;
00145                         
00146                         
00147                         for(int i=0;i<this.iterations;i++)
00148                         {
00149                                 tkplus1Matrix = trans.times(tkMatrix);
00150                                 tkplus1Matrix = tkplus1Matrix.times(1-a);
00151                                 tkplus1Matrix = tkplus1Matrix.plus(pMatrix.times(a));
00152                                 tkMatrix = new Matrix(tkplus1Matrix.getArrayCopy());
00153 //                              System.out.println(printMatrix(tkplus1Matrix.getArray()));
00154 //                              System.out.println("row=" + tkplus1Matrix.getRowDimension() + " column="+tkplus1Matrix.getColumnDimension());
00155                         }
00156                         
00157                         this.trustScores = tkMatrix;
00158 //                      for(int i=0;i<this.iterations;i++) trans = orig.times(trans);
00159 //                      trustScores = trans.transpose(); //re-transpose
00160 //                      
00161                         this.matrixFilled = true;
00162 
00163 //                      logger.info("cijMatrix after multiplying " + this.iterations + " times = " +  this.printMatrix(trustScores.getArray()));
00164                         
00165                 }       
00166                 
00167                 return trustScores.getArray()[sink.id][0];
00168                 
00169         }
00170 
00171         public int getIterations()
00172         {
00173                 return iterations;
00174         }
00175 
00176         public void setIterations(int iterations)
00177         {
00178                 this.iterations = iterations;
00179         }
00180 
00181 
00182         public double getThreshold2Satisfy()
00183         {
00184                 return threshold2Satisfy;
00185         }
00186 
00187 
00188         public void setThreshold2Satisfy(double threshold2Satisfy)
00189         {
00190                 this.threshold2Satisfy = threshold2Satisfy;
00191         }
00192         
00193         public String printMatrix(double[][] mat)
00194         {
00195                 String output = "\n";
00196                 for(int i=0;i<mat.length;i++)
00197                 {
00198                         for(int j=0;j<mat[i].length;j++)
00199                         {
00200                                 output = output + (mat[i][j] + " ");
00201                         }
00202                         output = output + "\n";
00203                 }
00204                 return output;
00205 
00206         }
00207 
00208 }