差分进化g05测试代码

/*
    Description:
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define Inf    1e-10;

int group;//群体规模    
int dimension;//变量维数
int maxGen;//最大迭代次数    
double CR;//交叉概率

double F;//缩放因子
double *x_min, *x_max;//各个变量的边界,维数为dimension    
double **next , **now;
double *cost;//每个个体的目标函数值,维数为group    
double *x_best;//维数为dimension    

int r[3];

double Func(double *px)
{
        return 3*px[0]+0.000001*pow(px[0], 3.0)\
        +2*px[1]+(0.000002*pow(px[1],3.0))/3.0;
}

double Random()
{
        double ans = rand()%group + 1;
        return ans/group;
}

void RandomInt(int index)//产生三个不同的正整数    
{
        do{
            r[0] = rand()%group;    
        }while(r[0] == index);
        
        do{
            r[1] = rand()%group;
        }while(r[1] == r[0] || r[1] == index);
        
        do{
            r[2] = rand()%group;
        }while(r[2] == r[1] || r[2] == r[0] || r[2] == index);
        
}

void Init()
{
        int i, j;
        dimension = 4;
        group = 10*dimension;
        
        maxGen = 500;
        
        CR    = 0.9;
        
        F     = 0.5;
        
        x_min = (double *)malloc(sizeof(double)*dimension);//new double[dimension];
        x_min[0] = x_min[1] = 0;
        x_min[2] = x_min[3] = -0.55;
        
        x_max = (double *)malloc(sizeof(double)*dimension);//new double[dimension];
        x_max[0] = x_max[1] = 1200;
        x_max[2] = x_max[3] = 0.55;    
        
        x_best = (double *)malloc(sizeof(double)*dimension);//new double[dimension];
        
        now    = (double **)malloc(sizeof(double*)*group);//new double*[group];
        next = (double **)malloc(sizeof(double*)*group);//new double*[group];
        
        /*     初始化种群    */
        for( i = 0; i < group; ++i){
                now[i]    = (double *)malloc(sizeof(double)*dimension);//new double[dimension];
                next[i] = (double *)malloc(sizeof(double)*dimension);//new double[dimension];
                
                
                for( j = 0; j < dimension; ++j)
                     now[i][j] = x_min[j] + Random()*(x_max[j] - x_min[j]);
        }
        
        cost = (double *)malloc(sizeof(double)*group);//new double[group];
}

double Constrain(int index, double *var)
{
        if(index == 0)
            return -var[3] + var[2] - 0.55;
            
        if(index == 1)
            return -var[2] + var[3] - 0.55;
            
         if(index == 2)
             return 1000*sin(-var[2]-0.25) + 1000*sin(-var[3]-0.25)+894.8 - var[0] - Inf ;
         if(index == 3)
             return 0.0-Inf-(1000*sin(-var[2]-0.25) + 1000*sin(-var[3]-0.25)+894.8 - var[0]);
            
            if(index == 4)
                return 1000*sin(var[2]-0.25) + 1000*sin(var[2]-var[3]-0.25)+894.8 - var[1] - Inf ;
            if(index == 5)
                return 0.0-Inf-(1000*sin(var[2]-0.25) + 1000*sin(var[2]-var[3]-0.25)+894.8 - var[1]);
                
             if(index == 6)
                 return 1000*sin(var[3]-0.25) + 1000*sin(var[3]-var[2]-0.25)+1294.8 - Inf;
             if(index == 7)
                 return 0.0-Inf-(1000*sin(var[3]-0.25) + 1000*sin(var[3]-var[2]-0.25)+1294.8);
}

