예제 #1
0
def test_get_middle_component():
    h1 = HyperRectangle(string_to_interval("[-10,10]", float))
    h2 = HyperRectangle(string_to_interval("[-5,5]", float))
    hlist = h1._setminus(h2, 0)
    assert hlist[0] == HyperRectangle(string_to_interval("[-10,-5)",float)) and \
            hlist[1] == HyperRectangle(string_to_interval("(5,10]", float)) and \
            hlist[2] == HyperRectangle(string_to_interval("(-5, 5)", float))
예제 #2
0
def test_interval_parsing():
    int1 = string_to_interval("(2,5)", int)
    assert int1.left_bound() == 2
    assert int1.left_bound_type() == BoundType.open
    assert int1.right_bound() == 5
    assert int1.right_bound_type() == BoundType.open
    assert str(int1) == "(2,5)"
    int1 = string_to_interval("(2,7]", int)
    assert int1.left_bound() == 2
    assert int1.left_bound_type() == BoundType.open
    assert int1.right_bound() == 7
    assert int1.right_bound_type() == BoundType.closed
    assert str(int1) == "(2,7]"
예제 #3
0
def test_region_string():
    h2 = HyperRectangle(string_to_interval("[2,5]", pc.Rational),
                        string_to_interval("[3,6]", pc.Rational))
    variables = [pc.Variable("x"), pc.Variable("y")]
    h3 = HyperRectangle.from_region_string(h2.to_region_string(variables),
                                           variables)
    assert h2 == h3
    h4 = HyperRectangle(string_to_interval("(2,5]", pc.Rational),
                        string_to_interval("[3,6]", pc.Rational))
    variables = [pc.Variable("x"), pc.Variable("y")]
    h5 = HyperRectangle.from_region_string(h4.to_region_string(variables),
                                           variables)
    assert h4 == h5
    assert h4 != h3
    h6 = HyperRectangle(string_to_interval("[2,5)", pc.Rational),
                        string_to_interval("(3,6)", pc.Rational))
    variables = [pc.Variable("x"), pc.Variable("y")]
    h7 = HyperRectangle.from_region_string(h6.to_region_string(variables),
                                           variables)
    assert h6 == h7
    assert h6 != h3
    assert h6 != h5
예제 #4
0
def test_setminus_hyperrectangles():
    h1 = HyperRectangle(string_to_interval("[0,1]", float),
                        string_to_interval("[0,1]", float))
    h2 = HyperRectangle(string_to_interval("[0.2,0.75]", float),
                        string_to_interval("[0.3,0.6]", float))
    hlist = h1.setminus(h2)
    assert hlist[0] == HyperRectangle(string_to_interval("[0.0,0.2)",float), string_to_interval("[0.0,1.0]", float)) and \
            hlist[1] == HyperRectangle(string_to_interval("(0.75, 1.0]",float), string_to_interval("[0.0,1.0]",float)) and \
            hlist[2] == HyperRectangle(string_to_interval("(0.2,0.75)",float), string_to_interval("[0.0,0.3)",float)) and \
            hlist[3] == HyperRectangle(string_to_interval("(0.2,0.75)",float), string_to_interval("(0.6, 1.0]", float))
