예제 #1
0
def test_term_aggregate_1():
    x1 = Antecedent(np.linspace(0, 10, 11), "x1")
    x1.automf(3)  # term labels: poor, average, good
    x2 = Antecedent(np.linspace(0, 10, 11), "x2")
    x2.automf(3)
    ta = (x1["poor"] & x2["good"])
    # print("- ta:", ta)
    assert isinstance(ta, TermAggregate)
    assert str(ta) == "x1[poor] AND x2[good]"
예제 #2
0
def test_tipping_problem():
    # The full tipping problem uses many of these methods
    food = Antecedent(np.linspace(0, 10, 11), 'quality')
    service = Antecedent(np.linspace(0, 10, 11), 'service')
    tip = Consequent(np.linspace(0, 25, 26), 'tip')

    food.automf(3)
    service.automf(3)

    # Manual membership function definition
    tip['bad'] = trimf(tip.universe, [0, 0, 13])
    tip['middling'] = trimf(tip.universe, [0, 13, 25])
    tip['lots'] = trimf(tip.universe, [13, 25, 25])

    # Define fuzzy rules
    rule1 = Rule(food['poor'] | service['poor'], tip['bad'])
    rule2 = Rule(service['average'], tip['middling'])
    rule3 = Rule(service['good'] | food['good'], tip['lots'])

    # The control system - defined both possible ways
    tipping = ControlSystem([rule1, rule2, rule3])

    tipping2 = ControlSystem()
    tipping2.addrule(rule2)
    tipping2.addrule(rule3)
    tipping2.addrule(rule1)

    tip_sim = ControlSystemSimulation(tipping)
    tip_sim2 = ControlSystemSimulation(tipping2)

    # Inputs added both possible ways
    inputs = {'quality': 6.5, 'service': 9.8}
    for key, value in inputs.items():
        tip_sim.input[key] = value

    tip_sim2.inputs(inputs)

    # Compute the system
    tip_sim.compute()
    tip_sim2.compute()

    assert tip_sim.output == tip_sim2.output
    tst.assert_allclose(tip_sim.output['tip'], 19.8578, atol=1e-2, rtol=1e-2)
def test_tipping_problem():
    # The full tipping problem uses many of these methods
    food = Antecedent(np.linspace(0, 10, 11), 'quality')
    service = Antecedent(np.linspace(0, 10, 11), 'service')
    tip = Consequent(np.linspace(0, 25, 26), 'tip')

    food.automf(3)
    service.automf(3)

    # Manual membership function definition
    tip['bad'] = trimf(tip.universe, [0, 0, 13])
    tip['middling'] = trimf(tip.universe, [0, 13, 25])
    tip['lots'] = trimf(tip.universe, [13, 25, 25])

    # Define fuzzy rules
    rule1 = Rule(food['poor'] | service['poor'], tip['bad'])
    rule2 = Rule(service['average'], tip['middling'])
    rule3 = Rule(service['good'] | food['good'], tip['lots'])

    # The control system - defined both possible ways
    tipping = ControlSystem([rule1, rule2, rule3])

    tipping2 = ControlSystem()
    tipping2.addrule(rule2)
    tipping2.addrule(rule3)
    tipping2.addrule(rule1)

    tip_sim = ControlSystemSimulation(tipping)
    tip_sim2 = ControlSystemSimulation(tipping2)

    # Inputs added both possible ways
    inputs = {'quality': 6.5, 'service': 9.8}
    for key, value in inputs.items():
        tip_sim.input[key] = value

    tip_sim2.inputs(inputs)

    # Compute the system
    tip_sim.compute()
    tip_sim2.compute()

    assert tip_sim.output == tip_sim2.output
    tst.assert_allclose(tip_sim.output['tip'], 19.8578, atol=1e-2, rtol=1e-2)