欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

麻省理工学院公开课-人工智能 公开课笔记1

程序员文章站 2022-07-12 13:52:36
...

What is Artificial Intelligence

Artificial Intelligence is about
algorithms enabled by
constraints exposed by
representations that support
models targeted at
thinking, perception and action.

The problem about the farmer crossing the river

A farmer with his fox, his grain and his goose wants to cross a river.
There is a leaky little boat that can only carry the farmer and one of his four possessions.

#!/usr/bin/env python
# coding=utf-8

sideA = [["fox", 1], ["goose", 1], ["grain", 1]]  # initial state
sideB = [["fox", 0], ["goose", 0], ["grain", 0]]
size = len(sideA)
last_one = -1  # 判断避免重复运输时使用


def judge(side):
    if side[1][1] == 1 and side[0][1] + side[2][1] == 1:  # constraints
        return False
    else:
        return True


def a_to_b():
    global last_one
    for i in range(size):
        if sideA[i][1] == 1 and i != last_one:
            sideA[i][1] -= 1
            if judge(sideA):
                sideB[i][1] += 1
                last_one = i
                print("%s, A—>B" % sideA[i][0])
                break
            else:
                sideA[i][1] += 1


def b_to_a():
    global last_one
    if not judge(sideB):
        for j in range(size):
            if sideB[j][1] == 1 and j != last_one:
                sideB[j][1] -= 1
                sideA[j][1] += 1
                last_one = j
                print("%s, B—>A" % sideB[j][0])
                break
    else:
        if success():
            print("任务完成")
        else:
            print("空船,B->A")


def show(side):
    existed_list = []
    for each_side in side:
        if each_side[1] == 1:
            existed_list.append(each_side)
    return existed_list


def success():
    if sideB[0][1] + sideB[1][1] + sideB[2][1] == 3:
        return True
    else:
        return False


def main():
    while 1:
        print("A岸上有", show(sideA))
        print("B岸上有", show(sideB))
        a_to_b()
        print("A岸上有", show(sideA))
        print("B岸上有", show(sideB))
        b_to_a()
        if success():
            break


if __name__ == '__main__':
    main()