Exemplo n.º 1
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--log", dest="loglevel", default="INFO")
    parser.add_argument("--test_file", default="experiments/data/test1.txt")
    parser.add_argument("--max_trace_length", default=5)
    parser.add_argument("--min_trace_length", default=3)

    args, unknown = parser.parse_known_args()
    """
    traces is 
     - list of different recorded values (traces)
     - each trace is a list of recordings at time units (time points)
     - each time point is a list of variable values (x1,..., xk) 
    """

    numeric_level = args.loglevel.upper()
    logging.basicConfig(level=numeric_level)

    max_trace_length = int(args.max_trace_length)
    min_trace_length = int(args.min_trace_length)

    with open(args.test_file) as test_file:
        for line in test_file:
            [f_1_string, f_2_string] = line.split(";")

            f_1 = Formula.convertTextToFormula(f_1_string)
            f_2 = Formula.convertTextToFormula(f_2_string)

            disambiguate(f_1, f_2, min_trace_length, max_trace_length, 1)
Exemplo n.º 2
0
def get_path(f,
             wall_locations,
             water_locations,
             robot_location,
             items_locations=None):
    f = Formula.convertTextToFormula(f)

    for tr in itertools.chain(
            range(constants.MIN_FINE_RANGE, constants.MAX_FINE_RANGE,
                  constants.STEP_FINE_RANGE),
            range(constants.MIN_COARSE_RANGE, constants.MAX_COARSE_RANGE,
                  constants.STEP_COARSE_RANGE)):

        disambiguation = get_finite_witness(f=f,
                                            wall_locations=wall_locations,
                                            trace_length=tr,
                                            water_locations=water_locations,
                                            robot_position=robot_location,
                                            items_locations=items_locations)

        if disambiguation == "unsat":
            continue
        else:
            (disambiguation_example, init_world, path) = disambiguation
            return path
    return False
Exemplo n.º 3
0
def generateFromFormulaFile(files,
                            outputFolder,
                            equalNumber,
                            traceLengths,
                            repetitions,
                            counter=0):
    for fileName in files:
        with open(fileName) as fileWithFormulas:
            for line in fileWithFormulas:
                f = Formula.convertTextToFormula(line)
                for minRep in repetitions:
                    for traceLength in traceLengths:
                        generatedTraces = testFileGeneration.generateTracesFromFormula(
                            f,
                            traceLength,
                            minRep,
                            minRep,
                            50 * minRep,
                            generateExactNumberOfTraces=equalNumber)
                        patternName = fileName.split("/")[-1]
                        patternName = patternName.split(".")[0]
                        testName = outputFolder + "{:04}.trace".format(counter)

                        if len(generatedTraces.acceptedTraces) > 0 and len(
                                generatedTraces.rejectedTraces) > 0:
                            generatedTraces.writeTracesToFile(testName)
                        counter += 1
    def readTracesFromStream(self, stream):

        readingMode = 0

        operators = None
        for line in stream:

            if '---' in line:
                readingMode += 1
            else:
                if readingMode == 0:

                    trace = lineToTrace(line)
                    trace.intendedEvaluation = True

                    self.acceptedTraces.append(trace)

                elif readingMode == 1:
                    trace = lineToTrace(line)
                    trace.intendedEvaluation = False
                    self.rejectedTraces.append(trace)

                elif readingMode == 2:
                    operators = [s.strip() for s in line.split(',')]

                elif readingMode == 3:
                    self.depthOfSolution = int(line)
                elif readingMode == 4:
                    possibleSolution = line.strip()
                    if possibleSolution.lower() == "none":
                        self.possibleSolution = None
                    else:
                        self.possibleSolution = Formula.convertTextToFormula(
                            possibleSolution)

                else:
                    break
        if operators == None:
            self.operators = defaultOperators
        else:
            self.operators = operators

        self.maxLengthOfTraces = 0
        for trace in self.acceptedTraces + self.rejectedTraces:
            if trace.lengthOfTrace > self.maxLengthOfTraces:
                self.maxLengthOfTraces = trace.lengthOfTrace

        # an assumption that number of variables is the same across all the traces
        try:
            self.numVariables = self.acceptedTraces[0].numVariables
            self.literals = self.acceptedTraces[0].literals
        except:
            self.literals = self.rejectedTraces[0].literals
            self.numVariables = self.rejectedTraces[0].numVariables
        for trace in self.acceptedTraces + self.rejectedTraces:
            if trace.numVariables != self.numVariables:
                raise Exception("wrong number of variables")
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--log", dest="loglevel", default="INFO")
    parser.add_argument("--candidate_formula_1", default="F(a)")
    parser.add_argument("--candidate_formula_2", default="F(&(a,b))")
    parser.add_argument("--trace_length", default = 5)

    args, unknown = parser.parse_known_args()

    """
    traces is 
     - list of different recorded values (traces)
     - each trace is a list of recordings at time units (time points)
     - each time point is a list of variable values (x1,..., xk) 
    """

    numeric_level = args.loglevel.upper()
    logging.basicConfig(level=numeric_level)

    trace_length = int(args.trace_length)

    f_1 = Formula.convertTextToFormula(args.candidate_formula_1)
    f_2 = Formula.convertTextToFormula(args.candidate_formula_2)
    difference_formula = Formula([encodingConstants.LOR,
                                 Formula([encodingConstants.LAND, f_1, Formula([encodingConstants.LNOT, f_2])]),
                                 Formula([encodingConstants.LAND, Formula([encodingConstants.LNOT, f_1]), f_2])
                                  ])

    #difference_formula = Formula(["&", f_1, Formula([encodingConstants.LNOT, f_2])])
    #difference_formula = Formula(["&", f_2, Formula([encodingConstants.LNOT, f_1])])

    # it is weird trace_length = trace_length +1 --> a mistake in the encoding that was reasons about number of states, rather than number of actions
    counterexample_witness = get_finite_witness(f=difference_formula, trace_length=trace_length)

    if counterexample_witness == "unsat":
        logging.error("Formulas {} and {} are equivalent (at least for all traces of length {}".format(f_1, f_2, trace_length))
    else:



        if counterexample_witness.evaluateFormulaOnTrace(difference_formula) == False:
            raise RuntimeError("looking for witness of satisfiability, but got a trace {} that is not a model for {}".format(cex, difference_formula))

        logging.info("the distinguishing trace is {}".format(counterexample_witness))
