集体智慧编程遇到的问题

第三章

P38

问题描述:安装PIL

  • 错误1:_imagingft.c:73:10: fatal error: 'freetype/fterrors.h' file not found
    解决方法:ln -s /usr/local/include/freetype2 /usr/local/include/freetype

  • 错误2:'X11/Xlib.h' file not found
    解决方法:ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers/X11 /usr/local/include/X11 (MacOSX10.9.sdk,换成自己的版本)

P40

问题描述:生成树状图blogclust.jpg时,报错
错误:

IOError: decoder zip not available

原因:PIL需要依赖其他库,但是依赖却出了问题(P38的错误1和2,就是依赖问题)
解决方法:安装PIL的一个分支,叫pillow
安装方法:

  1. 卸载之前安装的PIL,方法参考链接

  2. pip install pillow
    未知问题:由于之前通过源代码编译安装过PIL,后来才装的pillow,不知道直接安装pillow会不会出现问题

P99

问题描述:运行遗传算法,报错
错误:

    for d in range(len(sol) / 2):
TypeError: object of type 'NoneType' has no len()

原因:

# 当if,elif都不满足是,返回None
def mutate(vec):
    i = random.randint(0, len(domain) - 1)
    if random.random() < 0.5 and vec[i] > domain[i][0]:
        return vec[0:i] + [vec[i] - step] + vec[i+1:]
    elif vec[i] < domain[i][2]:
        return vec[0:i] + [vec[i] + step] + vec[i+1:]

解决方法:

# if,elif都不满足的情况是,vec[i]等于domain[i][3],因此把vec[i]等于domain[i][4]加入到if的条件判断中。这很合理,因为无聊random.random()的随机值是多少,只要vec[i] == doman[i][0],那么elif就会执行。  
def mutate(vec):
    i = random.randint(0, len(domain) - 1)
    if (random.random() < 0.5 and vec[i] > domain[i][0]) or vec[i] == domain[i][5]: 
        print '>', vec[0:i] + [vec[i] - step] + vec[i+1:]
        return vec[0:i] + [vec[i] - step] + vec[i+1:]
    elif vec[i] < domain[i][6]:
        print '<', vec[0:i] + [vec[i] + step] + vec[i+1:]
        return vec[0:i] + [vec[i] + step] + vec[i+1:]

# 什么都不做,也行
def mutate(vec):
    i = random.randint(0, len(domain) - 1)
    if random.random() < 0.5 and vec[i] > domain[i][0]:
        return vec[0:i] + [vec[i] - step] + vec[i+1:]
    elif vec[i] < domain[i][7]:
        return vec[0:i] + [vec[i] + step] + vec[i+1:]
    else:
        return vec

参考链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值