PKU-DAIR / open-box

Generalized and Efficient Blackbox Optimization System
https://open-box.readthedocs.io
Other
376 stars 52 forks source link

AttributeError: 'SwigPyObject' object has no attribute 'mean' #87

Closed lichengyuan98 closed 6 months ago

lichengyuan98 commented 7 months ago

space = sp.Space() x1 = sp.Real("x1", -5, 10, default_value=0) x2 = sp.Real("x2", 0, 15, default_value=0) space.add_variables([x1, x2])

Define Objective Function

def branin(config): x1, x2 = config['x1'], config['x2'] y = (x2-5.1/(4*np.pi2)*x1*2+5/np.pix1-6)2+10(1-1/(8np.pi))*np.cos(x1)+10 return {'objectives': [y]}

Run

opt = Optimizer( branin, space, max_runs=50, surrogate_type='gp', task_id='quick_start',) history = opt.run()

show the final results

print(history.get_importance())

- **Expected output**

The parameter importance table

- **Real output**
``` shell
Traceback (most recent call last):
  File "xxx\main.py", line 58, in <module>
    print(history.get_importance())
  File "D:\Programs\Miniconda\envs\my_env\lib\site-packages\openbox\utils\history.py", line 870, in get_importance
    feature_importance = importance_func(X, Y[:, i])
  File "D:\Programs\Miniconda\envs\my_env\lib\site-packages\openbox\utils\feature_importance\get_importance.py", line 47, in get_fanova_importance
    res = f.quantify_importance(p_list)
  File "D:\Programs\Miniconda\envs\my_env\lib\site-packages\openbox\utils\feature_importance\fanova\fanova.py", line 328, in quantify_importance
    self.__compute_marginals(dimensions)
  File "D:\Programs\Miniconda\envs\my_env\lib\site-packages\openbox\utils\feature_importance\fanova\fanova.py", line 297, in __compute_marginals
    if not np.isnan(ls.mean()):
AttributeError: 'SwigPyObject' object has no attribute 'mean'
swig/python detected a memory leak of type 'rfr::util::weighted_running_statistics< double > *', no destructor found.

Process finished with exit code 1

How to solve this issue? Thanks!

jhj0411jhj commented 7 months ago

Hi @lichengyuan98, please provide the (1) python version, (2) pyrfr version, (3) OS platform you are using. Thanks.

lichengyuan98 commented 7 months ago

(1) python=3.10.13 (2) pyrfr=0.9.0 (3) Windows 11 professional

Thanks!

jhj0411jhj commented 7 months ago

I'm not sure which part is broken. We test on py=3.10.13/pyrfr=0.9.0 and both windows 10/11, and everything is fine. Would you please try the following steps?

  1. Set surrogate_type='prf' instead of 'gp' or 'auto'. This will enable the model (prf) used in parameter importance analysis for optimization. See if the optimization can run normally.
  2. Check the installed SWIG version via swig -version. I suggest using SWIG 3.0.12. Please try re-installing SWIG 3.0 (https://open-box.readthedocs.io/en/latest/installation/install_swig.html) and then re-installing pyrfr.
  3. Use SHAP for importance analysis instead of fANOVA: history.get_importance(method='shap')
lichengyuan98 commented 6 months ago

After trying history.get_importance(method='shap'), everything is fine.

Thanks a lot!