Exemplo n.º 6
0
def debug_disambiguation():
    candidates = json.loads(request.args.get("candidates"))
    context = json.loads(request.args.get("context"))
    world = World(context, json_type=2)
    wall_locations = world.get_wall_locations()
    converted_candidates = [
        Formula.convertTextToFormula(c) for c in candidates
    ]
    status, disambiguation_world, disambiguation_path, candidate_1, candidate_2, considered_candidates, disambiguation_trace = create_disambiguation_example(
        converted_candidates, wall_locations=wall_locations)
    logging.debug("status is {}, disambiguation path is {}".format(
        status, disambiguation_path))
    logging.debug("disambiguation world is {}".format(disambiguation_world))
    answer = {}
    answer["status"] = status
    answer["path"] = disambiguation_path
    return answer
Exemplo n.º 7
0
def findDTDepth(fileName):
    overallSubs = []
    overallDepth = 0
    with open(fileName) as dtFile:

        for line in dtFile:
            fText = (line.split(':')[0]).strip()
            if fText == '*':
                continue

            else:
                overallDepth += 1
                obtainedFormula = Formula.convertTextToFormula(fText)
                subs = obtainedFormula.getSetOfSubformulas()

                overallSubs += subs
    overallSubs = set(overallSubs)
    overallDepth += len(overallSubs)
    overallDepth -= 1
    print(overallDepth, overallSubs)
    return overallDepth
