コード例 #1
0
ファイル: test_mke.py プロジェクト: tuahk/NiaPy
 def setUp(self):
     self.D = 20
     self.x, self.task = rnd.uniform(-2, 2,
                                     self.D), Task(self.D, 230, None,
                                                   MyBenchmark())
     self.sol1, self.sol2, self.sol3 = MkeSolution(x=self.x), MkeSolution(
         task=self.task), MkeSolution(x=self.x)
コード例 #2
0
    def __init__(self, **kwargs):
        r"""Initialize algorithm and create name for an algorithm.

		**Arguments**:
		name {string} -- Full name of algorithm
		shortName {string} -- Short name of algorithm
		NP {integer} -- population size
		D {integer} -- dimension of problem
		nGEN {integer} -- nuber of generation/iterations
		nFES {integer} -- number of function evaluations
		benchmark {object} -- benchmark implementation object
		task {Task} -- task to perform optimization on

		**See**:
		Algorithm.setParameters(self, **kwargs)
		"""
        task = kwargs.get('task', None)
        self.name, self.sName, self.rand = kwargs.get(
            'name',
            'Algorith'), kwargs.get('sName',
                                    'algo'), RandomState(kwargs.get('seed', 1))
        self.task = task if task != None else Task(
            kwargs.get('D', 10), kwargs.get('nFES', 100000), None,
            kwargs.get('benchmark', 'ackley'))
        self.setParameters(**kwargs)
コード例 #3
0
 def setUp(self):
     self.D, self.F, self.CR = 10, 0.9, 0.3
     self.x, self.task = rnd.uniform(10, 50,
                                     self.D), Task(self.D, 230, None,
                                                   MyBenchmark())
     self.s1, self.s2 = SolutionjDE(task=self.task), SolutionjDE(x=self.x,
                                                                 CR=self.CR,
                                                                 F=self.F)
コード例 #4
0
ファイル: algorithm.py プロジェクト: tuahk/NiaPy
    def __init__(self, **kwargs):
        r"""Initialize algorithm and create name for an algorithm.

		**Arguments:**

		name {string} -- Full name of algorithm

		shortName {string} -- Short name of algorithm

		NP {integer} -- population size

		D {integer} -- dimension of problem

		nGEN {integer} -- nuber of generation/iterations

		nFES {integer} -- number of function evaluations

		benchmark {object} -- benchmark implementation object

		task {Task} -- task to perform optimization on

		**Raises:**

		TypeError -- Raised when given benchmark function which does not exists.

		**See**:
		Algorithm.setParameters(self, **kwargs)
		"""
        task, self.name, self.sName, self.Rand = kwargs.pop(
            'task', None), kwargs.pop('name', 'Algorith'), kwargs.pop(
                'sName', 'algo'), rand.RandomState(kwargs.pop('seed', 1))
        self.task = task if task != None else Task(
            kwargs.pop('D', 10),
            kwargs.pop('nFES', 100000),
            kwargs.pop('nGEN', None),
            kwargs.pop('benchmark', 'ackley'),
            optType=kwargs.pop('optType', OptimizationType.MINIMIZATION))
        self.setParameters(**kwargs)
コード例 #5
0
 def setUp(self):
     self.D, self.nFES, self.nGEN = 10, 10, 10
     self.t = Task(self.D, self.nFES, self.nGEN, MyBenchmark())
コード例 #6
0
class TaskTestCase(TestCase):
    def setUp(self):
        self.D, self.nFES, self.nGEN = 10, 10, 10
        self.t = Task(self.D, self.nFES, self.nGEN, MyBenchmark())

    def test_isFeasible_fine(self):
        x = full(self.D, 10)
        self.assertTrue(self.t.isFeasible(x))
        x = full(self.D, -10)
        self.assertTrue(self.t.isFeasible(x))
        x = rnd.uniform(-10, 10, self.D)
        self.assertTrue(self.t.isFeasible(x))
        x = full(self.D, -20)
        self.assertFalse(self.t.isFeasible(x))
        x = full(self.D, 20)
        self.assertFalse(self.t.isFeasible(x))

    def test_nextIter_fine(self):
        for i in range(self.nGEN):
            self.assertFalse(self.t.stopCond())
            self.t.nextIter()
        self.assertTrue(self.t.stopCond())

    def test_stopCondI(self):
        for i in range(self.nGEN):
            self.assertFalse(self.t.stopCondI())
        self.assertTrue(self.t.stopCondI())

    def test_eval_fine(self):
        x = full(self.D, 0.0)
        for i in range(self.nFES):
            self.assertAlmostEqual(self.t.eval(x), 0.0)
        self.assertEqual(self.t.eval(x), inf)

    def test_eval_not_feasible_test(self):
        x = full(self.D, -20)
        self.assertEqual(self.t.eval(x), inf)
        x = full(self.D, 20)
        self.assertEqual(self.t.eval(x), inf)

    def test_eval_over_nFES_fine(self):
        x = full(self.D, 0.0)
        for i in range(self.nFES):
            self.t.eval(x)
        self.assertEqual(self.t.eval(x), inf)

    def test_eval_over_nGEN_fine(self):
        x = full(self.D, 0.0)
        for i in range(self.nGEN):
            self.t.nextIter()
        self.assertEqual(self.t.eval(x), inf)

    def test_nFES_count_fine(self):
        x = full(self.D, 0.0)
        for i in range(self.nFES):
            self.t.eval(x)
            self.assertEqual(self.t.Evals, i + 1,
                             'Error at %s. evaluation' % (i + 1))

    def test_nGEN_count_fine(self):
        x = full(self.D, 0.0)
        for i in range(self.nGEN):
            self.t.nextIter()
            self.assertEqual(self.t.Iters, i + 1,
                             'Error at %s. iteration' % (i + 1))

    def test_stopCond_evals_fine(self):
        x = full(self.D, 0.0)
        for i in range(self.nFES - 1):
            self.t.eval(x)
            self.assertFalse(self.t.stopCond())
        self.t.eval(x)
        self.assertTrue(self.t.stopCond())

    def test_stopCond_iters_fine(self):
        x = full(self.D, 0.0)
        for i in range(self.nGEN - 1):
            self.t.nextIter()
            self.assertFalse(self.t.stopCond())
        self.t.nextIter()
        self.assertTrue(self.t.stopCond())
コード例 #7
0
ファイル: test_algorithm.py プロジェクト: tuahk/NiaPy
	def setUp(self):
		self.D = 20
		self.x, self.task = rnd.uniform(-100, 100, self.D), Task(self.D, 230, None, MyBenchmark())
		self.s1, self.s2, self.s3 = Individual(x=self.x), Individual(task=self.task, rand=rnd), Individual(task=self.task)