/** * 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(); } }
private void addNumberVar(NumberVar var) { Type type = var.pop().type(); Integer value = restNumberVars.get(type); if (value == null) { value = 1; } else { value = value + 1; } HashSet<POP> pops = restPOPs.get(type); pops.remove(var.pop()); restNumberVars.put(type, value); uninstVars.add(var); }
/** * 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; }