Exemplo n.º 8
0
def generateFromFormulaFile(files, outputFolder, equalNumber, traceLengths, repetitions, numFiles, counter, finiteTraces, misclassificationRate):
    for fileName in files:
        with open(fileName) as fileWithFormulas:
            counter=0
            for line in fileWithFormulas:
                f = Formula.convertTextToFormula(line)
                print("Generating traces for Formula", str(f))
                
                for minRep in repetitions:
                    for traceLength in traceLengths:
                        num=0
                        while num<numFiles:
                            generatedTraces = testFileGeneration.generateTracesFromFormula(f, traceLength, minRep, minRep, 100*minRep, finiteTraces=finiteTraces, misclassificationRate=misclassificationRate)
                            patternName = fileName.split("/")[-1]
                            patternName = patternName.split(".")[0]
                            if not os.path.exists(outputFolder+'/'+patternName):
                                os.makedirs(outputFolder+'/'+patternName)
                            testName = outputFolder+patternName+'/'+"{:04}.trace".format(counter)
                            if len(generatedTraces.acceptedTraces) > 0 and len(generatedTraces.rejectedTraces) > 0:
                                generatedTraces.writeTraces(testName)
                                counter += 1
                                num+=1
Exemplo n.º 9
0
def main_compile_json(args):
    import json
    from utils import datas
    from utils.SimpleTree import Formula, DecisionTreeFormula

    output_file = args.output_file
    if output_file == "-": output_file = sys.stdout

    headers = []
    expressions = []
    for column in args.columns:
        split = column.split(':', 1)
        if len(split) == 1: header = expr = split[0]
        else: header, expr = split
        headers.append(header.strip())
        expressions.append(expr)

    traces_file_subst = []
    for subst in args.traces_file_subst:
        src, dst = subst.split(':')
        traces_file_subst.append(
            (os.path.realpath(src), os.path.realpath(dst)))

    if isinstance(output_file, str):
        cm = open(output_file, "w")
        # else: cm = contextlib.nullcontext(output_file)
    else:
        cm = contextlib.contextmanager(lambda: (yield output_file))()
    with cm as f1:
        writer = csv.writer(f1)
        if args.with_header:
            writer.writerow(headers)

        #Reading all the json files in the folder
        input_files = []
        for root, dirs, files in os.walk(args.inputs_folder):
            for file in files:
                if not file.endswith(args.traces_ext): continue
                input_files.append(os.path.join(root, file))

        constants = dict(
            SAT="SAT",
            MaxSAT="MaxSAT",
            SATDT="SAT-DT",
            SAT_DT="SAT-DT",
            MaxSATDT="MaxSAT-DT",
            MaxSAT_DT="MaxSAT-DT",
        )

        def eval_expr(expr, record, ignore_exceptions=False):
            try:
                return eval(expr, dict(record))
            except Exception as err:
                if not ignore_exceptions:
                    msg = f"while evaluating {expr!r} on {input_file}"
                    raise RuntimeError(msg) from err

        #Collating the results
        processed = []
        for input_file in input_files:
            try:
                with open(input_file, 'r') as f2:
                    record = json.load(f2, object_hook=datas.Data)

                # pre_compute filter for optim purpose
                if any(not eval_expr(expr, record, ignore_exceptions=True)
                       for expr in args.filters):
                    continue

                # load additional variables
                for key in {'traces', 'run', 'result'}:
                    record.setdefault(key, datas.Data())
                for key in {'sample', 'formula', 'decisionTree'}:
                    record.setdefault(key, None)
                if record.traces.filename:
                    record.traces = datas.json_traces_file(record.traces,
                                                           level=datas.FULL)
                if record.traces.traces:
                    record.sample = record.traces.traces
                if record.result.formula:
                    record.formula = Formula.convertTextToFormula(
                        record.result.formula)
                if record.result.decisionTree:
                    record.decisionTree = DecisionTreeFormula.convertTextToFormula(
                        record.result.decisionTree)
                for key in {
                        'result.misclassification', 'algo.args.minScore',
                        'run.time'
                }:
                    if not isinstance(record[key], float): continue
                    record[key] = numpy.round(record[key], decimals=15)

                # filtering traces
                if any(not eval_expr(expr, record) for expr in args.filters):
                    continue

                # trace file substitution
                for src, dst in traces_file_subst:
                    trc = os.path.realpath(record.traces.filename)
                    if os.path.commonpath([trc, src]) != src: continue
                    trc = os.path.join(dst, os.path.relpath(trc, start=src))
                    if not os.path.isabs(record.traces.filename):
                        trc = os.path.relpath(trc)
                    record.traces = datas.json_traces_file(dict(filename=trc),
                                                           level=datas.FULL)
                    record.sample = record.traces.traces
                    if record.formula is not None:
                        record.result.misclassification = 1 - record.traces.traces.get_score(
                            record.formula, score='count')
                        record.result.misclassification = numpy.round(
                            record.result.misclassification, decimals=15)
                    break
                if record.traces.traces:
                    record.sample = record.traces.traces
                for key in {
                        'result.misclassification', 'algo.args.minScore',
                        'run.time'
                }:
                    if not isinstance(record[key], float): continue
                    record[key] = numpy.round(record[key], decimals=15)

                values = [eval_expr(expr, record) for expr in expressions]
                # values = ["" if value is None else value for value in values]
                # values = [str(value) for value in values]
                writer.writerow(values)
            except Exception as err:
                msg = f"while handling {input_file}"
                raise RuntimeError(msg) from err
            processed.append(input_file)
        print(f"{len(processed)} result files compiled.", file=sys.stderr)
