用Python3.6编写PSO算法的高维约束问题---后续一

最近,对算法的理解慢慢上路了,所以我写了一个稍微完整的PSO算法,就是不知道约束条件添加的位置是否是正确的,欢迎大家进行讨论交流,不胜感激!!!

# -*- coding: utf-8 -*-
"""
Created on Wed Jan 16 09:59:15 2019


"""



import numpy as np  
import random   
import matplotlib.pyplot as plt  



#----------------------PSO参数设置---------------------------------  
class PSO():  
    def __init__(self,pN,dim,max_iter):  
        self.w = 0.8    
        self.c1 = 2     
        self.c2 = 2     
        self.r1= 0.6  
        self.r2=0.3  
        self.pN = pN                #粒子数量  
        self.dim = dim              #搜索维度  
        self.max_iter = max_iter    #迭代次数  
        self.X = np.zeros((self.pN,self.dim))       #所有粒子的位置和速度  
        self.V = np.zeros((self.pN,self.dim))  
        self.pbest = np.zeros((self.pN,self.dim))   #个体经历的最佳位置和全局最佳位置  
        self.gbest = np.zeros((1,self.dim))  
        self.p_fit = np.zeros(self.pN)              #每个个体的历史最佳适应值  
        self.fit = 1e10             #全局最佳适应值
        self.h = np.zeros((self.pN,self.dim))
        self.ps = [69.2201, 67.4473, 67.4473, 67.4473, 67.4473, 67.4473, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0] 
        self.po = [0, 0, 0, 0, 0, 0,272.1346, 251.9091, 251.9091, 251.9091, 251.9091, 251.9091, 0, 0, 0, 0, 0, 0] 
        self.cost = [0.9086, 0.4246, 0.9086, 1.0906, 0.9086, 1.0906, 0.9086, 0.4246, 0.9086, 1.0906, 0.9086, 1.0906,0.9086, 0.4246, 0.9086, 1.0906, 0.9086, 1.0906]     
        self.run_pro_speed = [48.605, 31.0979, 31.0979, 31.0979, 31.0979, 31.0979, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
        self.run_use_speed = [58.7868, 62.9171, 62.9171, 62.9171, 62.9171, 62.9171, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
        self.exit_flow = [0, 0, 0, 0, 0, 0, 59.0325, 68.2907, 68.2907, 68.2907, 68.2907, 68.2907, 0, 0, 0, 0, 0, 0] 
        self.run_use_speed1 = [0, 0, 0, 0, 0, 0, 58.7868, 62.9171, 62.9171, 62.9171, 62.9171, 62.9171, 0, 0, 0, 0, 0, 0] 
        self.paper_pro_speed = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95.4955, 116.6144, 116.6144, 116.6144, 116.6144, 116.6144] 
        self.paper_use_speed = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95.098, 107.4384, 107.4384, 107.4384, 107.4384, 107.4384] 
        
        
#---------------------目标函数Sphere函数-----------------------------  

    def function(self,x):  
        sum = a = b = c = 0  
        #length = len(x)  
        #x = x**2  
        for i in range(self.dim):
            if i < 6:
                a = x[i]*self.ps[i]*self.cost[i] 
            if i > 5 and i < 12:
                    
            
                b = x[i]*self.po[i]*self.cost[i]
            if i > 11 & i < 18:
            
                c = x[i]*0*self.cost[i]
            sum += abs(a + b + c)
        return sum
        
#---------------------初始化种群----------------------------------  
    def init_Population(self):  
        for i in range(self.pN):
            self.X[i][0] = random.uniform(0.5,1)
            self.X[i][1] = random.uniform(1,5)
            self.X[i][2] = random.uniform(0,1.5)
            self.X[i][3] = random.uniform(0.5,2)
            self.X[i][4] = random.uniform(1,5)
            self.X[i][5] = random.uniform(0.5,2)
            self.X[i][6] = random.uniform(1,2)
            self.X[i][7] = random.uniform(4,8)
            self.X[i][8] = random.uniform(0,1.5)
            self.X[i][9] = random.uniform(1,2)
            self.X[i][10] = random.uniform(1,6)
            self.X[i][11] = random.uniform(1,4)
            self.X[i][12] = random.uniform(0,2)
            self.X[i][13] = random.uniform(0,8)
            self.X[i][14] = random.uniform(0,2)
            self.X[i][15] = random.uniform(0,2)
            self.X[i][16] = random.uniform(0,6)
            self.X[i][17] = random.uniform(0,4)
            for j in range(self.dim):  
                
                self.V[i][j] = random.uniform(0,0.01)
                

        
        for i in range(self.pN):
            self.pbest[i] = self.X[i]  
            tmp = self.function(self.X[i])  
            self.p_fit[i] = tmp  
            if(tmp < self.fit):  
                self.fit = tmp  
                self.gbest = self.X[i]  
      
#----------------------更新粒子位置----------------------------------  
    def iterator(self):  
        fitness = []  
        for t in range(self.max_iter):  
            for i in range(self.pN):         #更新gbest\pbest  
               

                
                temp = self.function(self.X[i])  
                if(temp<self.p_fit[i]):      #更新个体最优  
                   self.p_fit[i] = temp  
                   self.pbest[i] = self.X[i]  
                   if(self.p_fit[i] < self.fit):  #更新全局最优  
                       self.gbest = self.X[i]  
                       self.fit = self.p_fit[i]

            for i in range(self.pN):  
                self.V[i] = self.w*self.V[i] + self.c1*self.r1*(self.pbest[i] - self.X[i]) + \
                            self.c2*self.r2*(self.gbest - self.X[i])  
                self.X[i] = self.X[i] + self.V[i]
            for i in range(self.pN):
#约束条件1
                self.h[i][0] = (self.run_pro_speed[0]*self.X[i][0]-self.run_use_speed[0]*self.X[i][6])/100 #h24点
                if self.h[i][0]>0.95:
                    d = self.h[i][0] - 0.95
                    self.X[i][0] =abs( self.X[i][0] - d*100/self.run_pro_speed[0] )
                    self.h[i][0] = 0.95
                    if self.X[i][0] > 2:
                       self.X[i][0] = 2
                    if self.X[i][0] < 0:
                       self.X[i][0] = 0
                if self.h[i][0]<0.20:
                    d = 0.20 - self.h[i][0]
               
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值