Exemplo n.º 1
0
  public List<SPOTriplet> sentence2triplets(String sentence) {
    List<SPOTriplet> triplets = new ArrayList<SPOTriplet>();
    LinkGrammar.parse(sentence);
    int num_linkages = LinkGrammar.getNumLinkages();
    SPOTriplet triplet = new SPOTriplet();
    String alter_p = new String();
    Boolean ready = false;

    for (int l = 0; l < num_linkages; ++l) {

      LinkGrammar.makeLinkage(l);
      Linkage linkage = new Linkage();
      linkage.setLinkCost(LinkGrammar.getLinkageLinkCost());
      linkage.setNumViolations(LinkGrammar.getLinkageNumViolations());
      linkage.setDisjunctCost(LinkGrammar.getLinkageDisjunctCost());
      linkage.setLinkedWordCount(LinkGrammar.getNumWords());

      String[] disjuncts = new String[LinkGrammar.getNumWords()];
      String[] words = new String[LinkGrammar.getNumWords()];

      for (int k = 0; k < words.length; k++) {
        disjuncts[k] = LinkGrammar.getLinkageDisjunct(k);
        words[k] = LinkGrammar.getLinkageWord(k);
      }

      for (int k = 0; k < LinkGrammar.getNumLinks() && !ready; k++) {

        String c = LinkGrammar.getLinkLabel(k);
        if (c.charAt(0) == 'S') {
          triplet.p = LinkGrammar.getLinkageWord(k);
          alter_p = words[LinkGrammar.getLinkRWord(k)];
          triplet.s = words[LinkGrammar.getLinkLWord(k)];
        }

        if (c.charAt(0) == 'O') {
          triplet.o = words[LinkGrammar.getLinkRWord(k)];

          if (triplet.p.equals(words[LinkGrammar.getLinkRWord(k)])) {
            triplet.cut();
            triplets.add(triplet);
            ready = true;
            break;
          } else if (alter_p.equals(words[LinkGrammar.getLinkRWord(k)])) {
            triplet.p = alter_p;
            triplet.cut();
            triplets.add(triplet);
            ready = true;
            break;
          }
        }
      }
    }

    return triplets;
  }
Exemplo n.º 2
0
  SPOTriplet ThisWasAnOperator(SPOTriplet triplet, String prg, double image[]) {

    double reward = 3.0 * triplet.cmp(prev_action) - 1.5;

    if (prcps.get(triplet) == null) {
      prcps.put(triplet, new Perceptron(3, 256 * 256, 80, 1));
    }

    SPOTriplet action = triplet;

    if (prev_reward > -java.lang.Double.MAX_VALUE && frqs.get(prev_action) != null) {
      frqs.get(prev_action).put(prev_state, frqs.get(prev_action).get(prev_state) + 1);

      double max_ap_q_sp_ap = max_ap_Q_sp_ap(image);

      for (int z = 0; z < 10; ++z) {
        double nn_q_s_a = (prcps.get(prev_action)).ThisWasAnOperator(prev_image);

        double q_q_s_a =
            nn_q_s_a
                + alpha(frqs.get(prev_action).get(prev_state))
                    * (reward + gamma * max_ap_q_sp_ap - nn_q_s_a);

        prcps.get(prev_action).learning(prev_image, q_q_s_a, nn_q_s_a);

        System.out.println("### " + (q_q_s_a - nn_q_s_a) + " " + q_q_s_a + " " + nn_q_s_a);
      }

      action = argmax_ap_f(prg, image);
    }

    prev_state = prg;
    prev_reward = reward;
    prev_action = action;
    prev_image = Arrays.copyOf(image, image.length);

    return action;
  }