Exemplo n.º 10
0
def findSizeOfTextFormula(formulaText):
    try:
        formula = Formula.convertTextToFormula(formulaText)
        return len(formula.getSetOfSubformulas())
    except:
        return "/"
Exemplo n.º 11
0
def user_decision_update():
    try:

        t = TicToc()
        if constants.TESTING:
            num_disambiguations = 0
            disambiguation_stats = []

        decision = request.args.get("decision")
        sessionId = request.args.get("sessionId")

        candidates = json.loads(request.args.get("candidates"))

        path = json.loads(request.args.get("path"))

        context = json.loads(request.args.get("context"))
        world = World(context, json_type=2)
        wall_locations = world.get_wall_locations()
        actions = json.loads(request.args.get("actions"))
        updated_candidates, updated_formulas = update_candidates(
            candidates, path, decision, world, actions)

        answer = {}
        answer["sessionId"] = sessionId
        answer["candidates"] = updated_candidates
        answer["formatted_candidates"] = [
            str(f.reFormat()) for f in updated_formulas
        ]
        if len(updated_candidates) == 0:
            answer["status"] = constants.FAILED_CANDIDATES_GENERATION_STATUS
            return answer
        elif len(updated_candidates) == 1:

            answer["status"] = "ok"
            if constants.TESTING:
                answer["num_disambiguations"] = num_disambiguations
                answer["disambiguation_stats"] = disambiguation_stats
            return answer
        elif len(updated_candidates) > 1:
            answer["status"] = "indoubt"

            converted_candidates = [
                Formula.convertTextToFormula(c) for c in updated_candidates
            ]

            t = TicToc()
            t.tic()
            status, disambiguation_world, disambiguation_path, candidate_1, candidate_2, considered_candidates, disambiguation_trace = create_disambiguation_example(
                converted_candidates, wall_locations=wall_locations)
            disambiguation_time = t.tocvalue()
            if constants.TESTING:
                num_disambiguations += 1
                disambiguation_stats.append(disambiguation_time)
            if not status == "indoubt":
                answer["status"] = status
                if status == "ok":

                    answer["candidates"] = [str(candidate_1)]
                    answer["formatted_candidates"] = [
                        str(candidate_1.reFormat())
                    ]
                else:
                    answer["candidates"] = []
                    answer["formatted_candidates"] = []
                if constants.TESTING:
                    answer["num_disambiguations"] = num_disambiguations
                    answer["disambiguation_stats"] = disambiguation_stats
                return answer
            else:

                answer["world"] = disambiguation_world.export_as_json()
                formatted_path = convert_path_to_formatted_path(
                    disambiguation_path, disambiguation_world)
                answer["path"] = formatted_path
                answer["disambiguation-candidate-1"] = str(candidate_1)
                answer["disambiguation-candidate-2"] = str(candidate_2)
                answer["candidates"] = [str(f) for f in considered_candidates]
                answer["formatted_candidates"] = [
                    str(f.reFormat()) for f in considered_candidates
                ]
                answer["actions"] = disambiguation_path

                if constants.TESTING:
                    answer["num_disambiguations"] = num_disambiguations
                    answer["disambiguation_stats"] = disambiguation_stats

                return answer
    except Exception as e:
        error_log.error("exception {}".format(e))
        return (str(e), 500)