废话不多说,大家直接看代码吧!
"""遗传算法实现求函数极大值—Zjh"""
import numpy as np
import random
import matplotlib.pyplot as plt
class Ga():
"""求出二进制编码的长度"""
def __init__(self):
self.boundsbegin = -2
self.boundsend = 3
precision = 0.0001 # 运算精确度
self.Bitlength = int(np.log2((self.boundsend - self.boundsbegin)/pre