예제 #5
0
def test_interval_setminus():
    i1 = string_to_interval("[-10,10]", float)  # the closed universe

    # check the cases:
    # left from the interval - should be empty
    i = string_to_interval("[-20,-15]", float)
    intersection = i1.setminus(i)
    assert i1 == intersection[0]
    i = string_to_interval("(-20,-15)", float)
    intersection = i1.setminus(i)
    assert i1 == intersection[0]
    i = string_to_interval("[-20,-15)", float)
    intersection = i1.setminus(i)
    assert i1 == intersection[0]
    i = string_to_interval("(-20,-15]", float)
    intersection = i1.setminus(i)
    assert i1 == intersection[0]

    # right from the interval -should be empty
    i = string_to_interval("[15,20]", float)
    intersection = i1.setminus(i)
    assert i1 == intersection[0]
    i = string_to_interval("[15,20)", float)
    intersection = i1.setminus(i)
    assert i1 == intersection[0]
    i = string_to_interval("(15,20]", float)
    intersection = i1.setminus(i)
    assert i1 == intersection[0]
    i = string_to_interval("(15,20)", float)
    intersection = i1.setminus(i)
    assert i1 == intersection[0]

    # left from the interval ending at the lower bound
    i = string_to_interval("[-20,-10]", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("(-10,10]", float)
    i = string_to_interval("(-20,-10)", float)
    intersection = i1.setminus(i)
    assert i1 == intersection[0]
    i = string_to_interval("[-20,-10)", float)
    intersection = i1.setminus(i)
    assert i1 == intersection[0]
    i = string_to_interval("(-20,-10]", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("(-10,10]", float)

    # right from the interval ending at the lower bound
    i = string_to_interval("[10,15]", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("[-10,10)", float)
    i = string_to_interval("(10,15)", float)
    intersection = i1.setminus(i)
    assert i1 == intersection[0]
    i = string_to_interval("[10,15)", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("[-10,10)", float)
    i = string_to_interval("(10,15]", float)
    intersection = i1.setminus(i)
    assert i1 == intersection[0]

    # left from the interval ending in the interval
    i = string_to_interval("[-20,0]", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("(0,10]", float)
    i = string_to_interval("(-20,0)", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("[0,10]", float)
    i = string_to_interval("[-20,0)", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("[0,10]", float)
    i = string_to_interval("(-20,0]", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("(0,10]", float)

    # right from the interval ending in the interval
    i = string_to_interval("[0,15]", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("[-10,0)", float)
    i = string_to_interval("(0,15)", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("[-10,0]", float)
    i = string_to_interval("[0,15)", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("[-10,0)", float)
    i = string_to_interval("(0,15]", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("[-10,0]", float)

    # left from the interval ending at the upper bound
    i = string_to_interval("[-20,10]", float)
    intersection = i1.setminus(i)
    assert not intersection
    i = string_to_interval("(-20,10)", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("[10,10]", float)
    i = string_to_interval("[-20,10)", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("[10,10]", float)
    i = string_to_interval("(-20,10]", float)
    intersection = i1.setminus(i)
    assert not intersection

    # right from the interval ending at the lower bound
    i = string_to_interval("[-10,15]", float)
    intersection = i1.setminus(i)
    assert not intersection
    i = string_to_interval("(-10,15)", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("[-10,-10]", float)
    i = string_to_interval("[-10,15)", float)
    intersection = i1.setminus(i)
    assert not intersection
    i = string_to_interval("(-10,15]", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("[-10,-10]", float)

    # inside the interval
    i = string_to_interval("[-5,5]", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("[-10,-5)", float) and \
           intersection[1] == string_to_interval("(5,10]", float)
    i = string_to_interval("[-5,5)", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("[-10,-5)", float) and \
           intersection[1] == string_to_interval("[5,10]", float)
    i = string_to_interval("(-5,5]", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("[-10,-5]", float) and \
           intersection[1] == string_to_interval("(5,10]", float)
    i = string_to_interval("(-5,5)", float)
    intersection = i1.setminus(i)
    assert intersection[0] == string_to_interval("[-10,-5]", float) and \
           intersection[1] == string_to_interval("[5,10]", float)

    # bigger than the interval
    i = string_to_interval("[-20,20]", float)
    intersection = i1.setminus(i)
    assert not intersection
    i = string_to_interval("(-20,20)", float)
    intersection = i1.setminus(i)
    assert not intersection
    i = string_to_interval("[-20,20)", float)
    intersection = i1.setminus(i)
    assert not intersection
    i = string_to_interval("(-20,20]", float)
    intersection = i1.setminus(i)
    assert not intersection
예제 #6
0
def test_constraint_to_interval():
    s = "-10.2<fghhklöl<15.3"
    interval = constraint_to_interval(s, float)
    assert interval == string_to_interval("(-10.2,15.3)", float)
예제 #7
0
def test_contains():
    pycarl.clear_variable_pool()
    x = pc.Variable("x")
    p = Parameter(x, string_to_interval("(0,1)", int))
    p_new = pickle.loads(pickle.dumps(p))
    assert p == p_new
def read_pstorm_result(location, require_result=True):
    """
    Read the output of pstorm into a ParametricResult
    :param location: The location of the file to be read
    :type location: str
    :param require_result: If true, parsing fails if no result is found
    :type require_result: Bool
    :return: The data obtained from the model.
    :rtype: ParametricResult
    """
    logging.debug("Open result file from storm...")
    with open(location) as f:
        inputstring = f.read()

    # Build parameters
    logger.debug("Reading parameters...")
    parameters = ParameterOrder()
    parameter_strings = re.findall(r'\$Parameters:\s(.*)',
                                   inputstring)[0].split(";")
    for parameter_string in parameter_strings:
        if parameter_string.strip():
            name_and_info = parameter_string.split()
            var = pc.variable_with_name(name_and_info[0].strip())
            if var.is_no_variable:
                var = pc.Variable(name_and_info[0].strip())
            if len(name_and_info) == 1:
                bound = interval.Interval(pc.Rational(0),
                                          interval.BoundType.open,
                                          pc.Rational(1),
                                          interval.BoundType.open)
            else:
                bound = interval.string_to_interval(name_and_info[1],
                                                    pc.Rational)
            parameters.append(Parameter(var, bound))
    logger.debug("Parameters: %s", str(parameters))

    # Build well-defined constraints
    logging.debug("Reading constraints...")
    constraints_string = re.findall(
        r'(\$Well-formed Constraints:\s*\n.+?)(?=\$|(?:\s*\Z))', inputstring,
        re.DOTALL)[0]
    constraints_string = constraints_string.split("\n")[:-1]
    constraints = [pc.parse(cond) for cond in constraints_string[1:]]
    logger.debug("Constraints: %s", ",".join([str(c) for c in constraints]))
    # Build graph-preserving constraints
    constraints_string = re.findall(
        r'(\$Graph-preserving Constraints:\s*\n.+?)(?=\$|(?:\s*\Z))',
        inputstring, re.DOTALL)[0]
    constraints_string = constraints_string.split("\n")
    gpconstraints = [
        pc.parse(cond) for cond in constraints_string[1:] if cond.strip() != ""
    ]
    logger.debug("GP Constraints: %s",
                 ",".join([str(c) for c in gpconstraints]))

    # Build rational function
    logger.debug("Looking for solution function...")
    match = re.findall(r'\$Result:(.*)$', inputstring, re.MULTILINE)

    if require_result:
        if len(match) == 0:
            raise ValueError(
                "Expected a result in the result file at {}".format(location))
    if len(match) > 0:
        logger.debug("Building solution function...")
        solution = pc.parse(match[0])
        if isinstance(solution, pc.Monomial):
            solution = pc.Polynomial(solution)
        logger.debug("Solution function is %s", solution)
    else:
        solution = None

    logger.debug("Parsing complete.")
    return ParametricResult(parameters, constraints, gpconstraints, solution)