联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> javajava

日期:2018-09-16 02:10



from pyevolve import GSimpleGA

from pyevolve import G1DList

from pyevolve import Selectors

from pyevolve import Initializators, Mutators

from pyevolve import Consts

import math

import numpy as np

n=8      #number of workpiece

m=8       #number of CNC

wait=560 #CNC to work

A_1=np.array(

[[28,31,28,31,28,31,28,31]]*8      #TIME TO LOAD

)


WASH_TIME = 25   #TIME TO WASH



A_3=np.array([         #TIME TO MOVE

[0,    0,    20,    20,    33,    33,    46,    46],

[0,    0,    20,    20,    33,    33,    46,    46],

[20,    20,    0,    0,    20,    20,    33,    33],

[20,    20,    0,    0,    20,    20,    33,    33],

[33,    33,    20,    20,    0,    0,    20,    20],

[33,    33,    20,    20,    0,    0,    20,    20],

[46,    46,    33,    33,    20,    20,    0,    0],

[46,    46,    33,    33,    20,    20,    0,    0]

])


time_cost=0

time_wait=np.array([0,0,0,0,0,0,0,0])

TIME_MAX=8*60*60

info = np.array([[0]*4]*400)


def eval_func(ind):

   info = np.array([[0]*4]*400)

   time_wait=np.array([0,0,0,0,0,0,0,0])

   # time.sleep(1)

   time_used=0.0

   score = 0

   for x in range(0, len(ind)):

       ind[x]=int(ind[x])

   for x in range(0,len(ind)):


       info[x][0]=x+1

       info[x][1]=ind[x]+1

       info[x][2] = time_used

       if(x!=len(ind)-1):       #step 1

           time_cost = A_1[ind[x]][ind[x+1]]

       else :

           time_cost = A_1[ind[x]][ind[0]]

       time_used += time_cost


       for i in range(0,m):

           time_wait[i] -= time_cost

           if(time_wait[i]<0):

               time_wait[i]=0

       time_wait[ind[x]]=wait


       if(x!=len(ind)-1):   #step 2

           time_cost = A_3[ind[x]][ind[x+1]]

       else :

           time_cost = A_3[ind[x]][ind[0]]

       time_used += time_cost

       for i in range(0,m):

           time_wait[i] -= time_cost

           if(time_wait[i]<0):

               time_wait[i]=0


   while(1):    #step 3

       x=score % m

       wait_time = time_wait[ind[x]]

       info[score+m][2] = time_used + wait_time

       if(x != n-1):  #TIME TO LOAD and wait

           time_cost = wait_time + A_1[ind[x]][ind[x+1]] + WASH_TIME

       else :

           time_cost = wait_time + A_1[ind[x]][0] + WASH_TIME

       time_used += time_cost

       info[score+m][0] = score + m + 1

       info[score+m][1] = ind[x]+1

       

       info[score][3] = time_used - WASH_TIME



       for i in range(0,m):  

           time_wait[i] -= time_cost

           if(time_wait[i] < 0 ):

               time_wait[i]=0

       time_wait[ind[score%m]]=wait

       if(time_used > TIME_MAX):

           break

       

       if(score % m != n-1):

           time_cost = A_3[ind[x]][ind[x+1]]

       else :

           time_cost = A_3[ind[score%m]][0]

       time_used += time_cost

       for i in range(0,m):  

           time_wait[i] -= time_cost

           if(time_wait[i]<0):

               time_wait[i]=0

       score += 1

   print (info)

   np.savetxt("ques_1_1.txt",info)

   return score



# Genome instance

genome = G1DList.G1DList(n)

genome.setParams(rangemin=0, rangemax=7.999)


# genome = G1DBinaryString.G1DBinaryString(27)


# Change the initializator to Real values

genome.initializator.set(Initializators.G1DListInitializatorReal)


# Change the mutator to Gaussian Mutator

genome.mutator.set(Mutators.G1DListMutatorRealGaussian)


# The evaluator function (objective function)

genome.evaluator.set(eval_func)


# Genetic Algorithm Instance

ga = GSimpleGA.GSimpleGA(genome)

ga.minimax = Consts.minimaxType["maximize"]

ga.selector.set(Selectors.GRouletteWheel)

# ga.setPopulationSize()

ga.nGenerations = 50


# Do the evolution

ga.evolve(10)


# Best individualsss

be = ga.bestIndividual()





eval_func(be)


# exp=[0,1,2,3,4,5,6,7]

# print eval_func(exp)


版权所有:留学生程序网 2020 All Rights Reserved 联系方式:QQ:99515681 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。