# -*- coding: utf-8 -*-
import sys
import numpy as np
from func_timeout import func_timeout, FunctionTimedOut
import matplotlib.pyplot as plt
import multiprocessing
import os
class GA():
def __init__(self, function, dimension, variable_type='bool', variable_type_mixed=None,
variable_boundaries=None, function_timeout=5000,
algorithm_parameters=None):
'''
This is the call of the class with the parameters of the genetic
algorithm
Parameters
----------
function : Function
The callable fitness function used to evaluate the parameters.
dimension : int
number of parameters of the problem.
variable_type : str, optional
The variables types. it can be either 'bool', 'int' or 'real';
'int' represent discrete integer variables while 'real' represent
continuous real variables. This however will be not be used if
variable_type_mixed is set to a value. The default is 'bool'.
variable_type_mixed : numpy array, optional
A numpy array of strings with length equal to the dimension,
defining each variable type either 'bool', 'int' or 'real'.
The default is None which means it will not be used.
variable_boundaries : numpy array
a numpy array of length equal to the dimension. each element
consists of a list of 2 elements [min,max]. The default is None
which will raise an error.
function_timeout : float, optional
maximum time in seconds for fitness function evaluation.
The default is 5000.
algorithm_parameters : dictionary
A dictionary of algorithm additional parameters
'population_size': int, size of population
'mutation_probability': float, probability of mutation
'elit_ratio': float, ratio of elite children which will be taken
'crossover_probability': float, probability of cross-over
'parents_portion': float, parents portion of the population size
'crossover_type': str, can be either 'uniform', 'one_point', or
'two_point'
'max_iteration_without_improv': int or None
'Population_file_path': str, path for population file save
'Number_of_processes': int or 'max'
Returns
-------
object of the class.
'''
#############################################################
# input function
assert (callable(function)), "function must be callable"
self.f = function
#############################################################
# dimension
self.dim = int(dimension)
#############################################################
# input variable type
assert (variable_type == 'bool' or variable_type == 'int' or \
variable_type == 'real'), \
"\n variable_type must be 'bool', 'int', or 'real'"
#############################################################
# input variables' type (MIXED)
if variable_type_mixed is None:
if variable_type == 'real':
self.var_type = np.array([['real']] * self.dim)
else:
self.var_type = np.array([['int']] * self.dim)
else:
assert (type(variable_type_mixed).__module__ == 'numpy'), \
"\n variable_type must be numpy array"
assert (len(variable_type_mixed) == self.dim), \
"\n variable_type must have a length equal dimension."
for i in variable_type_mixed:
assert (i == 'real' or i == 'int'), \
"\n variable_type_mixed is either 'int' or 'real' " + \
"ex:['int','real','real']" + \
"\n for 'boolean' use 'int' and specify boundary as [0,1]"
self.var_type = variable_type_mixed
#############################################################
# input variables' boundaries
assert (not isinstance(variable_boundaries, type(None))), "Please define variable boundaries"
if variable_type != 'bool' or type(variable_type_mixed).__module__ == 'numpy':
assert (type(variable_boundaries).__module__ == 'numpy'), \
"\n variable_boundaries must be numpy array"
assert (len(variable_boundaries) == self.dim), \
"\n variable_boundaries must have a length equal dimension"
for i in variable_boundaries:
assert (len(i) == 2), \
"\n boundary for each variable must be a tuple of length two."
assert (i[0] <= i[1]), \
"\n lower_boundaries must be smaller than upper_boundaries [lower,upper]"
self.var_bound = variable_boundaries
else:
self.var_bound = np.array([[0, 1]] * self.dim)
#############################################################
# Timeout
self.funtimeout = float(function_timeout)
#############################################################
# input algorithm's parameters
assert (algorithm_parameters != None), "Please define algorithm parameters."
param = algorithm_parameters
self.pop_s = int(param['population_size'])
assert (param['parents_portion'] <= 1 \
and param['parents_portion'] >= 0), \
"parents_portion must be in range [0,1]"
self.par_portion = param['parents_portion']
self.par_s = int(self.par_portion * self.pop_s)
trl = self.pop_s - self.par_s
if trl % 2 != 0:
self.par_s += 1
self.prob_mut = param['mutation_probability']
assert (self.prob_mut <= 1 and self.prob_mut >= 0), \
"mutation_probability must be in range [0,1]"
self.prob_cross = param['crossover_probability']
assert (self.prob_cross <= 1 and self.prob_cross >= 0), \
"mutation_probability must be in range [0,1]"
assert (param['elit_ratio'] <= 1 and param['elit_ratio'] >= 0), \
"elit_ratio must be in range [0,1]"
self.elite_ratio = param['elit_ratio']
trl = self.pop_s * self.elite_ratio
if trl < 1 and self.elite_ratio > 0:
self.num_elit = 1
else:
self.num_elit = int(trl)
assert (self.par_s >= self.num_elit), \
"\n number of parents must be greater than number of elits"
if param['max_num_iteration'] == None:
self.iterate = 0
for i in range(0, self.dim):
if self.var_type[i] == 'int':
self.iterate += (self.var_bound[i][1] - self.var_bound[i][0]) * self.dim * (100 / self.pop_s)
else:
self.iterate += (self.var_bound[i][1] - self.var_bound[i][0]) * 50 * (100 / self.pop_s)
self.iterate = int(self.iterate)
if (self.iterate * self.pop_s) > 10000000:
self.iterate = 10000000 / self.pop_s
else:
self.iterate = int(param['max_num_iteration'])
self.c_type = param['crossover_type']
assert (self.c_type == 'uniform' or self.c_type == 'one_point' or \
self.c_type == 'two_point'), \
"\n crossover_type must 'uniform', 'one_point', or 'two_point' Enter string"
self.stop_mniwi = False
if param['max_iteration_without_improv'] == None:
self.mniwi = self.iterate + 1
else:
self.mniwi = int(param['max_iteration_without_improv'])
###########
没有合适的资源?快使用搜索试试~ 我知道了~
python+算法+并行遗传算法GA
共2个文件
py:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 43 浏览量
2024-03-23
11:01:56
上传
评论
收藏 6KB ZIP 举报
温馨提示
python+算法+并行遗传算法GA,以进程进行并行优化
资源推荐
资源详情
资源评论
收起资源包目录
GA.zip (2个子文件)
GA
main.py 1KB
Genetic_algorithm.py 31KB
共 2 条
- 1
资源评论
资源存储库
- 粉丝: 4501
- 资源: 392
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功