Ejemplo n.º 1
0
def test_fully_cross_with_uncrossed_simple_factors():
    other = Factor('other', ['l1', 'l2'])
    block = fully_cross_block([color, text, other], [color, text], [])

    backend_request = BackendRequest(25)
    FullyCross.apply(block, backend_request)

    (expected_cnf, _) = to_cnf_tseitin(
        And([
            Iff(25, And([1, 3])),
            Iff(26, And([1, 4])),
            Iff(27, And([2, 3])),
            Iff(28, And([2, 4])),
            Iff(29, And([7, 9])),
            Iff(30, And([7, 10])),
            Iff(31, And([8, 9])),
            Iff(32, And([8, 10])),
            Iff(33, And([13, 15])),
            Iff(34, And([13, 16])),
            Iff(35, And([14, 15])),
            Iff(36, And([14, 16])),
            Iff(37, And([19, 21])),
            Iff(38, And([19, 22])),
            Iff(39, And([20, 21])),
            Iff(40, And([20, 22]))
        ]), 41)

    assert backend_request.fresh == 74
    assert backend_request.cnfs == [expected_cnf]
    assert backend_request.ll_requests == [
        LowLevelRequest("GT", 0, [25, 29, 33, 37]),
        LowLevelRequest("GT", 0, [26, 30, 34, 38]),
        LowLevelRequest("GT", 0, [27, 31, 35, 39]),
        LowLevelRequest("GT", 0, [28, 32, 36, 40])
    ]
Ejemplo n.º 2
0
def test_fully_cross_with_transition_in_design(design):
    block = fully_cross_block(design, [color, text], [])

    backend_request = BackendRequest(23)
    FullyCross.apply(block, backend_request)

    (expected_cnf, _) = to_cnf_tseitin(
        And([
            Iff(23, And([1, 3])),
            Iff(24, And([1, 4])),
            Iff(25, And([2, 3])),
            Iff(26, And([2, 4])),
            Iff(27, And([5, 7])),
            Iff(28, And([5, 8])),
            Iff(29, And([6, 7])),
            Iff(30, And([6, 8])),
            Iff(31, And([9, 11])),
            Iff(32, And([9, 12])),
            Iff(33, And([10, 11])),
            Iff(34, And([10, 12])),
            Iff(35, And([13, 15])),
            Iff(36, And([13, 16])),
            Iff(37, And([14, 15])),
            Iff(38, And([14, 16]))
        ]), 39)

    assert backend_request.fresh == 72
    assert backend_request.cnfs == [expected_cnf]
    assert backend_request.ll_requests == [
        LowLevelRequest("GT", 0, [23, 27, 31, 35]),
        LowLevelRequest("GT", 0, [24, 28, 32, 36]),
        LowLevelRequest("GT", 0, [25, 29, 33, 37]),
        LowLevelRequest("GT", 0, [26, 30, 34, 38])
    ]
Ejemplo n.º 3
0
def test_fully_cross_with_constraint():
    (expected_cnf, _) = to_cnf_tseitin(
        And([
            Iff(25, And([1, 3])),
            Iff(26, And([1, 4])),
            Iff(27, And([2, 3])),
            Iff(28, And([2, 4])),
            Iff(29, And([7, 9])),
            Iff(30, And([7, 10])),
            Iff(31, And([8, 9])),
            Iff(32, And([8, 10])),
            Iff(33, And([13, 15])),
            Iff(34, And([13, 16])),
            Iff(35, And([14, 15])),
            Iff(36, And([14, 16])),
            Iff(37, And([19, 21])),
            Iff(38, And([19, 22])),
            Iff(39, And([20, 21])),
            Iff(40, And([20, 22]))
        ]), 41)

    backend_request = BackendRequest(25)
    FullyCross.apply(block, backend_request)

    assert backend_request.fresh == 74
    assert backend_request.cnfs == [expected_cnf]
    assert backend_request.ll_requests == [
        LowLevelRequest("GT", 0, [25, 29, 33, 37]),
        LowLevelRequest("GT", 0, [26, 30, 34, 38]),
        LowLevelRequest("GT", 0, [27, 31, 35, 39]),
        LowLevelRequest("GT", 0, [28, 32, 36, 40])
    ]
