Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash on custom objective function #108

Closed
ibell opened this issue May 2, 2018 · 5 comments
Closed

Crash on custom objective function #108

ibell opened this issue May 2, 2018 · 5 comments
Assignees
Labels
bug Bugs, bug fixes, etc.

Comments

@ibell
Copy link

ibell commented May 2, 2018

  • PySwarms version: head
  • Python version: 3.6
  • Operating System: linux

Description

Tried to optimize a harder function than the sphere, which is basically trivial. The sphere function, with all else held constant, works fine.

What I Did

import pyswarms as ps
from numpy import cos, sqrt

def Griewangk(x):
    sum1 = 0 
    prod1 = 1
    for i in range(len(x)):
        sum1 += pow(x[i], 2)
        prod1 *= cos(x[i]/sqrt(i+1))
    f = sum1/4000.0 - prod1 + 1
    return f

# Set-up hyperparameters
options = {'c1': 0.5, 'c2': 0.3, 'w':0.9}

# Call instance of GlobalBestPSO
optimizer = ps.single.GlobalBestPSO(n_particles=100, dimensions=10,
                                    options=options)

# Perform optimization
stats = optimizer.optimize(Griewangk, iters=1000)
print(stats)

yields

ValueError                                Traceback (most recent call last)
<ipython-input-40-515ac38c5127> in <module>()
     19 
     20 # Perform optimization
---> 21 stats = optimizer.optimize(Griewangk, iters=1000)
     22 print(stats)

~/miniconda3/envs/jupy36/lib/python3.6/site-packages/pyswarms/single/global_best.py in optimize(self, objective_func, iters, print_step, verbose)
    153             _m = np.repeat(m[:, np.newaxis], self.dimensions, axis=1)
    154             self.personal_best_pos = np.where(~_m, self.personal_best_pos,
--> 155                                               self.pos)
    156 
    157             # Get theValueError                                Traceback (most recent call last)
<ipython-input-40-515ac38c5127> in <module>()
     19 
     20 # Perform optimization
---> 21 stats = optimizer.optimize(Griewangk, iters=1000)
     22 print(stats)

~/miniconda3/envs/jupy36/lib/python3.6/site-packages/pyswarms/single/global_best.py in optimize(self, objective_func, iters, print_step, verbose)
    153             _m = np.repeat(m[:, np.newaxis], self.dimensions, axis=1)
    154             self.personal_best_pos = np.where(~_m, self.personal_best_pos,
--> 155                                               self.pos)
    156 
    157             # Get the minima of the pbest and check if it's less than

ValueError: operands could not be broadcast together with shapes (10,10) (100,10) (100,10)  minima of the pbest and check if it's less than

ValueError: operands could not be broadcast together with shapes (10,10) (100,10) (100,10) 
@ljvmiranda921
Copy link
Owner

Hi @ibell , let me get to this during the weekend, currently traveling so I can't access my work laptop

@ljvmiranda921 ljvmiranda921 self-assigned this May 3, 2018
@ljvmiranda921 ljvmiranda921 added the bug Bugs, bug fixes, etc. label May 3, 2018
@ibell
Copy link
Author

ibell commented May 3, 2018

Actually, the bug was/is in my understanding. I didn't realize that the inputs to the objective functions should be vector inputs (all inputs are evaluated in parallel). Maybe that just needs to be clarified in the docs. I think in the docs it makes more sense to use a hard-coded function so you can see what is going on rather than using a baked in function.

@ljvmiranda921
Copy link
Owner

Hi @ibell , understood. Yes you are correct, the documentation still leaves a lot for improvement and it would be nice to do these upgrades. Just in case, would you also like to include this 'clarification' in your Pull Request?

@ljvmiranda921
Copy link
Owner

Reference #107

@rfpg1
Copy link

rfpg1 commented Nov 29, 2023

I know this is quite old
But i bumped into the same problem and I can't figure out how to write my own function and pass it as objective function
Could you give me some instructions?
I'm trying to make a simple example:

import pyswarms as ps
from pyswarms.utils.functions import single_obj as fx

def f(x):
    return x**2

options = {'c1':0.5, 'c2': 0.3, 'w':0.9}

optimizer = ps.single.GlobalBestPSO(n_particles=100, dimensions=2, options=options)

best_cost, best_pos = optimizer.optimize(f, iters=100)

print(best_cost)
print(best_pos)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bugs, bug fixes, etc.
Projects
None yet
Development

No branches or pull requests

3 participants