public Action policy(State observation, float reward) {

    Map<Float, Action> options = new HashMap<Float, Action>();
    scala.collection.Iterator<Action> it = actions().iterator();
    while (it.hasNext()) {
      Action action = it.next();
      options.put(expectedReward(observation, action), action);
    }

    Action bestGuess = options.get(new TreeSet<Float>(options.keySet()).last());

    Action decision = bestGuess;

    if (random.nextDouble() < randomFactor) {
      decision = randomAction();
    }

    Situation currentSA = new Situation(observation, decision);

    float currentExpected = expectedReward(observation, decision);

    float lastExpected = expectedReward(lastSA.getKey(), lastSA.getValue());

    float newQValue = lastExpected + alpha * (reward + gamma * currentExpected - lastExpected);

    qTable.put(lastSA, newQValue); // update the Q-Table
    lastSA = currentSA;

    return decision;
  }
Example #2
0
    @SuppressWarnings("unchecked")
    private ModuleConstructor(
        List<Consumer<NTree<Class<?>>>> verification,
        BiConsumer<S, S> addMethod,
        Class<S> moduleClass) {
      this.verification = new ArrayList<>(verification);
      moduleGeneration =
          NTree.traverseUp(
              (L, l) -> {
                // parent object -- instance of multi module.
                S multiModule = l;

                // children instance of S, child module.
                Iterator<S> it = L.iterator();

                while (it.hasNext()) {
                  S m = it.next();
                  addMethod.accept(multiModule, m);
                }

                return multiModule;
              },
              (Class<?> l) -> {
                S re = null;
                try {
                  re = (S) l.newInstance();
                } catch (IllegalAccessException | InstantiationException e) {
                  reflectError("Exception in creating class " + l, e);
                } catch (ClassCastException v) {
                  reflectError(
                      "Class " + l + " is not an instance of the module class " + moduleClass);
                }
                return re;
              });
    }
 public static void addModels(final Class<?> clazz, final Map<String, Model> models) {
   scala.collection.immutable.List<Model> modelOption = ModelConverters.readAll(clazz);
   scala.collection.Iterator<Model> iterator = modelOption.iterator();
   while (iterator.hasNext()) {
     Model model = iterator.next();
     if (!isIgnorableModel(model.name())) {
       models.put(model.name(), model);
     }
   }
 }
 /**
  * @param message
  * @return
  */
 private MessageModel parse(Message message, String er7Message) {
   MessageElement root = new MessageElement();
   List<SegOrGroup> children = message.children();
   if (children != null && !children.isEmpty()) {
     scala.collection.Iterator<SegOrGroup> it = children.iterator();
     while (it.hasNext()) {
       process(it.next(), "", root);
     }
   }
   return new MessageModel(root.getChildren(), getDelimeters(er7Message));
 }
  /**
   * @param f : field
   * @param parent : parent
   */
  private void process(Field f, MessageElement parent) {
    Location loc = f.location();
    Req req = f.req();
    Range card = Util.getOption(req.cardinality());
    String rep = f.toString();
    MessageElementData data =
        new MessageElementData(
            loc.uidPath(),
            loc.desc(),
            req.usage().toString(),
            card.min(),
            card.max(),
            loc.line(),
            loc.column(),
            -1,
            f.position(),
            f.instance(),
            null,
            FIELD);

    MessageElement el = new MessageElement(NODE_FIELD, data, parent);
    if (f instanceof SimpleField) {
      SimpleField s = (SimpleField) f;
      MessageElementData value =
          new MessageElementData(
              loc.uidPath(),
              loc.desc(),
              req.usage().toString(),
              card.min(),
              card.max(),
              loc.line(),
              loc.column(),
              -1,
              f.position(),
              f.instance(),
              s.value().raw(),
              FIELD);
      new MessageElement("value", value, el);
    } else {
      ComplexField c = (ComplexField) f;
      List<Component> children = c.children();
      if (children != null && !children.isEmpty()) {
        Iterator<Component> it = children.iterator();
        while (it.hasNext()) {
          process(it.next(), el);
        }
      }
    }
  }
 /**
  * @param c
  * @param parent
  */
 private void process(Component c, MessageElement parent) {
   Location loc = c.location();
   Req req = c.req();
   MessageElementData data =
       new MessageElementData(
           loc.uidPath(),
           loc.desc(),
           req.usage().toString(),
           -1,
           null,
           loc.line(),
           loc.column(),
           -1,
           c.position(),
           c.instance(),
           null,
           COMPONENT);
   MessageElement el = new MessageElement(NODE_COMPONENT, data, parent);
   if (c instanceof SimpleComponent) {
     SimpleComponent s = (SimpleComponent) c;
     MessageElementData value =
         new MessageElementData(
             loc.uidPath(),
             loc.desc(),
             req.usage().toString(),
             -1,
             null,
             loc.line(),
             loc.column(),
             -1,
             s.position(),
             c.instance(),
             s.value().raw(),
             COMPONENT);
     new MessageElement("value", value, el);
   } else {
     ComplexComponent cc = (ComplexComponent) c;
     List<SimpleComponent> children = cc.children();
     if (children != null && !children.isEmpty()) {
       Iterator<SimpleComponent> it = children.iterator();
       while (it.hasNext()) {
         process(it.next(), el);
       }
     }
   }
 }
  @Override
  public Future<Seq<Seq<Span>>> getTraces(QueryRequest input) {
    io.zipkin.QueryRequest.Builder request =
        new io.zipkin.QueryRequest.Builder()
            .serviceName(input.serviceName())
            .spanName(input.spanName().isDefined() ? input.spanName().get() : null)
            .endTs(input.endTs())
            .limit(input.limit());

    for (Iterator<String> i = input.annotations().iterator(); i.hasNext(); ) {
      request.addAnnotation(i.next());
    }

    for (Iterator<Tuple2<String, String>> i = input.binaryAnnotations().iterator(); i.hasNext(); ) {
      Tuple2<String, String> keyValue = i.next();
      request.addBinaryAnnotation(keyValue._1(), keyValue._2());
    }
    return toSeqFuture(this.spanStore.getTraces(request.build()));
  }
  /**
   * @param e
   * @param parentName
   * @param parent
   */
  private void process(SegOrGroup e, String parentName, MessageElement parent) {
    if (e == null) {
      return;
    }
    if (e instanceof Segment) {
      Segment s = (Segment) e;
      Location loc = s.location();
      Req req = s.req();
      Range card = Util.getOption(req.cardinality());
      MessageElementData data =
          new MessageElementData(
              loc.uidPath(),
              loc.desc(),
              req.usage().toString(),
              card.min(),
              card.max(),
              loc.line(),
              loc.column(),
              -1,
              s.position(),
              s.instance(),
              null,
              SEGMENT);
      MessageElement el = new MessageElement(NODE_SEGMENT, data, parent);
      List<Field> children = s.children();
      if (children != null && !children.isEmpty()) {
        Iterator<Field> it = children.iterator();
        while (it.hasNext()) {
          process(it.next(), el);
        }
      }

    } else if (e instanceof Group) {
      Group g = (Group) e;
      List<SegOrGroup> children = g.children();
      if (children != null && !children.isEmpty()) {
        scala.collection.Iterator<SegOrGroup> it = children.iterator();
        while (it.hasNext()) {
          process(it.next(), "", parent);
        }
      }
    }
  }