double cta(double q)
{
        if(q < 0.001)
            return 5.0;
        else if(q <= 0.1)
            return 10.0;
        else if(q <= 1.0)
            return 15.0;
        else return 30.0;
}
double Penality(int t, double *var)
{
        int i;
        double ans = 0.0;
        double q, p;
        for( i = 0; i <= 7; ++i){
                q = Constrain(i, var);
                q = (q > 0)?q:0;
                p = (q >= 1)?q:1.0;
                ans += cta(q)*q*p;
        }
            
         return ans*t*sqrt((double)(t));

}
void SelectBest(int t)
{
        int i, j;
        int minIndex = 0;
        double p;
        
        for(i = 0; i < group; ++i){
                p = Penality(t, now[i]);    
                cost[i] = Func(now[i]) + p;
                if(cost[minIndex] > cost[i]){
                        minIndex = i;
                }
        }
        
        for(j = 0; j < dimension; ++j){
                x_best[j] = now[minIndex][j];
        }
}
int main()
{
        
        
     int iter = 0;
     int i, j;
     double *pv;
     double lamda;
     double p;
     int randIndex;    
     double temp;
     srand(time(NULL));    
     Init();
        
     while(iter < maxGen){
             ++iter;
                
            SelectBest(iter);
                
             for(i = 0; i < group; ++i){
                     RandomInt(i);    
                     randIndex = rand()%group;
                        
                     pv = (double *)malloc(sizeof(double)*dimension);//new double[dimension];
                     lamda = 1 - ((double)(iter))/maxGen;    
                        
                     for(j = 0; j < dimension; ++j){
                                /*变异操作*/
                             pv[j] = lamda*(now[r[2]][j]-x_best[j])+x_best[j]+F*(now[r[1]][j]-now[r[0]][j]);
                             //pv[j] = lamda*x_best[j]+(1-lamda)*now[i][j]+F*(now[r[1]][j]-now[r[0]][j]);
                             /*越界处理*/    
                             if(pv[j] < x_min[j]){
                                     pv[j] = x_min[j]+Random()*(now[i][j]-x_min[j]);
                             }
                             if(pv[j] > x_max[j]){
                                     pv[j] = now[i][j]+Random()*(x_max[j]-now[i][j]);
                             }
                                
                             /*交叉操作*/
                             p = Random();    
                             if(p <= CR || j == randIndex){
                                        //NULL operation
                             }
                             else pv[j] = now[i][j];
                     }
                        
                     /*选择操作*/
                     temp = Func(pv) + Penality(iter, pv);
                        
                     if(cost[i] <= temp){
                             memcpy(next[i], now[i], sizeof(double)*dimension);
                     }
                     else{
                             memcpy(next[i], pv, sizeof(double)*dimension);
                     }
             }
                
             for(i = 0; i < group; ++i){
                     for(j = 0; j < dimension; ++j){
                             now[i][j] = next[i][j];
                     }    
             }
     }    
        
     SelectBest(iter);
        
     //打印结果
     printf("The objective function value is : %lf,", cost[0]);
     printf(" with precision %lf\n", Penality(iter,now[0]));
        
        
     for(j = 0; j < dimension; ++j){
             printf("x(%d) = %lf\n", j+1, now[0][j]);
     }
        
     system("pause");
     return 0;
}

本文出自 “东方快翔” 博客,谢绝转载!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
差分进化算法是一种跨越搜索空间的优化方法,用于解决复杂的基于优化目标的问题。在MATLAB中,可以使用以下代码来实现差分进化算法的测试函数。 首先,需要定义目标函数,这是差分进化算法的优化目标。以下是一个简单的目标函数示例: ``` function y = test_func(x) y = sum(x.^2); ``` 接下来,使用MATLAB创建差分进化算法的实现。以下是一个简单的实现示例: ``` function [best_sol, best_val] = differential_evolution(func, N, D, Lb, Ub, max_gen, F, CR) % 初始化种群 pop = Lb + rand(N, D) .* (Ub - Lb); % 计算初始种群的适应度 fitness = feval(func, pop); % 选择最优解 [best_val, best_idx] = min(fitness); best_sol = pop(best_idx, :); % 差分进化算法的迭代过程 for gen = 1:max_gen for i = 1:N % 选择3个不同的个体 rand_idx = randperm(N, 3); idx_1 = rand_idx(1); idx_2 = rand_idx(2); idx_3 = rand_idx(3); % 生成新个体 trial_sol = pop(idx_1, :) + F .* (pop(idx_2, :) - pop(idx_3, :)); % 防止新个体越界 trial_sol(trial_sol < Lb) = Lb(trial_sol < Lb); trial_sol(trial_sol > Ub) = Ub(trial_sol > Ub); % 计算新个体的适应度 trial_val = feval(func, trial_sol); % 更新种群 if trial_val < fitness(i) pop(i, :) = trial_sol; fitness(i) = trial_val; % 更新最优解 if trial_val < best_val best_val = trial_val; best_sol = trial_sol; end end end end end ``` 使用上述代码,可以调用差分进化算法的`differential_evolution`函数来解决特定的优化问题。请注意,参数`func`应为目标函数的函数句柄,`N`是种群大小,`D`是问题的维度,`Lb`和`Ub`是变量的上下界限,`max_gen`是最大迭代次数,`F`是缩放因子,`CR`是交叉概率。 例如,要解决上述示例的目标函数,可以使用以下代码: ``` func = @test_func; % 目标函数 N = 50; % 种群大小 D = 2; % 变量的维度 Lb = [-100, -100]; % 变量的下界限 Ub = [100, 100]; % 变量的上界限 max_gen = 100; % 最大迭代次数 F = 0.8; % 缩放因子 CR = 0.9; % 交叉概率 [best_sol, best_val] = differential_evolution(func, N, D, Lb, Ub, max_gen, F, CR); disp(best_sol); disp(best_val); ``` 以上是一个简单的差分进化算法测试函数的MATLAB代码。实际上,根据问题的不同,可能需要进行适当的调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值