Ejemplo n.º 4
0
def test_fully_cross_simple():
    block = fully_cross_block([color, text], [color, text], [])

    (expected_cnf, _) = to_cnf_tseitin(
        And([
            Iff(17, And([1, 3])),
            Iff(18, And([1, 4])),
            Iff(19, And([2, 3])),
            Iff(20, And([2, 4])),
            Iff(21, And([5, 7])),
            Iff(22, And([5, 8])),
            Iff(23, And([6, 7])),
            Iff(24, And([6, 8])),
            Iff(25, And([9, 11])),
            Iff(26, And([9, 12])),
            Iff(27, And([10, 11])),
            Iff(28, And([10, 12])),
            Iff(29, And([13, 15])),
            Iff(30, And([13, 16])),
            Iff(31, And([14, 15])),
            Iff(32, And([14, 16]))
        ]), 33)

    backend_request = BackendRequest(17)
    FullyCross.apply(block, backend_request)

    assert backend_request.fresh == 66
    assert backend_request.cnfs == [expected_cnf]
    assert backend_request.ll_requests == [
        LowLevelRequest("GT", 0, [17, 21, 25, 29]),
        LowLevelRequest("GT", 0, [18, 22, 26, 30]),
        LowLevelRequest("GT", 0, [19, 23, 27, 31]),
        LowLevelRequest("GT", 0, [20, 24, 28, 32])
    ]
Ejemplo n.º 5
0
def fully_cross_block(design: List[Factor],
                      crossing: List[Factor],
                      constraints: List[Constraint],
                      require_complete_crossing=True,
                      cnf_fn=to_cnf_tseitin) -> Block:
    """Returns a fully crossed :class:`.Block` meant to be used in experiment
    synthesis. This is the preferred mechanism for describing an experiment.

    :param design:
        A :class:`list` of all the :class:`Factors <.Factor>` in the design.
        When a sequence of trials is generated, each trial will have one level
        from each factor in ``design``.

    :param crossing:
        A :class:`list` of :class:`Factors <.Factor>` used to produce
        crossings. The number of trials in each run of the experiment is
        determined as the product of the number of levels of factors in
        ``crossing``.

        If ``require_complete_crossing`` is ``False``, the ``constraints`` can
        reduce the total number of trials.

        Different trial sequences of the experiment will have different
        combinations of levels in different orders. The factors in ``crossing``
        supply an implicit constraint that every combination of levels in the
        cross should appear once. Derived factors impose additional
        constraints: only combinations of levels that are consistent with
        derivations can appear as a trial. Additional constraints can be
        manually imposed via the ``constraints`` parameter.

    :param constraints:
        A :class:`list` of :class:`Constraints <.Constraint>` that restrict the
        generated trials.

    :param require_complete_crossing:
        Whether every combination in ``crossing`` must appear in a block of
        trials. ``True`` by default. A ``False`` value is appropriate if
        combinations are excluded through an :class:`.Exclude`
        :class:`.Constraint`.

    :param cnf_fn:
        A CNF conversion function. Default is :func:`.to_cnf_tseitin`.
    """
    all_constraints = cast(List[Constraint],
                           [FullyCross(), Consistency()]) + constraints
    all_constraints = __desugar_constraints(
        all_constraints)  #expand the constraints into a form we can process.
    block = FullyCrossBlock(design, [crossing], all_constraints,
                            require_complete_crossing, cnf_fn)
    block.constraints += DerivationProcessor.generate_derivations(block)
    if not constraints and not list(filter(
            lambda f: f.is_derived(), crossing)) and not list(
                filter(lambda f: f.has_complex_window, design)):
        block.complex_factors_or_constraints = False
    return block
Ejemplo n.º 6
0
def test_fully_cross_with_transition_in_crossing():
    direction = Factor("direction", ["up", "down"])

    block = fully_cross_block([direction, color, color_repeats_factor],
                              [direction, color_repeats_factor], [])

    backend_request = BackendRequest(29)
    FullyCross.apply(block, backend_request)

    (expected_cnf, _) = to_cnf_tseitin(
        And([
            Iff(29, And([5, 21])),
            Iff(30, And([5, 22])),
            Iff(31, And([6, 21])),
            Iff(32, And([6, 22])),
            Iff(33, And([9, 23])),
            Iff(34, And([9, 24])),
            Iff(35, And([10, 23])),
            Iff(36, And([10, 24])),
            Iff(37, And([13, 25])),
            Iff(38, And([13, 26])),
            Iff(39, And([14, 25])),
            Iff(40, And([14, 26])),
            Iff(41, And([17, 27])),
            Iff(42, And([17, 28])),
            Iff(43, And([18, 27])),
            Iff(44, And([18, 28])),
        ]), 45)

    assert backend_request.fresh == 78
    assert backend_request.cnfs == [expected_cnf]
    assert backend_request.ll_requests == [
        LowLevelRequest("GT", 0, [29, 33, 37, 41]),
        LowLevelRequest("GT", 0, [30, 34, 38, 42]),
        LowLevelRequest("GT", 0, [31, 35, 39, 43]),
        LowLevelRequest("GT", 0, [32, 36, 40, 44])
    ]
