test_my_air_cargo_problems.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import os
  2. import sys
  3. import unittest
  4. from aimacode.planning import Action
  5. from aimacode.utils import expr
  6. from aimacode.search import Node
  7. from lp_utils import decode_state
  8. from my_air_cargo_problems import (
  9. air_cargo_p1, air_cargo_p2, air_cargo_p3,
  10. )
  11. class TestAirCargoProb1(unittest.TestCase):
  12. def setUp(self):
  13. self.p1 = air_cargo_p1()
  14. def test_ACP1_num_fluents(self):
  15. self.assertEqual(len(self.p1.initial), 12)
  16. def test_ACP1_num_requirements(self):
  17. self.assertEqual(len(self.p1.goal),2)
  18. class TestAirCargoProb2(unittest.TestCase):
  19. def setUp(self):
  20. self.p2 = air_cargo_p2()
  21. def test_ACP2_num_fluents(self):
  22. self.assertEqual(len(self.p2.initial), 27)
  23. def test_ACP2_num_requirements(self):
  24. self.assertEqual(len(self.p2.goal),3)
  25. class TestAirCargoProb3(unittest.TestCase):
  26. def setUp(self):
  27. self.p3 = air_cargo_p3()
  28. def test_ACP3_num_fluents(self):
  29. self.assertEqual(len(self.p3.initial), 32)
  30. def test_ACP3_num_requirements(self):
  31. self.assertEqual(len(self.p3.goal),4)
  32. class TestAirCargoMethods(unittest.TestCase):
  33. def setUp(self):
  34. self.p1 = air_cargo_p1()
  35. self.act1 = Action(
  36. expr('Load(C1, P1, SFO)'),
  37. [[expr('At(C1, SFO)'), expr('At(P1, SFO)')], []],
  38. [[expr('In(C1, P1)')], [expr('At(C1, SFO)')]]
  39. )
  40. def test_AC_get_actions(self):
  41. # to see a list of the actions, uncomment below
  42. # print("\nactions for problem")
  43. # for action in self.p1.actions_list:
  44. # print("{}{}".format(action.name, action.args))
  45. self.assertEqual(len(self.p1.actions_list), 20)
  46. def test_AC_actions(self):
  47. # to see list of possible actions, uncomment below
  48. # print("\npossible actions:")
  49. # for action in self.p1.actions(self.p1.initial):
  50. # print("{}{}".format(action.name, action.args))
  51. self.assertEqual(len(self.p1.actions(self.p1.initial)), 4)
  52. def test_AC_result(self):
  53. fs = decode_state(self.p1.result(self.p1.initial, self.act1), self.p1.state_map)
  54. self.assertTrue(expr('In(C1, P1)') in fs.pos)
  55. self.assertTrue(expr('At(C1, SFO)') in fs.neg)
  56. def test_h_ignore_preconditions(self):
  57. n = Node(self.p1.initial)
  58. self.assertEqual(self.p1.h_ignore_preconditions(n),2)
  59. if __name__ == '__main__':
  60. unittest.main()