使用遗传算法求解三元函数最大值

使用遗传算法求解三元函数最大值


使用遗传算法求解三元函数z的最大值
z=f(x,y)=xsin(4πx)-ysin(4πy+π)+1
在-1<x<1和-1<y<1上的最大值

#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1)
//目标函数 
double f(double x,double y){
	return x*sin(4*pi*x)-y*sin(4*pi*y+pi)+1;
}
//编码转真实数字 
double get_n(int a[40],int k){
	double ans=0;
	for(int i=k;i<k+15;i++){
		ans=ans*2+a[i];
	}
	ans=(ans-16384)/16384;
	return ans;
}
//随机数生成函数 
int random(){	
	int r = rand() % 100 + 1;
	return (r<=50)?0:1;
}
double random2(){
	double r = (rand() % 100 + 1)*1.0/100;
	return r;
}
int random3(){
	double r = rand() % 30 ;
	return r;
}
//个体 
struct node{
	int code[40];
	double n_x;
	double n_y;
	double ans;
	node(){
		for(int i=0;i<30;i++){
			code[i]=random();
		}
		n_x=get_n(code,0);
		n_y=get_n(code,15);
		ans=f(n_x,n_y);
	}
	void init(int a[40]){
		for(int i=0;i<30;i++){
			code[i]=a[i];
		}
		n_x=get_n(code,0);
		n_y=get_n(code,15);
		ans=f(n_x,n_y);
	}
}n[55];
double get_f(node x){
	return f(get_n(x.code,0),get_n(x.code,15));
}
//得到每个节点被随机抽取的范围 
double p[55];
void set_p(){
	double sum=0;
	for(int i=0;i<50;i++){
		sum+=n[i].ans;
	}
	p[0]=n[0].ans/sum;
	for(int i=1;i<50;i++){
		p[i]=p[i-1]+n[i].ans/sum;
	}
}
//随机抽取父个体 
int get_parent(){
	double r = random2();
	int parent=0;
	for(int i=1;i<50;i++){
		if(r>p[i-1]&&r<=p[i]){
			parent=i;
			break;
		}
	}
	return parent;
}
//输出个体信息 
void put_node(node x){
	for(int i=0;i<30;i++)
		printf("%d",x.code[i]);
	printf("\n%lf\n",x.ans);
}
//初始群体 
void initpop(){
	for(int i=0;i<50;i++){
		n[i]=node();
		while(n[i].ans<0){
			n[i]=node();
		}
	}	
}
//变异 
node get_vari(node x){
	node temp=x;
	double p_vari = random2();
	int k=random3();
	if(p_vari<=0.8){
		temp.code[k]=(temp.code[k]+1)%2;
	}
	temp.init(temp.code);
	return temp;
}
//交叉 
node get_child(){
	int parent1=get_parent();
	int parent2=get_parent(); 
	int k=random3();
	int temp[40];
	for(int i=0;i<=k;i++){
		temp[i]=n[parent1].code[i];
	}
	for(int i=k+1;i<30;i++){
		temp[i]=n[parent2].code[i];
	}
	node child;
	child.init(temp);
	child=get_vari(child);
	if(child.ans<0)
		child=get_child();
	if(child.ans<n[parent1].ans)
		child=n[parent1];
	if(child.ans<n[parent2].ans)
		child=n[parent2];
	return child;
}


int main(){
	srand((unsigned)time(NULL));
	initpop();
	node children[55]; 
	double mmax=-1;
	node anse;
	double maxx=-1;
	for(int i=0;i<20;i++){//生成20代 
		printf("----------第%d代----------\n",i+1);
		set_p();
		for(int i=0;i<50;i++){
			children[i]=get_child();
			if(children[i].ans>maxx){
				maxx=children[i].ans;
				anse=children[i];
			}
		}
		for(int i=0;i<50;i++){
			n[i]=children[i];
		}
		put_node(anse);
	}
} 

参考使用遗传算法求解函数最值

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
遗传算法是一种基于自然选择和遗传学原理的优化算法,可以用于求解函数极值问题。下面是遗传算法求解三元函数极值的步骤: 1.定义适应度函数:对于三元函数f(x1,x2,x3)=x1*x1-x1*x2+x3,我们可以将其作为适应度函数,即fitness(x1,x2,x3)=f(x1,x2,x3)。 2.初始化种群:随机生成一定数量的个体,每个个体由三个实数编码表示,即x1、x2、x3。 3.选择操作:采用轮盘赌选择算子,根据个体适应度大小选择优秀的个体。 4.交叉操作:采用单点交叉算子,对选出的个体进行交叉操作,生成新的个体。 5.变异操作:对新生成的个体进行变异操作,增加种群的多样性。 6.评估函数:计算每个个体的适应度值。 7.选择优秀个体:根据适应度值选择优秀的个体。 8.重复执行步骤3-7,直到达到预设的迭代次数或找到最优解。 下面是Python代码实现: ```python import random # 定义适应度函数 def fitness(x1, x2, x3): return x1 * x1 - x1 * x2 + x3 # 初始化种群 def init_population(pop_size): population = [] for i in range(pop_size): x1 = random.uniform(-10, 10) x2 = random.uniform(-10, 10) x3 = random.uniform(-10, 10) population.append((x1, x2, x3)) return population # 选择操作 def selection(population): fitness_list = [fitness(*individual) for individual in population] total_fitness = sum(fitness_list) probability_list = [fitness / total_fitness for fitness in fitness_list] selected_population = [] for i in range(len(population)): selected_individual = random.choices(population, weights=probability_list)[0] selected_population.append(selected_individual) return selected_population # 交叉操作 def crossover(population): offspring_population = [] for i in range(len(population)): parent1 = population[i] parent2 = random.choice(population) crossover_point = random.randint(0, 2) offspring = parent1[:crossover_point] + parent2[crossover_point:] offspring_population.append(offspring) return offspring_population # 变异操作 def mutation(population, mutation_rate): mutated_population = [] for individual in population: if random.random() < mutation_rate: mutated_individual = list(individual) mutated_individual[random.randint(0, 2)] += random.uniform(-1, 1) mutated_population.append(tuple(mutated_individual)) else: mutated_population.append(individual) return mutated_population # 遗传算法求解三元函数极值 def genetic_algorithm(pop_size, max_iter, mutation_rate): population = init_population(pop_size) for i in range(max_iter): selected_population = selection(population) offspring_population = crossover(selected_population) mutated_population = mutation(offspring_population, mutation_rate) population = mutated_population best_individual = max(population, key=lambda x: fitness(*x)) return best_individual # 测试 best_individual = genetic_algorithm(100, 1000, 0.1) print("最优解:", best_individual) print("最优解对应的函数值:", fitness(*best_individual)) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值