-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
580 additions
and
202 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import copy | ||
|
||
from pyrimidine import * | ||
from pyrimidine.benchmarks.optimization import * | ||
|
||
# generate a knapsack problem randomly | ||
n_bags = 50 | ||
evaluate = Knapsack.random(n=n_bags) | ||
|
||
|
||
class YourIndividual(BinaryChromosome // n_bags): | ||
|
||
def _fitness(self): | ||
return evaluate(self.decode()) | ||
|
||
|
||
class YourPopulation(HOFPopulation): | ||
element_class = YourIndividual | ||
default_size = 20 | ||
|
||
|
||
from pyrimidine.deco import add_memory | ||
|
||
@add_memory({'measure_result': None, 'fitness': None}) | ||
class MyIndividual(QuantumChromosome // n_bags): | ||
|
||
def _fitness(self): | ||
return evaluate(self.decode()) | ||
|
||
def backup(self, check=False): | ||
f = super().fitness | ||
if not check or (self.memory['fitness'] is None or f > self.memory['fitness']): | ||
self._memory = { | ||
'measure_result': self.measure_result, | ||
'fitness': f | ||
} | ||
|
||
|
||
class MyPopulation(HOFPopulation): | ||
|
||
element_class = MyIndividual | ||
default_size = 20 | ||
|
||
def init(self): | ||
for i in self: | ||
i.backup() | ||
super().init() | ||
|
||
def backup(self, check=True): | ||
for i in self: | ||
i.backup(check=check) | ||
|
||
def transition(self, *args, **kwargs): | ||
""" | ||
Update the `hall_of_fame` after each step of evolution | ||
""" | ||
super().transition(*args, **kwargs) | ||
self.backup() | ||
|
||
|
||
stat={'Mean Fitness': 'mean_fitness', 'Best Fitness': 'best_fitness'} | ||
mypop = MyPopulation.random() | ||
|
||
yourpop = YourPopulation([YourIndividual(i.decode()) for i in mypop]) | ||
mydata = mypop.evolve(n_iter=100, stat=stat, history=True) | ||
yourdata = yourpop.evolve(n_iter=100, stat=stat, history=True) | ||
|
||
import matplotlib.pyplot as plt | ||
fig = plt.figure() | ||
ax = fig.add_subplot(111) | ||
yourdata[['Mean Fitness', 'Best Fitness']].plot(ax=ax) | ||
mydata[['Mean Fitness', 'Best Fitness']].plot(ax=ax) | ||
ax.legend(('Mean Fitness', 'Best Fitness', 'Mean Fitness(Quantum)', 'Best Fitness(Quantum)')) | ||
ax.set_xlabel('Generations') | ||
ax.set_ylabel('Fitness') | ||
ax.set_title(f'Demo of (Quantum)GA: {n_bags}-Knapsack Problem') | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.