예제 #1
0
 /**
  * Assign Ic Probability ofr network n.
  *
  * @param n input network
  * @param w init weight
  */
 public static Network initParam(Network n, double w) {
   for (Long rid : n.getEdges()) {
     Relation r = n.getRelation(rid);
     r.setLtWeight(w);
   }
   return n;
 }
예제 #2
0
  public static Network initParamFromFile(Network n, String fileName) throws Exception {
    n = LtWeightInitiator.initParam(n, 0.0);

    BufferedReader br = new BufferedReader(new FileReader(fileName));
    String tmpLine = null;
    while (true) {
      tmpLine = br.readLine();
      if (tmpLine == null) break;
      String[] tmp = tmpLine.split("\t");
      Long from = Long.parseLong(tmp[0]);
      Long to = Long.parseLong(tmp[1]);
      double value = Double.parseDouble(tmp[2]);
      Long rid = n.findEdge(from, to);
      if (rid == null) continue;
      Relation r = n.getRelation(rid);
      r.setLtWeight(value);
    }
    br.close();

    /*Collection<LinkUnit> linkWeights = IOUtility.readLinkValue( fileName );
    for( LinkUnit linkunit : linkWeights ){
    	Integer from = linkunit.getFrom();
    	Integer to = linkunit.getTo();
    	double value = linkunit.getValue();
    	Integer rid = n.findEdge( from , to );
    	Relation r = n.getRelation( rid );
    	r.setLtWeight( value );
    }
    */
    return n;
  }
    public void runEvaluator() {
      int nSize = r.getNodeCount();
      nodeValues = new Double[nSize];
      allValues = new Double[nSize][];
      for (int i = 0; i < nSize; i++) {
        allValues[i] = new Double[nSize];
      }

      // get Oj's
      double[] o = new double[nSize];
      if (orgFile == null) {
        for (int i = 0; i < nSize; i++) {
          o[i] = 1.0;
        }
      } else {
        try {
          FileInputStream fis = new FileInputStream(orgFile);
          InputStreamReader isr = new InputStreamReader(fis);
          BufferedReader in = new BufferedReader(isr, 8096);
          String line = in.readLine();

          // try to guess delimiters
          String delim = " ";
          if (line.indexOf('\t') != 0) {
            delim = "\t";
          } else if (line.indexOf(',') != 0) {
            delim = ",";
          }
          Hashtable values = new Hashtable();
          while (line != null) {
            StringTokenizer st = new StringTokenizer(line, delim);
            line = in.readLine();
            String key = st.nextToken().trim();
            if (st.hasMoreTokens()) {
              try {
                Double d = Double.valueOf(st.nextToken());
                values.put(key, d);
              } catch (NumberFormatException nfe) {
                // ignore?
              }
            }
          }
          fis.close();
          TreeSet missingActors = new TreeSet();
          for (int i = 0; i < nSize; i++) {
            String actorName = r.getParent().getActor(i).getName();
            Double oValue = (Double) values.get(actorName);
            if (oValue == null) {
              missingActors.add(actorName);
              o[i] = 1.0;
            } else {
              o[i] = oValue.doubleValue();
            }
          }
          if (missingActors.size() > 0) {
            String errorMsg = "Couldn't find organization values for ";
            if (missingActors.size() <= 10) {
              Iterator iter = missingActors.iterator();
              errorMsg += "'" + iter.next() + "'";
              while (iter.hasNext()) {
                errorMsg += ", '" + iter.next() + "'";
              }
              errorMsg += ".";
            } else {
              errorMsg += missingActors.size() + " actors.";
            }
            JFrame f = new JFrame();
            JOptionPane.showMessageDialog(
                f, errorMsg, "Missing Values", JOptionPane.WARNING_MESSAGE);
          }
        } catch (IOException ioe) {
          Application.handleNonFatalThrowable(ioe);
        }
      }

      // i think i need to calculate all p_ij's
      double[][] p = new double[nSize][];
      for (int i = 0; i < nSize; i++) {
        p[i] = new double[nSize];
        double i_out = 0.0;
        for (int j = 0; j < nSize; j++) {
          if ((i != j) || (!ignoreDiagonals)) {
            if (investments == OUTBOUND || investments == BOTH) {
              i_out += r.getTieStrength(i, j);
            }
            if (investments == INBOUND || investments == BOTH) {
              i_out += r.getTieStrength(j, i);
            }
          }
        }

        for (int j = 0; j < nSize; j++) {
          p[i][j] = 0.0;
          if (i_out != 0.0) {
            if (investments == OUTBOUND || investments == BOTH) {
              p[i][j] += r.getTieStrength(i, j) / i_out;
            }
            if (investments == INBOUND || investments == BOTH) {
              p[i][j] += r.getTieStrength(j, i) / i_out;
            }
          }
        }
      }

      switch (type) {
          // implementation of Burt (1992: 64) equation 2.7
        case CONSTRAINT:
          for (int i = 0; i < nSize; i++) {
            double i_total = 0.0;
            allValues[i][i] = new Double(Double.NaN);
            for (int j = 0; j < nSize; j++) {
              if (i != j) {
                double c_sum = p[i][j];
                for (int q = 0; q < nSize; q++) {
                  if ((q != i) && (q != j)) {
                    c_sum += p[i][q] * p[q][j];
                  }
                }
                double c_ij = c_sum * c_sum * o[j];
                i_total += c_ij;
                allValues[i][j] = new Double(c_ij);
              }
            }
            nodeValues[i] = new Double(i_total);
          }
          break;

        case EFFECTIVE_SIZE:
          break;
      }
    }