Ejemplo n.º 1
0
  /**
   * compute the feasible region based on the evidence, the method will only use the v2 if 1. v1 is
   * a direct parent of v2, or 2. v1 and v2 are the same, i.e. corresponding to evidence variable
   *
   * @param v1 the variable to sample
   * @param v2 the variable corresponding to evidence
   * @return
   */
  private Region computeRegionFromEvidence(BayesNetVar v1, BayesNetVar v2) {
    if (v1.equals(v2)) {
      // the variable is in the evidence, it should be directly set to evidence
      // value
      return new SingletonRegion(evidence.getObservedValue(v2));
    }
    if (v2 instanceof DerivedVar) {
      ArgSpec as = ((DerivedVar) v2).getArgSpec();
      if (as instanceof CardinalitySpec) {
        // child is cardinality of a set
        ImplicitSetSpec iss = ((CardinalitySpec) as).getSetSpec();
        Type childType = iss.getType();
        if (v1 instanceof NumberVar) {
          // parent is number variable, potential match
          NumberVar numv1 = (NumberVar) v1;
          POP parentPOP = numv1.pop();
          Type parentType = parentPOP.type();
          if (!parentType.equals(childType)) {
            return null;
          } else {
            // potential match, if
            Object[] objs = numv1.args(); // generating objects for parent
            // variable
            if (objs.length == 0) {
              // no any constraint
              return Region.FULL_REGION;
            } else {
              // v1 is parent of v2
              Formula cond = iss.getCond();
              if (cond.isDetermined(this) && cond.isTrue(this)) {
                int value = ((Number) evidence.getObservedValue(v2)).intValue();
                // get all previously satisfied var
                Set<BayesNetVar> pvars = getAlreadySampledParentVars(v2);
                for (BayesNetVar pv : pvars) {
                  // eliminate the already sampled ones
                  int a = ((Number) this.getValue(pv)).intValue();
                  value -= a;
                }
                // v1 is parent of v2, and will be sampled immediately
                pvars.add(v1);
                if ((value > 0) && anyMoreNumberVar(childType, cond)) {
                  return new IntRegion(0, value);
                } else {
                  return new SingletonRegion(value);
                }
              }
            }
          }
        } /* TODO check other cases of v1 that can be potential parent of this */

      } else {
        // other cases of argspec in v1
      }

    } else {
      // other variables
      // might be parent
      // TODO check whether the condition has v1 as parent
    }
    return null;
  }