def test_instantiate_consequent(): # Correctly create a minimal Consequent universe = np.linspace(0, 5, 7) label = 'test-Consequent Labeling ??$%&dwlkj234!"' con = Consequent(universe, label) # Assure expected behavior tst.assert_equal(universe, con.universe) assert con.label == label assert con._id == id(con) assert_empty_ordereddict(con.terms) assert con.__name__ == 'Consequent' assert con.__repr__() == 'Consequent: {0}'.format(label)
def test_instantiate_consequent(): # Correctly create a minimal Consequent universe = np.linspace(0, 5, 7) label = 'test-Consequent Labeling ??$%&dwlkj234!"' con = Consequent(universe, label) # Assure expected behavior tst.assert_equal(universe, con.universe) assert con.label == label assert con._id == id(con) assert_empty_ordereddict(con.terms) assert con.__name__ == 'Consequent' assert con.__repr__() == 'Consequent: {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 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)