NiaOrg / NiaPy

Python microframework for building nature-inspired algorithms. Official docs: https://niapy.org
https://niapy.org
MIT License
261 stars 80 forks source link

__init__() missing 3 required positional arguments: 'D', 'nFES', and 'benchmark' #244

Closed fakhrulfa closed 4 years ago

fakhrulfa commented 4 years ago

Hi,

I tried your example of Flower Pollination Algorithm code, but i got an error like this :

     11 for i in range(5):
     12     task = StoppingTask(D=10, nFES=10000, benchmark=Sphere())
---> 13     algo = FlowerPollinationAlgorithm(NP=20, p=0.5)
     14     best = algo.run(task=algo)
     15     print(best)

TypeError: __init__() missing 3 required positional arguments: 'D', 'nFES', and 'benchmark'

Here's the code :

# encoding=utf8
# This is temporary fix to import module from parent folder
# It will be removed when package is published on PyPI
import sys
sys.path.append('../')
# End of fix

import random
from NiaPy.algorithms.basic import FlowerPollinationAlgorithm
from NiaPy.task import StoppingTask
from NiaPy.benchmarks import Sphere

#we will run Flower Pollination Algorithm for 5 independent runs
for i in range(5):
    task = StoppingTask(D=10, nFES=10000, benchmark=Sphere())
    algo = FlowerPollinationAlgorithm(NP=20, p=0.5)
    best = algo.run(task=algo)
    print(best)

Hope you can help, thanks!

GregaVrbancic commented 4 years ago

Hi @fakhrulfa,

which version of NiaPy are you using? If not already, please install the latest release candidate version pip install NiaPy==2.0.0rc10.

If you are still facing some issues with the latest RC version installed, please let us know.

fakhrulfa commented 4 years ago

Thanks, its work!

but, when i tried to run this code. i got an output like this :

(None, None)
(None, None)
(None, None)
(None, None)
(None, None)

How to add the random number(import random) to the code, wheres the parameter that can contain the random number ?

GregaVrbancic commented 4 years ago

Could you provide us with a code example, which you are trying to run?

fakhrulfa commented 4 years ago

Heres the code, same as code that i used before.

import random
from NiaPy.algorithms.basic import FlowerPollinationAlgorithm
from NiaPy.task import StoppingTask
from NiaPy.benchmarks import Sphere

#we will run Flower Pollination Algorithm for 5 independent runs
for i in range(5):
    task = StoppingTask(D=10, nFES=10000, benchmark=Sphere())
    algo = FlowerPollinationAlgorithm(NP=20, p=0.5)
    best = algo.run(task=algo)
    print(best)
GregaVrbancic commented 4 years ago

You have a problem in line best = algo.run(task=algo), where it should be best = algo.run(task=task), like in the example https://github.com/NiaOrg/NiaPy/blob/master/examples/run_fpa.py .

fakhrulfa commented 4 years ago

ow, okay, thanks. its works!

But, how can i find the best solution in case when i try to find best value for parameter in sklearn method. In this case, i try to find best value of C and Epsilon, in parameter of SVM in Sklearn.

Heres the code:

regressor = SVR(kernel='linear', C=value, gamma=0.1, epsilon=value)
regressor.fit(X_train, y_train)

Hope u can help, thanks again!

GregaVrbancic commented 4 years ago

No problem. Regarding the parameter optimization of SVM, in general, you should implement your custom benchmark class which would map and evaluate the solution obtained from the optimization algorithm. For more information, you can check out the following blog post, where NiaPy is utilized for finding the most suitable parameters for KNN. Please, be aware that in the mentioned blog post the NiaPy version 1 is used, which is not compatible with version 2 (which you are using), so you can not copy-paste the code from the blog post to your project, but the general idea is the same.

fakhrulfa commented 4 years ago

Okay, i will try to follow the idea of the code on your blog. Thanks!