Skip to content

Commit

Permalink
Added deleted class unpickling
Browse files Browse the repository at this point in the history
  • Loading branch information
fmder committed Jul 19, 2023
1 parent df3ba81 commit bfccb17
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ def test_pickle_ind_ndarray(self):
self.assertTrue(all(ind == ind_l), "Unpickled individual numpy.ndarray != pickled individual numpy.ndarray")
self.assertEqual(ind.fitness, ind_l.fitness, "Unpickled individual fitness != pickled individual fitness")

def test_pickle_delete_ind_list(self):
creator.create("TempInd", list, fitness=creator.FitnessMax)
ind = creator.TempInd([1.0, 2.0, 3.0])
del creator.TempInd
ind.fitness.values = (4.0,)
ind_s = pickle.dumps(ind)
ind_l = pickle.loads(ind_s)
self.assertEqual(ind, ind_l, "Unpickled individual list != pickled individual list")
self.assertEqual(ind.fitness, ind_l.fitness, "Unpickled individual fitness != pickled individual fitness")

def test_pickle_tree_input(self):
pset = gp.PrimitiveSetTyped("MAIN", [int], int, "IN")
pset.addPrimitive(operator.add, [int, int], int)
Expand All @@ -79,9 +89,9 @@ def test_pickle_tree_input(self):
ind.fitness.values = (1.0,)
ind_s = pickle.dumps(ind, pickle.HIGHEST_PROTOCOL)
ind_l = pickle.loads(ind_s)
msg = "Unpickled individual %s != pickled individual %s" % (str(ind), str(ind_l))
msg = "Unpickled individual %s != pickled individual %s" % (str(ind), str(ind_l))
self.assertEqual(ind, ind_l, msg)
msg = "Unpickled fitness %s != pickled fitness %s" % (str(ind.fitness), str(ind_l.fitness))
msg = "Unpickled fitness %s != pickled fitness %s" % (str(ind.fitness), str(ind_l.fitness))
self.assertEqual(ind.fitness, ind_l.fitness, msg)

def test_pickle_tree_term(self):
Expand All @@ -94,9 +104,9 @@ def test_pickle_tree_term(self):
ind.fitness.values = (1.0,)
ind_s = pickle.dumps(ind, pickle.HIGHEST_PROTOCOL)
ind_l = pickle.loads(ind_s)
msg = "Unpickled individual %s != pickled individual %s" % (str(ind), str(ind_l))
msg = "Unpickled individual %s != pickled individual %s" % (str(ind), str(ind_l))
self.assertEqual(ind, ind_l, msg)
msg = "Unpickled fitness %s != pickled fitness %s" % (str(ind.fitness), str(ind_l.fitness))
msg = "Unpickled fitness %s != pickled fitness %s" % (str(ind.fitness), str(ind_l.fitness))
self.assertEqual(ind.fitness, ind_l.fitness, msg)

def test_pickle_tree_ephemeral(self):
Expand All @@ -109,17 +119,17 @@ def test_pickle_tree_ephemeral(self):
ind.fitness.values = (1.0,)
ind_s = pickle.dumps(ind, pickle.HIGHEST_PROTOCOL)
ind_l = pickle.loads(ind_s)
msg = "Unpickled individual %s != pickled individual %s" % (str(ind), str(ind_l))
msg = "Unpickled individual %s != pickled individual %s" % (str(ind), str(ind_l))
self.assertEqual(ind, ind_l, msg)
msg = "Unpickled fitness %s != pickled fitness %s" % (str(ind.fitness), str(ind_l.fitness))
msg = "Unpickled fitness %s != pickled fitness %s" % (str(ind.fitness), str(ind_l.fitness))
self.assertEqual(ind.fitness, ind_l.fitness, msg)

def test_pickle_population(self):
ind1 = creator.IndList([1.0,2.0,3.0])
ind1 = creator.IndList([1.0, 2.0, 3.0])
ind1.fitness.values = (1.0,)
ind2 = creator.IndList([4.0,5.0,6.0])
ind2 = creator.IndList([4.0, 5.0, 6.0])
ind2.fitness.values = (2.0,)
ind3 = creator.IndList([7.0,8.0,9.0])
ind3 = creator.IndList([7.0, 8.0, 9.0])
ind3.fitness.values = (3.0,)

pop = [ind1, ind2, ind3]
Expand All @@ -141,7 +151,7 @@ def test_pickle_logbook(self):
logbook = tools.Logbook()

stats.register("mean", numpy.mean)
record = stats.compile([1,2,3,4,5,6,8,9,10])
record = stats.compile([1, 2, 3, 4, 5, 6, 8, 9, 10])
logbook.record(**record)

logbook_s = pickle.dumps(logbook)
Expand Down

0 comments on commit bfccb17

Please sign in to comment.