|
1 | 1 | import unittest |
2 | 2 | import json |
3 | | -from py_fcm import FuzzyCognitiveMap, TYPE_DECISION |
| 3 | +from py_fcm import FuzzyCognitiveMap, TYPE_DECISION, TYPE_FUZZY, TYPE_SIMPLE |
4 | 4 |
|
5 | 5 |
|
6 | 6 | class FuzzyCognitiveMapTests(unittest.TestCase): |
@@ -379,6 +379,54 @@ def test_inference_lceq(self): |
379 | 379 | json_fcm = json.loads(fcm.to_json()) |
380 | 380 | self.assertEqual(expected_json["concepts"], json_fcm["concepts"]) |
381 | 381 |
|
| 382 | + def test_inference_fuzzy(self): |
| 383 | + expected_json = { |
| 384 | + 'concepts': [{'id': 'r1', 'is_active': True, 'type': 'DECISION', 'activation': 0.4687996143920417}, |
| 385 | + {'id': 'r2', 'is_active': True, 'type': 'DECISION', 'activation': 0.4625401367231719}, |
| 386 | + {'id': 'c1', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.3, |
| 387 | + 'custom_function': 'threestate'}, |
| 388 | + {'id': 'c2', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.3, |
| 389 | + 'custom_function': 'threestate'}, |
| 390 | + {'id': 'c3', 'is_active': True, 'type': 'SIMPLE', 'activation': 0.3, |
| 391 | + 'custom_function': 'fuzzy', 'activation_dict': {'membership': [0.25, 0.5, 1.0, 0.5, 0.25], |
| 392 | + 'val_list': [1.0, 2.0, 3.0, 4.0, 5.0]}}] |
| 393 | + } |
| 394 | + |
| 395 | + fcm = FuzzyCognitiveMap(activ_function='sigmoid') |
| 396 | + |
| 397 | + fcm.add_concept('r1', TYPE_DECISION) |
| 398 | + fcm.add_concept('r2', TYPE_DECISION) |
| 399 | + fcm.add_concept('c1', TYPE_SIMPLE, activation_function="threestate") |
| 400 | + fcm.add_concept('c2', TYPE_SIMPLE, activation_function="threestate") |
| 401 | + fcm.add_concept('c3', TYPE_FUZZY, activation_dict={ |
| 402 | + 'membership': [0.25, 0.5, 1.0, 0.5, 0.25], |
| 403 | + 'val_list': [1, 2, 3, 4, 5] |
| 404 | + }) |
| 405 | + |
| 406 | + fcm.add_relation('c3', 'r1', 0.5) |
| 407 | + fcm.add_relation('c3', 'r2', 0.5) |
| 408 | + |
| 409 | + fcm.add_relation('r1', 'c1', 1.0) |
| 410 | + fcm.add_relation('r2', 'c2', 1.0) |
| 411 | + |
| 412 | + fcm.add_relation('c1', 'c3', 0.7) |
| 413 | + fcm.add_relation('c1', 'r2', -0.3) |
| 414 | + |
| 415 | + fcm.add_relation('c2', 'c3', 0.8) |
| 416 | + fcm.add_relation('c2', 'r1', -0.2) |
| 417 | + |
| 418 | + fcm.init_concept('c1', -1.0) |
| 419 | + fcm.init_concept('c2', -1.0) |
| 420 | + |
| 421 | + fcm.run_inference() |
| 422 | + |
| 423 | + json_fcm = json.loads(fcm.to_json()) |
| 424 | + self.assertEqual(expected_json["concepts"], json_fcm["concepts"]) |
| 425 | + fcm.debug = False |
| 426 | + fcm.run_inference() |
| 427 | + json_fcm = json.loads(fcm.to_json()) |
| 428 | + self.assertEqual(expected_json["concepts"], json_fcm["concepts"]) |
| 429 | + |
382 | 430 | def test_inference_several_functions(self): |
383 | 431 | expected_json = { |
384 | 432 | 'concepts': [ |
|
0 commit comments