def setup_rule_order(): global a, b, c, d a = Antecedent(np.linspace(0, 10, 11), 'a') b = Antecedent(np.linspace(0, 10, 11), 'b') c = Antecedent(np.linspace(0, 10, 11), 'c') d = Antecedent(np.linspace(0, 10, 11), 'd') for v in (a, b, c, d): v.automf(3)
def test_instantiate_antecedent(): # Correctly create a minimal Antecedent universe = np.linspace(0, 5, 7) label = 'test-Antecedent Labeling 130948@!!"' ant = Antecedent(universe, label) # Assure expected behavior tst.assert_equal(universe, ant.universe) assert ant.label == label assert ant._id == id(ant) assert_empty_ordereddict(ant.terms) assert ant.__name__ == 'Antecedent' assert ant.__repr__() == 'Antecedent: {0}'.format(label)
def test_add_mf(): universe = np.linspace(0, 7, 21) ant_label = 'service' con_label = 'TIP' ant = Antecedent(universe, ant_label) con = Consequent(universe, con_label) mf0 = np.sin(universe / 7. * 4 * np.pi) / 2. + 0.5 mf1 = np.arange(ant.universe.size) / ant.universe.size # Assign mfs to Antecedent ant['wavy'] = mf0 ant['high'] = mf1 # Ensure they were added correctly assert list(ant.terms.keys()) == ['wavy', 'high'] tst.assert_equal(ant.terms['wavy'].mf, mf0) tst.assert_equal(ant.terms['high'].mf, mf1) # Assign mfs to Consequent con['wavy'] = mf0 con['high'] = mf1 # Ensure they were added correctly assert list(con.terms.keys()) == ['wavy', 'high'] tst.assert_equal(con.terms['wavy'].mf, mf0) tst.assert_equal(con.terms['high'].mf, mf1)
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]"
def setup(): global ant global con global universe universe0 = np.linspace(0, 5, 6) universe1 = np.linspace(0, 9, 10) ant_label = 'service' con_label = 'TIP' ant = Antecedent(universe0, ant_label) con = Consequent(universe1, con_label)
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)