am_dot_com

IA - 2022-10-26

Oct 26th, 2022 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. # 5.py
  2.  
  3. """
  4. tool #1
  5. helper for the generation of model
  6. model / set of attributions
  7. { door is close <- True, Lights are on <- True }
  8. { p1, p2 } # this is a notation that means p1 is True and p2 is False
  9. """
  10. def getListOfAlternatingTrueFalse(
  11. piHowManyValues:int,
  12. piAlternateEveryN:int=1,
  13. pbStartValue:bool=True
  14. ):
  15. retList = list()
  16. retList = []
  17.  
  18. bCurrentValue = pbStartValue
  19. while(len(retList)<piHowManyValues):
  20. retList.append(bCurrentValue)
  21. bAlternateTime:bool = \
  22. len(retList)%piAlternateEveryN == 0
  23. if(bAlternateTime):
  24. bCurrentValue = not bCurrentValue
  25. # while
  26.  
  27. return retList
  28. # def getListOfAlternatingTrueFalse
  29.  
  30. """
  31. l1 = getListOfAlternatingTrueFalse(
  32. 33,
  33. piAlternateEveryN=4,
  34. pbStartValue=True
  35. )
  36. print (l1)
  37. """
  38.  
  39.  
  40. """
  41. 1 prop (1 var) => basta 1 lista q alterne de 1-1
  42. 2 props (2 vars) => 2 listas (1-1 , 2-2)
  43. 3 props (3 vars) => 3 listas (1-1 , 2-2, 4-4)
  44. n props (n vars) => n listas (2**0=1, 2**1=2, ... 2**n-1)
  45. ferramenta auxiliar para compilar todas as listas
  46. de combinações possíveis
  47. """
  48. def getAllPossibleAlternatingListsForNProps(
  49. pN:int=2,
  50. pbStartValue:bool=True
  51. ):
  52. retListOfLists = list()
  53.  
  54. iHowManyCombosThereAreForNProps = 2**pN
  55. iCurrentList = 0
  56. while(len(retListOfLists)<pN):
  57. listOfAlternatingValues = \
  58. getListOfAlternatingTrueFalse(
  59. piHowManyValues=iHowManyCombosThereAreForNProps,
  60. piAlternateEveryN=2**iCurrentList,
  61. pbStartValue=pbStartValue
  62. )
  63. retListOfLists.append(listOfAlternatingValues)
  64. iCurrentList+=1
  65. # while
  66. return retListOfLists
  67. # def getAllPossibleAlternatingListsForNProps
  68.  
  69. """
  70. l2 = getAllPossibleAlternatingListsForNProps(3)
  71. print (l2)
  72. """
  73.  
  74. def getAllModelsForNProps(
  75. pN:int=2,
  76. pbStartValue:bool=True
  77. ):
  78. retListOfAllPossibleModels = list()
  79.  
  80. listAlts =\
  81. getAllPossibleAlternatingListsForNProps(
  82. pN=pN,
  83. pbStartValue=pbStartValue
  84. )
  85.  
  86. for idxValue in range(2**pN):
  87. currentModel = list()
  88. for idxListAlt in range(len(listAlts)):
  89. currentList = listAlts[idxListAlt]
  90. bCurrentValue:bool = currentList[idxValue]
  91. currentModel.append(bCurrentValue)
  92. # for
  93. # a new model is ready
  94.  
  95. currentModel.reverse() # in-place
  96. retListOfAllPossibleModels.append(
  97. currentModel
  98. )
  99. # for
  100.  
  101. return retListOfAllPossibleModels
  102. # def getAllModelsForNProps
  103.  
  104. """
  105. models = getAllModelsForNProps(3)
  106. print(models)
  107. """
  108.  
  109. def getAllDictModelsForNamedProps(
  110. pListNamedProps = ["a", "b", "c"]
  111. ):
  112. retListOfDictModels = list()
  113.  
  114. iHowManyProps = len(pListNamedProps)
  115. allModels = getAllModelsForNProps(pN=iHowManyProps)
  116.  
  117. for idxModel in range(len(allModels)):
  118. currentModel = allModels[idxModel]
  119. newDictModel = dict()
  120. for idxProp in range(len(pListNamedProps)):
  121. strCurrentProp = pListNamedProps[idxProp]
  122. bCurrentValue:bool = currentModel[idxProp]
  123. newDictModel[strCurrentProp] = bCurrentValue
  124. # for
  125. # new model (as dict)
  126. retListOfDictModels.append(newDictModel)
  127. # for
  128.  
  129. return retListOfDictModels
  130. # def getAllDictModelsForNamedProps
  131.  
  132. def truthTable(
  133. pListOfPropositions, # a list of proposition
  134. pF # some boolean function
  135. ):
  136. listOfCorrespondences = list()
  137.  
  138. listOfAllPossibleModelsForProps =\
  139. getAllDictModelsForNamedProps(
  140. pListOfPropositions
  141. )
  142.  
  143. for m in listOfAllPossibleModelsForProps:
  144. listOfValuesInTheCurrentModel = list()
  145. for propName in m.keys():
  146. propValue:bool = m[propName]
  147. listOfValuesInTheCurrentModel.append(
  148. propValue
  149. )
  150. # for every value in the value
  151. # now we have the list of values for model m
  152.  
  153. resultForTheCurrentModel = pF(
  154. *listOfValuesInTheCurrentModel
  155. )
  156. listOfCorrespondences.append(
  157. (m, resultForTheCurrentModel)
  158. )
  159. # for every model
  160.  
  161. return listOfCorrespondences
  162. # def truthTable
  163.  
  164. def headerForTruthTable(
  165. pT,
  166. pDescription:str=""
  167. ):
  168. strHeader:str=""
  169. bThereAreResults = len(pT)>0
  170. if (bThereAreResults):
  171. theFirstModelAsTuple = pT[0]
  172. theFirstModel:dict = theFirstModelAsTuple[0] # the model
  173. # the keys n the 1st result are the same
  174. # as in any other results
  175. for k in theFirstModel.keys():
  176. strHeader+=f"{k}\t"
  177. # for every proposition
  178.  
  179. strHeader+=pDescription
  180. strHeader+="\n"
  181. #if
  182. return strHeader
  183. # def headerForTruthTable
  184.  
  185. def renderTruthTable (pT, pDescription:str=""):
  186. strTablePresentation:str = headerForTruthTable(
  187. pT,
  188. pDescription
  189. )
  190. for tupleDictBool in pT:
  191. theModel:dict = tupleDictBool[0]
  192. result:bool = tupleDictBool[1]
  193.  
  194. strLine = ""
  195. for prop in theModel.keys():
  196. v:bool = theModel[prop]
  197. strLine+=str(v)+"\t"
  198. # for
  199. # example: "True\tTrue\t"
  200. strLine+=f"{result}\n"
  201. # example: "True\tTrue\tTrue\n"
  202. strTablePresentation+=strLine
  203. # for every tuple (dict, result)
  204. return strTablePresentation
  205. # def renderTruthTable
  206.  
  207.  
  208. def logicalConsequence (pPropositions, pLeft, pRight)->bool:
  209. truthLeft = truthTable(pPropositions, pLeft)
  210. truthRight = truthTable(pPropositions, pRight)
  211.  
  212. bDoWeHaveLogicalConsequence = True
  213.  
  214. for idxModel in range(len(truthLeft)):
  215. currentTupleModelResult = truthLeft[idxModel]
  216. currentLeftResult = currentTupleModelResult[1]
  217. if(currentLeftResult==True):
  218. currentRightResult = truthRight[idxModel][1]
  219. bDoWeHaveLogicalConsequence =\
  220. bDoWeHaveLogicalConsequence and \
  221. (currentLeftResult == currentRightResult)
  222. if (not bDoWeHaveLogicalConsequence):
  223. return False
  224. # if
  225. # if
  226. # for
  227. return bDoWeHaveLogicalConsequence
  228. # def logicalConsequence
  229.  
  230. def logicalEquivalence(pProps, pLeftExp, pRightExp)->bool:
  231. bLeftImpliesRight = logicalConsequence(pProps, pLeftExp, pRightExp)
  232. bRightImpliesLeft = logicalConsequence(pProps, pRightExp, pLeftExp)
  233. return bLeftImpliesRight and bRightImpliesLeft
  234. # def logicalEquivalence
  235.  
  236. # (p or q) |= p
  237. def leftSide(p, q):
  238. return (p or q)
  239. # def leftSide
  240.  
  241. def rightSide(p, q):
  242. return p
  243. # def rightSide
  244.  
  245. isRightLogicalConsequenceOfTheLeft =\
  246. logicalConsequence(["p", "q"], leftSide, rightSide)
  247.  
  248. print(isRightLogicalConsequenceOfTheLeft)
  249.  
  250.  
Advertisement
Add Comment
Please, Sign In to add comment