100 lines
3.6 KiB
Python
Executable file
100 lines
3.6 KiB
Python
Executable file
#!/usr/bin/python3
|
|
# Copyright 2020, 2023 Weber Yann
|
|
#
|
|
# This file is part of rpnifs.
|
|
#
|
|
# geneifs is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# geneifs is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with geneifs. If not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
|
|
import random
|
|
import unittest
|
|
|
|
|
|
try:
|
|
from tqdm import tqdm
|
|
except (ImportError, NameError) as e:
|
|
tqdm = lambda i, *args, **kwargs:iter(i)
|
|
|
|
try:
|
|
import pyrpn
|
|
except (ImportError, NameError) as e:
|
|
print("Error importing pyrpn. Try to run make.",
|
|
file=sys.stderr)
|
|
raise e
|
|
|
|
## TODO : test w_add_elt and w_mut_elt
|
|
|
|
class TestRpnExprMutation(unittest.TestCase):
|
|
|
|
def test_base_mutation(self):
|
|
""" Testing mutation without parameters """
|
|
expr = pyrpn.RPNExpr("", 3)
|
|
expr.mutate()
|
|
expr.mutate(n_mutations=10)
|
|
|
|
def test_mutation_params_len(self):
|
|
""" Testing some value for parameters checking resulting expr len """
|
|
|
|
tests = (
|
|
([1,1,0,0,0,(0,0,0),(0,0,0)], [1,0,100]),
|
|
([1,2,1,0,0,(0,0,0),(0,0,0)], [1/3]),
|
|
([1,2,0,1,0,(0,0,0),(0,0,0)], [2/3]),
|
|
([1,3,1,2,0,(0,0,0),(0,0,0)], [2/6]),
|
|
)
|
|
|
|
for p_args, f_args in tests:
|
|
params = pyrpn.RPNMutationParamsTuple(p_args)
|
|
with self.subTest(params=params):
|
|
self._generic_mutation_params_lentest(params, *f_args)
|
|
|
|
def test_random_int_mutation_params(self):
|
|
""" Testing int random values for parameters checking resulting expr len """
|
|
for _ in range(50):
|
|
m_params = [random.randint(1,20) for _ in range(4)]
|
|
expt_len = (m_params[0]-m_params[1])/sum(m_params)
|
|
expt_len = 0 if expt_len < 0 else expt_len
|
|
params = [1] + m_params + [(0,0,0),(0,0,0)]
|
|
params = pyrpn.RPNMutationParamsTuple(params)
|
|
with self.subTest(params=params):
|
|
self._generic_mutation_params_lentest(params, expt_len)
|
|
|
|
def test_random_float_mutation_params(self):
|
|
""" Testing float random values for parameters checking resulting expr len """
|
|
for _ in range(50):
|
|
m_params = [random.randint(0,20)/20 for _ in range(4)]
|
|
expt_len = (m_params[0]-m_params[1])/sum(m_params)
|
|
expt_len = 0 if expt_len < 0 else expt_len
|
|
params = [1] + m_params + [(0,0,0),(0,0,0)]
|
|
params = pyrpn.RPNMutationParamsTuple(params)
|
|
with self.subTest(params=params):
|
|
self._generic_mutation_params_lentest(params, expt_len)
|
|
|
|
def _generic_mutation_params_lentest(self, params, expt_len, percent_error=.05,n_mut=5000):
|
|
|
|
expt_len = int(expt_len*n_mut)
|
|
|
|
expr = pyrpn.RPNExpr("", 3)
|
|
expr.mutate(n_mut, params)
|
|
msg = "Expected a len of %d ( [%d..%d] with %.1f%% error) but got %d"
|
|
self.assertTrue(abs(len(expr) - expt_len) <= n_mut*percent_error,
|
|
msg=msg % (expt_len,
|
|
expt_len + n_mut*percent_error,
|
|
expt_len - n_mut*percent_error,
|
|
percent_error*100,
|
|
len(expr)))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|
|
|