examples/runner.py 1 location
|
@@ 24-33 (lines=10) @@
|
| 21 |
|
|
| 22 |
|
"""Example demonstrating the use of NiaPy Runner.""" |
| 23 |
|
|
| 24 |
|
class MyBenchmark(Benchmark): |
| 25 |
|
def __init__(self): |
| 26 |
|
Benchmark.__init__(self, -10, 10) |
| 27 |
|
|
| 28 |
|
def function(self): |
| 29 |
|
def evaluate(D, sol): |
| 30 |
|
val = 0.0 |
| 31 |
|
for i in range(D): val += sol[i] ** 2 |
| 32 |
|
return val |
| 33 |
|
return evaluate |
| 34 |
|
|
| 35 |
|
runner = Runner( |
| 36 |
|
D=40, |
examples/advanced_example_custom_pop.py 1 location
|
@@ 13-22 (lines=10) @@
|
| 10 |
|
from numpy import random as rand, apply_along_axis |
| 11 |
|
|
| 12 |
|
# our custom benchmark class |
| 13 |
|
class MyBenchmark(Benchmark): |
| 14 |
|
def __init__(self): |
| 15 |
|
Benchmark.__init__(self, -10, 10) |
| 16 |
|
|
| 17 |
|
def function(self): |
| 18 |
|
def evaluate(D, sol): |
| 19 |
|
val = 0.0 |
| 20 |
|
for i in range(D): val += sol[i] ** 2 |
| 21 |
|
return val |
| 22 |
|
return evaluate |
| 23 |
|
|
| 24 |
|
|
| 25 |
|
# custom initialization population function |
examples/advanced_example.py 1 location
|
@@ 12-21 (lines=10) @@
|
| 9 |
|
from NiaPy.algorithms.basic import GreyWolfOptimizer |
| 10 |
|
|
| 11 |
|
# our custom benchmark class |
| 12 |
|
class MyBenchmark(Benchmark): |
| 13 |
|
def __init__(self): |
| 14 |
|
Benchmark.__init__(self, -10, 10) |
| 15 |
|
|
| 16 |
|
def function(self): |
| 17 |
|
def evaluate(D, sol): |
| 18 |
|
val = 0.0 |
| 19 |
|
for i in range(D): val += sol[i] ** 2 |
| 20 |
|
return val |
| 21 |
|
return evaluate |
| 22 |
|
|
| 23 |
|
|
| 24 |
|
# we will run 10 repetitions of Grey Wolf Optimizer against our custom MyBenchmark benchmark function |
examples/custom_benchmark.py 1 location
|
@@ 11-20 (lines=10) @@
|
| 8 |
|
from NiaPy.benchmarks import Benchmark |
| 9 |
|
from NiaPy.algorithms.basic import ParticleSwarmAlgorithm |
| 10 |
|
|
| 11 |
|
class MyBenchmark(Benchmark): |
| 12 |
|
def __init__(self): |
| 13 |
|
Benchmark.__init__(self, -10, 10) |
| 14 |
|
|
| 15 |
|
def function(self): |
| 16 |
|
def evaluate(D, sol): |
| 17 |
|
val = 0.0 |
| 18 |
|
for i in range(D): val += sol[i] ** 2 |
| 19 |
|
return val |
| 20 |
|
return evaluate |
| 21 |
|
|
| 22 |
|
|
| 23 |
|
# we will run Particle Swarm Algorithm with on custom benchmark |