Skip to content

Commit

Permalink
ploting parameters added
Browse files Browse the repository at this point in the history
  • Loading branch information
salehafzoon committed Jun 25, 2020
1 parent 6a7402a commit 04fcd4f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 37 deletions.
Binary file added hw6_PSO/Results2.xlsx
Binary file not shown.
Binary file modified hw6_PSO/document.docx
Binary file not shown.
77 changes: 40 additions & 37 deletions hw6_PSO/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@

TEST_FUNC = None
BOUND = None
# Ns = [10, 30, 50]
Ns = [50]

Ns = [10, 30, 50]
# Ns = [10, 30]

wMax = 0.9
wMin = 0.2
wC = 0.7

wC = 0.5

# 40000 evaluation
ITERATIONS = 500
SWARM_SIZE = 80

DEBUG = False
TIMER_MODE = False
EXEl_WRITE = False
TIMER_MODE = True
EXEl_WRITE = True

c1Min = c2Min = 0.5
c1Max = c2Max = 2.5
Expand Down Expand Up @@ -73,6 +73,7 @@ def positioning(self):

for i in range(self.n):
self.x[i] = rn.uniform(-BOUND, BOUND)
# self.x[i] = 0
self.v[i] = 0.1 * self.x[i]

self.calculate_f()
Expand Down Expand Up @@ -175,11 +176,12 @@ def updateVelocityAndPos(self, gbest, t):
self.updateW(t)

def updateW(self, t):
# self.w *= wC
# self.w = wMax/math.log(t+1)
self.w = math.exp(-0.9 * t+1)*wMax
# self.w = max(wMax * wC, wMin)
# self.w = max(wMax/math.log(t+1), wMin)
# self.w = max(math.exp(-0.99 * t+1) * 2.24, wMin)

self.w = min(self.w, wMin)
# self.w = wMin
self.w = min(math.exp(-0.9 * t+1)*wMax, wMin)

def __str__(self):
return "x: " + str(self.x[:5]) + "f(x): " + str(self.f)
Expand Down Expand Up @@ -249,7 +251,10 @@ def writeResultToExel(answers, myRow):
avgTime = sum(ans[2] for ans in answers)/len(answers)
maxTime = max(answers, key=lambda t: t[2])

wbkName = 'Results.xlsx'
wbkName = 'Results2.xlsx'
if TIMER_MODE:
wbkName = 'one_min_Results.xlsx'

wbk = openpyxl.load_workbook(wbkName)
for wks in wbk.worksheets:
myCol = 2
Expand All @@ -269,46 +274,37 @@ def writeResultToExel(answers, myRow):
wbk.close


def plotResult(generation, bests, averages):

plt.plot(list(range(generation)), bests, '-', color="gray",
label='best of generations', linewidth=1.52)

plt.xlabel('best solution')
plt.ylabel('generation')
def plotParameters(parameters):

plt.show()
# print(parameters)

plt.plot(list(range(generation)), averages, 'bo-',
label='avg of generations', linewidth=1.5)
plt.plot(list(range(ITERATIONS)), [y[0] for y in parameters], '-', color="gray",
label='gbest per iteration', linewidth=1.52)

plt.xlabel('best solution')
plt.ylabel('generation')
plt.xlabel('iteration')
plt.ylabel('gbest')

plt.show()

plt.plot(list(range(ITERATIONS)), [y[1] for y in parameters], 'bo-',
label='Inertia weight per iteration', linewidth=1.5)

def plotProgress(bests, avgs):

plt.plot(list(range(len(bests))), bests, '-', color="gray",
label='best of generations', linewidth=1.5)

plt.xlabel('best solution')
plt.ylabel('generation')

plt.xlabel('iteration')
plt.ylabel('W')
plt.show()

plt.plot(list(range(len(avgs))), avgs, 'bo-',
label='avg of generations', linewidth=1.5)
plt.plot(list(range(ITERATIONS)), [y[2] for y in parameters], 'bo-',
label='velocity per iteration', linewidth=1.5)

plt.xlabel('avg solution')
plt.ylabel('generation')
plt.xlabel('iteration')
plt.ylabel('V')

plt.show()


def PSO(SWARM_SIZE, N, ITERATIONS=50, DEBUG=True):

parameters = []
timer = time.time()
if TIMER_MODE:
ITERATIONS = 10000000000
Expand All @@ -318,6 +314,10 @@ def PSO(SWARM_SIZE, N, ITERATIONS=50, DEBUG=True):

for i in range(ITERATIONS):

if TIMER_MODE and time.time()-timer > 60:
print('time out')
break

for particle in swarm.particles:
particle.updatePbest()

Expand All @@ -330,7 +330,10 @@ def PSO(SWARM_SIZE, N, ITERATIONS=50, DEBUG=True):
print("iteration:", i, "gbest: ",
swarm.gbestVal, "\t", [round(x, 3) for x in swarm.gbest[:10]])

# print("iteration:", i, "gbest: ", swarm.gbestVal)
parameters.append(
(round(swarm.gbestVal, 3), swarm.particles[0].w, swarm.particles[0].v[0]))

# plotParameters(parameters)

return (swarm.gbest, swarm.gbestVal)

Expand All @@ -344,7 +347,7 @@ def PSO(SWARM_SIZE, N, ITERATIONS=50, DEBUG=True):
else:
run = 10

for test_func in FUNCTIONS[4:]:
for test_func in FUNCTIONS:
(TEST_FUNC, BOUND) = test_func
row += 3

Expand Down
Binary file added hw6_PSO/one_min_Results.xlsx
Binary file not shown.
Binary file modified hw6_PSO/~$cument.docx
Binary file not shown.
Binary file removed hw6_PSO/~WRL1918.tmp
Binary file not shown.

0 comments on commit 04fcd4f

Please sign in to comment.