moead算法c++代码

时间: 2023-05-26 12:00:55 浏览: 114
A:以下是MOEA/D算法的C代码。 ``` #include <stdio.h> #include <stdlib.h> #include <math.h> #include <float.h> #include <string.h> #define N 1000 //population size #define D 7 //number of decision variables #define M 2 //number of objectives #define NR 1000 //number of iterations #define T 20 //neighbourhood size #define CR 1.0 //crossover probability #define F 0.5 //differential weight #define EPS 1e-6 //epsilon double genrand(){ //random number generator; Mersenne Twister from Matsumoto and Nishimura (1998) unsigned long x[624], y; static int p=0; static unsigned long mag01[2]={0x0UL,0x9908b0dfUL}; int i; if (p == 0) for (i=0;i<624;i++) { y = (x[i] & 0x80000000UL) + (x[(i+1)%624] & 0x7fffffffUL); x[i] = x[(i + 397) % 624] ^ (y >> 1) ^ mag01[y & 0x1UL]; } y = x[p]; p = (p + 1) % 624; y ^= (y >> 11); y ^= (y << 7) & 0x9d2c5680UL; y ^= (y << 15) & 0xefc60000UL; y ^= (y >> 18); return (double)y/(double)((unsigned long)0xffffffffUL+1.0); } void set_ref(){ //initialize reference point int i; for(i=0; i<M; i++) f_ref[i] = DBL_MAX; i=0; while(i<N){ double x[D]; int j; for(j=0; j<D; j++) x[j] = 2.0*genrand() - 1.0; decode(x, N,P,Q,nvar, node, edge); //decode evaluate(x, &f[i*M]); //evaluate int flag=0; int k; for(k=0;k<i;k++){ int l=0; while(l<M && f_ref[l] - f[k*M+l]>EPS) l++; if(l==M){ flag=1; break; } } if(flag==0){ for(k=0;k<M;k++) f_ref[k]=f[k]; i++; } } } double sgn(double x){ if(x<0.0) return -1.0; if(x>0.0) return 1.0; return 0.0; } double power(double x, int n){ double y=1.0; int i; for(i=0;i<n;i++) y *= x; return y; } //---------------------------------------------------------------------------- void func(vector x, vector f){ //multi-objective functions //f1 = x1 //f2 = g(x)h(f1,g(x)) double a=1.0; double b=1.0; int i; for(i=2;i<D;i++){ a += x[i]*x[i] - 10.0*cos(4.0*PI*x[i]); } f[0] = x[0]; f[1] = a*(1.0-sqrt(x[0]/a)-x[0]*sin(10.0*PI*x[0]))*b; } void evaluate(double x[], double f[]){ //evaluate an individual vector vx = alloc_vector(D); vector vf = alloc_vector(M); int i; for(i=0; i<D; i++) vx[i] = x[i]; func(vx,vf); for(i=0; i<M; i++) f[i] = vf[i]; free_vector(vf); free_vector(vx); } //---------------------------------------------------------------------------- void crossover(double xp[], double x1[], double x2[], double x3[]){ //crossover int idx = (int)(genrand()*D); int i; for(i=0; i<D; i++){ if(genrand()<CR || i == idx){ x1[i] = xp[i] + F*(x2[i] - x3[i]); if(x1[i]<-1.0) x1[i] = -1.0; if(x1[i]>1.0) x1[i] = 1.0; }else{ x1[i] = xp[i]; } } } void mutation(double xp[], double x1[]){ //mutation int i; for(i=0; i<D; i++){ x1[i] = xp[i]; if(genrand()<0.1){ x1[i] = 2.0*genrand() - 1.0; } } } void select_parent(int p[], int ci){ //select a parent based on binary tournament selection int i; for(i=0; i<2; i++){ int j = (int)(genrand()*N); if(f[j*M+ci]<f[p[i]*M+ci]){ p[i] = j; } } } void moead(){ //MOEA/D -- main procedure int t, i, j, k; for(i=0;i<N;i++) for(j=0;j<M;j++) f[i*M+j]=0.0; set_ref(); //initialize reference point for(t=0; t<NR; t++){ printf("# %d\n",t); for(i=0; i<N; i++){ //mutate every individual int p[2] = {i, i}; select_parent(p, 0); crossover(x[p[0]], xp, x[p[1]], x[perm[i][genrand()*T]]); mutation(xp, x1); decode(x1, N,P,Q,nvar, node, edge); evaluate(x1, f1); for(j=0; j<T; j++){ //update neighbourhood individuals k = nei[i][j]; double nf[M]; evaluate(x[k], nf); int flag=0; int l=0; while(l<M && nf[l]-f[k*M+l]<EPS) l++; if(l==M){ double dis = 0.0; int p; for(p=0;p<D;p++) dis += pow(x1[p]-x[k][p],2.0); if(dis < eps){ memcpy(x[k], x1, sizeof(double)*D); memcpy(f+k*M, f1, sizeof(double)*M); }else if(genrand()<0.5){ memcpy(x[k], x1, sizeof(double)*D); memcpy(f+k*M, f1, sizeof(double)*M); } } } } } } //---------------------------------------------------------------------------- int main(int argc, char *argv[]){ init(); moead(); return 0; } ``` 请注意这是一个示例代码,并且可能不完全符合您的实际需求。

相关推荐

最新推荐

recommend-type

C++递归算法实例代码

主要介绍了C++递归算法实例代码,还是比较不错的,运用了递归算法解决相关问题,这里分享给大家,需要的朋友可以参考下。
recommend-type

用C++实现DBSCAN聚类算法

本篇文章是对使用C++实现DBSCAN聚类算法的方法进行了详细的分析介绍,需要的朋友参考下
recommend-type

C++实现分水岭算法(Watershed Algorithm)

主要为大家详细介绍了C++实现分水岭算法Watershed Algorithm,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

遗传算法c++代码,车间调度

主要是关于车间遗传算法的代码,仅仅参考,当然交流之用,我也是从别人那里拷来的
recommend-type

c++代码实现tea加密算法的实例详解

主要介绍了c++代码实现tea加密算法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。