Esempio n. 1
0
def sumOutVariable(factor, variable):
    """
    Sum out a variable from a factor
    """
    newVars = [x for x in factor.variables if x != variable]
    varIndex = factor.variables.index(variable)
    newF = Factor(newVars, mnCards)
    
    if debug: newF.printF()
    
    usedVar = [False for x in range(factor.size)]
    
    for i in range(newF.size):
        newF.phi.append(0)
        
        start = 0
        for k in range(factor.size):
            if usedVar[k] == False:
                start = k
                break
        
        for j in range(factor.card[varIndex]):
            key = start + factor.stride[varIndex] * j
            newF.phi[i] += factor.phi[key]
            usedVar[key] = True

        if debug: print "phi: ", i, " ", newF.phi[i]

    return newF
Esempio n. 2
0
def sumOutVariable(factor, variable):
    """
    Sum out a variable for variable elimination
    """
    if debug: print "eliminating factor"

    newVars = [x for x in factor.variables if x != variable]

    if debug: print "new vars: ", newVars

    newF = Factor(newVars, mnCards)

    if len(newVars) > 0:
        varIndex = factor.variables.index(variable)
        if debug: 
            print "var index: ", varIndex
            newF.printF()

        usedVar = [False for x in range(factor.size)]

        if debug: 
            print "used var: ", usedVar
            print "stride: ", factor.stride[varIndex], ", card: ", factor.card[varIndex]

        psi = []
        for i in range(newF.size):
            psi.append(0)
    
            start = 0
            for k in range(factor.size):
                if usedVar[k] == False:
                    start = k
                    break
    
            for j in range(factor.card[varIndex]):
                if debug: print "start: ", start, " stride: ", factor.stride[varIndex], " j: ", j
                psi[i] += factor.phi[start + factor.stride[varIndex] * j]
                usedVar[start + factor.stride[varIndex] * j] = True

            if debug: print "psi: ", i, " ", psi[i]

        newF.phi = psi[:]

    return newF