예제 #1
0
  /**
   * set the value of the variable will potentially add new random variables into the possible world
   * if this variable is number variable
   *
   * @param var
   * @param value
   */
  public void setValue(VarWithDistrib var, Object value) {
    super.setValue(var, value);

    if (var instanceof NumberVar) {
      int varDepth = getVarDepth(var);
      NumberVar nv = (NumberVar) var;
      if ((depthBound < 0) || (varDepth < depthBound)) {
        if (getVarDepth(var) >= maxInt) {
          // We're creating non-guaranteed objects of greater depth,
          // so increase maxInt as well
          increaseMaxInt();
        }

        // Add objects generated by this number variable
        addObjects(nv.pop().type(), getSatisfiers(nv));
      }
      Type ty = nv.pop().type();
      Integer t = restNumberVars.get(ty) - 1;
      restNumberVars.put(ty, t);
    }

    if (var != lastVar) {
      Util.debug("Fatal error: last variable sampled is not the same as var");
    } else {
      lastIter.remove();
    }
  }
예제 #2
0
  /**
   * Increases the maximum magnitude of integers (and natural numbers) in the object lists by 1.
   * Adds any basic RVs that use the newly added integers to the uninstantiated variables list.
   *
   * <p>Note that no variables will be added if integers don't serve as arguments to basic RVs in
   * this model, or if they only occur in argument lists along with types that happen to have an
   * empty extension in this world. In either case, if increasing maxInt by 1 doesn't yield new
   * variables, then further increases won't do anything either. So there's no point in calling this
   * method in a loop.
   */
  void increaseMaxInt() {
    if (intsAreArgs && ((intBound < 0) || (maxInt < intBound))) {

      ++maxInt;

      if (objectsByType.containsKey(BuiltInTypes.NATURAL_NUM)) {
        addObjects(BuiltInTypes.NATURAL_NUM, Collections.singleton(new Integer(maxInt)));
      }

      if (objectsByType.containsKey(BuiltInTypes.INTEGER)) {
        List newInts = new ArrayList();
        newInts.add(new Integer(maxInt));
        newInts.add(new Integer(-maxInt));
        addObjects(BuiltInTypes.INTEGER, newInts);
      }
    }
  }
예제 #3
0
  /** add all random variables without parents into the uninstantiated variables */
  protected void init() {

    // added number variables for those number statement without origin
    // functions
    for (Type generatedType : model.getTypes()) {
      Collection<POP> pops = generatedType.getPOPs();

      // set initial size of unused number statements for each type
      restPOPs.put(generatedType, new HashSet<POP>(pops));

      for (POP pop : pops) {
        for (int i = 0; i < pop.getArgTypes().length; ++i) {
          objectsByType.put(pop.getArgTypes()[i], new ArrayList());
        }

        if (pop.getArgTypes().length == 0) {
          addNumberVar(new NumberVar(pop, Collections.EMPTY_LIST));
        }
      }
    }

    // Determine what types serve as arguments to basic RVs. Initialize
    // their object lists to be empty. As we're doing this, add any
    // random variables with empty arg lists to the list of uninstantiated
    // RVs.

    for (Function f : model.getFunctions()) {
      if (f instanceof RandomFunction) {
        for (int i = 0; i < f.getArgTypes().length; ++i) {
          objectsByType.put(f.getArgTypes()[i], new ArrayList());
        }

        if (f.getArgTypes().length == 0) {
          uninstVars.add(new RandFuncAppVar((RandomFunction) f, Collections.EMPTY_LIST));
        }
      }
    }

    // add skolem constants defined in symbol evidence
    // already added in step 1
    // for (SkolemConstant c : evidence.getSkolemConstants()) {
    // uninstVars.add(new RandFuncAppVar(c, Collections.EMPTY_LIST));
    // }

    // Create initial object lists for those types. While doing so,
    // add uninstantiated variables that have these objects as arguments.
    for (Type type : objectsByType.keySet()) {
      if (type.isSubtypeOf(BuiltInTypes.INTEGER)) {
        addObjects(type, Collections.singleton(new Integer(0)));
        intsAreArgs = true;
      } else if (type == BuiltInTypes.BOOLEAN) {
        addObjects(type, type.getGuaranteedObjects());
      } else if (type.isBuiltIn()) {
        Util.fatalError("Illegal argument type for random function: " + type, false);
      } else {
        // user-defined type
        addObjects(type, type.getGuaranteedObjects());
      }
    }

    // set the iterator
    lastIter = uninstVars.iterator();
  }