def test_fully_cross_with_three_factors():
    (expected_cnf, _) = to_cnf_tseitin(
        And([
            Iff(65, And([3, 5, 7])),
            Iff(66, And([3, 5, 8])),
            Iff(67, And([3, 6, 7])),
            Iff(68, And([3, 6, 8])),
            Iff(69, And([4, 5, 7])),
            Iff(70, And([4, 5, 8])),
            Iff(71, And([4, 6, 7])),
            Iff(72, And([4, 6, 8])),
            Iff(73, And([11, 13, 15])),
            Iff(74, And([11, 13, 16])),
            Iff(75, And([11, 14, 15])),
            Iff(76, And([11, 14, 16])),
            Iff(77, And([12, 13, 15])),
            Iff(78, And([12, 13, 16])),
            Iff(79, And([12, 14, 15])),
            Iff(80, And([12, 14, 16])),
            Iff(81, And([19, 21, 23])),
            Iff(82, And([19, 21, 24])),
            Iff(83, And([19, 22, 23])),
            Iff(84, And([19, 22, 24])),
            Iff(85, And([20, 21, 23])),
            Iff(86, And([20, 21, 24])),
            Iff(87, And([20, 22, 23])),
            Iff(88, And([20, 22, 24])),
            Iff(89, And([27, 29, 31])),
            Iff(90, And([27, 29, 32])),
            Iff(91, And([27, 30, 31])),
            Iff(92, And([27, 30, 32])),
            Iff(93, And([28, 29, 31])),
            Iff(94, And([28, 29, 32])),
            Iff(95, And([28, 30, 31])),
            Iff(96, And([28, 30, 32])),
            Iff(97, And([35, 37, 39])),
            Iff(98, And([35, 37, 40])),
            Iff(99, And([35, 38, 39])),
            Iff(100, And([35, 38, 40])),
            Iff(101, And([36, 37, 39])),
            Iff(102, And([36, 37, 40])),
            Iff(103, And([36, 38, 39])),
            Iff(104, And([36, 38, 40])),
            Iff(105, And([43, 45, 47])),
            Iff(106, And([43, 45, 48])),
            Iff(107, And([43, 46, 47])),
            Iff(108, And([43, 46, 48])),
            Iff(109, And([44, 45, 47])),
            Iff(110, And([44, 45, 48])),
            Iff(111, And([44, 46, 47])),
            Iff(112, And([44, 46, 48])),
            Iff(113, And([51, 53, 55])),
            Iff(114, And([51, 53, 56])),
            Iff(115, And([51, 54, 55])),
            Iff(116, And([51, 54, 56])),
            Iff(117, And([52, 53, 55])),
            Iff(118, And([52, 53, 56])),
            Iff(119, And([52, 54, 55])),
            Iff(120, And([52, 54, 56])),
            Iff(121, And([59, 61, 63])),
            Iff(122, And([59, 61, 64])),
            Iff(123, And([59, 62, 63])),
            Iff(124, And([59, 62, 64])),
            Iff(125, And([60, 61, 63])),
            Iff(126, And([60, 61, 64])),
            Iff(127, And([60, 62, 63])),
            Iff(128, And([60, 62, 64])),
        ]), 129)

    backend_request = BackendRequest(65)
    FullyCross.apply(block, backend_request)

    assert backend_request.fresh == 258
    assert backend_request.cnfs == [expected_cnf]
    assert backend_request.ll_requests == [
        LowLevelRequest("GT", 0, [65, 73, 81, 89, 97, 105, 113, 121]),
        LowLevelRequest("GT", 0, [66, 74, 82, 90, 98, 106, 114, 122]),
        LowLevelRequest("GT", 0, [67, 75, 83, 91, 99, 107, 115, 123]),
        LowLevelRequest("GT", 0, [68, 76, 84, 92, 100, 108, 116, 124]),
        LowLevelRequest("GT", 0, [69, 77, 85, 93, 101, 109, 117, 125]),
        LowLevelRequest("GT", 0, [70, 78, 86, 94, 102, 110, 118, 126]),
        LowLevelRequest("GT", 0, [71, 79, 87, 95, 103, 111, 119, 127]),
        LowLevelRequest("GT", 0, [72, 80, 88, 96, 104, 112, 120, 128])
    ]