Esempio n. 1
0
  public void searchStep(int i) {
    double oldScore = network.logLikelihood() - scorer.graphScore(network);
    GraphStep step = stepper.step(network.graph);

    step.forward(network.graph);
    network.learnCPDs();
    double newScore = network.logLikelihood() - scorer.graphScore(network);

    double ratio = Math.exp(newScore - oldScore);
    // double ratio = newScore / oldScore;

    boolean accepted = true;

    if (ratio < 1.0) {
      double p = rand.nextDouble();

      if (walkBack && p <= 1.0 - ratio) {
        step.reverse(network.graph);
        network.learnCPDs();
        // System.out.println("\tRejected.");
        accepted = false;
      }
    }

    if (accepted) {
      System.out.println(String.format("Step %d: %s", i, step.toString()));
      if (ratio < 1.0) {
        System.out.println(String.format("\tRejection Ratio: %.4f", ratio));
      }
    }
  }
Esempio n. 2
0
 @JRubyMethod(name = "pub_key")
 public synchronized IRubyObject get_pub_key() {
   DSAPublicKey key;
   BigInteger param;
   if ((key = this.pubKey) != null) {
     return BN.newBN(getRuntime(), key.getY());
   } else if (specValues != null) {
     if ((param = specValues[SPEC_Y]) != null) {
       return BN.newBN(getRuntime(), param);
     }
   }
   return getRuntime().getNil();
 }
Esempio n. 3
0
 @JRubyMethod(name = "g")
 public synchronized IRubyObject get_g() {
   // FIXME: return only for public?
   DSAKey key;
   BigInteger param;
   if ((key = this.pubKey) != null || (key = this.privKey) != null) {
     if ((param = key.getParams().getG()) != null) {
       return BN.newBN(getRuntime(), param);
     }
   } else if (specValues != null) {
     if ((param = specValues[SPEC_G]) != null) {
       return BN.newBN(getRuntime(), param);
     }
   }
   return getRuntime().getNil();
 }
Esempio n. 4
0
 public BNSearch(BN bn, GraphStepper st, BNModelScore sc) {
   rand = new Random();
   this.network = bn;
   stepper = st;
   scorer = sc;
   network.learnCPDs();
   walkBack = true;
 }
Esempio n. 5
0
 @JRubyMethod(name = "priv_key")
 public synchronized IRubyObject get_priv_key() {
   DSAPrivateKey key;
   BigInteger param;
   if ((key = this.privKey) != null) {
     return BN.newBN(getRuntime(), key.getX());
   }
   return getRuntime().getNil();
 }
Esempio n. 6
0
  private IRubyObject setKeySpecComponent(int index, IRubyObject value) {
    BigInteger[] vals;
    // illegal to set if we already have a key for this component
    // FIXME: allow changes after keys are created? MRI doesn't prevent it...
    if (this.pubKey != null
        || this.privKey != null
        || (vals = this.specValues) != null && vals[index] != null) {
      throw newDSAError(getRuntime(), "illegal modification");
    }
    // get the BigInteger value
    BigInteger bival = BN.getBigInteger(value);

    if (vals != null) {
      // we already have some vals stored, store this one, too
      vals[index] = bival;
      // check to see if we have all values yet
      for (int i = vals.length; --i >= 0; ) {
        if (vals[i] == null) {
          // still missing components, return
          return value;
        }
      }
      // we now have all components. create the key.
      DSAPublicKeySpec spec =
          new DSAPublicKeySpec(vals[SPEC_Y], vals[SPEC_P], vals[SPEC_Q], vals[SPEC_G]);
      try {
        this.pubKey = (DSAPublicKey) KeyFactory.getInstance("DSA").generatePublic(spec);
      } catch (InvalidKeySpecException e) {
        throw newDSAError(getRuntime(), "invalid keyspec");
      } catch (NoSuchAlgorithmException e) {
        throw newDSAError(getRuntime(), "unsupported key algorithm (DSA)");
      }
      // clear out the specValues
      this.specValues = null;

    } else {

      // first value received, save
      this.specValues = new BigInteger[4];
      this.specValues[index] = bival;
    }
    return value;
  }