예제 #1
0
파일: magic.py 프로젝트: rasata/ProbLog
def runproblogsampling(s, n=5, output='html'):
    model = PrologString(s)
    samples = plsample.sample(model, n=n, tuples=True)
    result = ''
    for sample in samples:
        result += ','.join(
            str(Term(query[0], *query[1:-1])) for query in sample) + '<br/>'
    if output == 'html':
        return '<pre>{}</pre>'.format(result)
    return result
예제 #2
0
def runproblogsampling(s, n=5, output="html"):
    model = PrologString(s)
    samples = plsample.sample(model, n=n, tuples=True)
    result = ""
    for sample in samples:
        result += (
            ",".join(str(Term(query[0], *query[1:-1])) for query in sample) + "<br/>"
        )
    if output == "html":
        return "<pre>{}</pre>".format(result)
    return result
def create_interpretations(p, n):
        result = sample.sample(p, n, format='dict')
        resultNew  = []
        for i in range(n):
                interpr = next(result)
                toRemove = []
                for j in interpr:
                        if random.uniform(0, 1) <= 0.3:
                                toRemove.append(j)
                for j in toRemove:
                        dict.pop(interpr, j)
                resultNew.append(interpr)
        return resultNew
def generate_dataset(model, size, missing_rate):
    datapoints = []
    result = sample.sample(model, n=size, format='dict')
    for s in result:
        datapoints.append(s)
    output = pd.DataFrame(datapoints)
    output = output.applymap(lambda x: 1 if x == True else 0)
    # applying missing rate
    if missing_rate != 0:
        # transform values greater than 1 to percentage
        if missing_rate >= 1:
            missing_rate = float(missing_rate) / 100.
        # missing values are represented as NaN
        output = output.applymap(lambda x: x
                                 if random() > missing_rate else float('NaN'))
    return output
예제 #5
0
def missing_sample(modeltext, missing_rate):
    """Returns a relational observation sampled from modeltext (which should be a structure in
    ProbLog syntax).
    """
    model = PrologString(modeltext)
    result = sample.sample(model, n=1, format='dict')
    output = ""
    for s in result:
        for q in s:
            value = s[q]
            if random() > missing_rate / 100:
                if value == True:
                    output += "evidence({},true).\n".format(str(q))
                else:
                    output += "evidence({},false).\n".format(str(q))
            else:
                output += "evidence({},none).\n".format(str(q))
    return output
예제 #6
0
def generate_interpretations(problog_filename, m):
    pl = PrologFile(problog_filename)
    interpretations = []

    for interpretation in sample(pl, n=m, as_evidence=True):
        temp = []

        # drop each observation with a probability of 30%
        for i in interpretation.splitlines():
            if random.random() <= 0.7:
                temp.append(i)

        interpretations.append("\n".join(temp))

    interpretations_file = os.path.abspath(
        os.path.join(os.path.dirname(__file__), "files",
                     "interpretations.txt"))
    with open(interpretations_file, "w") as out:
        sep = os.linesep + separator_1 + os.linesep
        out.write(sep.join(interpretations))

    return interpretations
예제 #7
0
node(n4, 5, [n1]).
node(n5, 5, [n2]).
node(n6, 5, [n2]).

link(n1,n3,40,5). % link(source node, target node, latency, bw)
link(n1,n2,10,5).
link(n1,n4,10,5).
link(n5,n2,10,5).
link(n6,n2,10,5).

route(s1, path(n5, n1, [n5, n2]), 45, 100).
route(s1, path(n3, n1, [n3]), 45, 20).
route(s1, path(n6, n1, [n6, n2]), 45, 20).
route(s2, path(n4, n4, []), 45, 20).

query(nop(s1)).



"""

# result = get_evaluatable().create_from(PrologString(model)).evaluate()

model = PrologString(modeltext)
result2 = get_evaluatable().create_from(model).evaluate()
print(result2)

result = sample.sample(model, n=3, format='dict')
print(result)

# https: // github.com / yuce / pyswip / issues / 48
    my_uniform(0,10)::a.
    0.5::b.
    c :- value(a, A), A >= 3; b.
    query(a).
    query(b).
    query(c).
"""

modeltext_new = """
    0.5::b.
    c :- value(a, A), A >= 3; b.
    query(a).
    query(b).
    query(c).
"""



s = SimpleProgram()
unif = Term('my_uniform')(Constant(0),Constant(10))
a = Term('a', p=unif)
s += a
for clause in PrologString(modeltext_new):
    s += clause

model = PrologString(modeltext)
# Pass the mapping between name and function using the distributions parameter.
x = list(PrologString(modeltext).__iter__())
x2 = list(s.__iter__())
result = list(sample.sample(model, n=3, format='dict', distributions={'my_uniform': integer_uniform}))
result2 = list(sample.sample(s, n=3, format='dict', distributions={'my_uniform': integer_uniform}))
예제 #9
0
def sample_model(model_text):
    model = PrologString(model_text)
    result = sample.sample(model, n=10, format='dict')
    for i in result:
        print(i)
예제 #10
0
def get_samples(modeltext, sample_size):
    model = PrologString(modeltext)
    result = sample.sample(model, n=sample_size, format='dict')
    return result