-
大小: 7KB文件類型: .zip金幣: 2下載: 0 次發(fā)布日期: 2021-06-08
- 語言: 其他
- 標(biāo)簽: KnowledgeBas??AI??Python??
資源簡介
可以實(shí)現(xiàn)按要求開展50-100次游戲,每次游戲中的怪獸、坑、金子均隨機(jī)出現(xiàn),且可以調(diào)節(jié)pits出現(xiàn)的概率。Agent會(huì)根據(jù)KB理論盡可能地選取最佳路徑獲得金幣,贏得勝利。日志會(huì)記錄每次的移動(dòng)信息與在每一個(gè)cell所感知到的信息(氣味、微風(fēng))。

代碼片段和文件信息
#?
#?Propositional?and?First-Order?logic.
#
#?Compiled?against?Python?2.7
import?re
“““
define?some?basic?functions?used?below
“““
def?isnumber(x):
????return?hasattr(x?‘__int__‘)
def?num_or_str(x):
????if?isnumber(x):?return?x
????try:
????????return?int(x)
????except?ValueError:
????????try:
????????????return?float(x)
????????except?ValueError:
????????????return?str(x).strip()
def?find_if(predicate?seq):
????for?x?in?seq:
????????if?predicate(x):?return?x
????return?None
#______________________________________________________________________________
class?KB:
????def?__init__(self?sentence=None):
????????abstract
????def?tell(self?sentence):
????????“Add?the?sentence?to?the?KB.“
????????abstract
????def?ask(self?query):
????????for?result?in?self.ask_generator(query):
????????????return?result
????????return?False
????def?ask_generator(self?query):
????????“Yield?all?the?substitutions?that?make?query?true.“
????????abstract
????def?retract(self?sentence):
????????“Remove?sentence?from?the?KB.“
????????abstract
class?PropKB(KB):
????“A?KB?for?propositional?logic.?Inefficient?with?no?indexing.“
????def?__init__(self?sentence=None):
????????self.conds?=?[]
????????if?sentence:
????????????self.tell(sentence)
????def?tell(self?sentence):
????????“Add?the?sentence‘s?conds?to?the?KB.“
????????self.conds.extend(conjuncts(to_cnf(sentence)))
????def?ask_generator(self?query):
????????“Yield?the?empty?substitution?if?KB?implies?query;?else?nothing.“
????????if?tt_entails(Expr(‘&‘?*self.conds)?query):
????????????yield?{}
????def?retract(self?sentence):
????????“Remove?the?sentence‘s?conds?from?the?KB.“
????????for?c?in?conjuncts(to_cnf(sentence)):
????????????if?c?in?self.conds:
????????????????self.conds.remove(c)
#______________________________________________________________________________
class?Expr:
????def?__init__(self?op?*args):
????????“Op?is?a?string?or?number;?args?are?Exprs?(or?are?coerced?to?Exprs).“
????????assert?isinstance(op?str)?or?(isnumber(op)?and?not?args)
????????self.op?=?num_or_str(op)
????????self.args?=?map(expr?args)
????def?__call__(self?*args):
????????assert?is_symbol(self.op)?and?not?self.args
????????return?Expr(self.op?*args)
????def?__repr__(self):
????????“Show?something?like?‘P‘?or?‘P(x?y)‘?or?‘~P‘?or?‘(P?|?Q?|?R)‘“
????????if?not?self.args:?????????
????????????return?str(self.op)
????????elif?is_symbol(self.op):??
????????????return?‘%s(%s)‘?%?(self.op?‘?‘.join(map(repr?self.args)))
????????elif?len(self.args)?==?1:?
????????????return?self.op?+?repr(self.args[0])
????????else:?????????????????????
????????????return?‘(%s)‘?%?(‘?‘+self.op+‘?‘).join(map(repr?self.args))
????def?__eq__(self?other):
????????return?(other?is?self)?or?(isinstance(other?Expr)
????????????and?self.op?==?other.op?and?self.args?==?other.args)
????def?__ne__(self?other):
????????return?not?self.__eq__(other)
????def?__hash__(self):
????????“Need?a?hash?method?so?Exprs?can?live?in?dicts.“
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????8970??2018-12-10?10:43??WumpusWorldAI.py
?????文件???????10037??2018-12-10?10:39??logic.py
?????文件????????2909??2018-12-10?10:39??logic_info_KB.py
?????文件?????????822??2018-12-10?10:32??README.md
評論
共有 條評論