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

请问整数规划该怎么写? #15

Closed
bjmwang opened this issue Oct 24, 2019 · 7 comments
Closed

请问整数规划该怎么写? #15

bjmwang opened this issue Oct 24, 2019 · 7 comments
Labels

Comments

@bjmwang
Copy link

bjmwang commented Oct 24, 2019

demo_func=lambda x: x[0]**2 + x[1]**2 + x[2]**2
ga = GA(func=demo_func,n_dim=3, max_iter=500, lb=[-1, -10, -5], ub=[2, 10, 2])
best_x, best_y = ga.fit()

如果 x[0] 只能取整数, 代码应该怎么写?
谢谢

@guofei9987
Copy link
Owner

算法库没有特地做整数规划,但可以通过调整 precision 参数来实现0/1规划,代码如下

from sko.GA import GA

demo_func = lambda x: x[0] ** 2 + (x[1] - 0.05) ** 2 + x[2] ** 2
ga = GA(func=demo_func, n_dim=3, max_iter=500, lb=[0, 0, 0], ub=[1, 1, 1], precision=1)
best_x, best_y = ga.run()
print('best_x:', best_x, '\n', 'best_y:', best_y)

@keybat

@bjmwang
Copy link
Author

bjmwang commented Oct 25, 2019

收到, 多谢!

btw, 在我安装的版本里,

  • ga.run()报错, 改成ga.fit()可以运行
  • ga = GA(func=demo_func, n_dim=3, max_iter=500, lb=[0, 0, 0], ub=[1, 1, 1], precision=1)中, 参数precision可以设为列表, 为各个分量设置精度

@bjmwang bjmwang closed this as completed Oct 25, 2019
@guofei9987
Copy link
Owner

好的,如果还有需求,尽管在issue中提。以后会整理并解决需求。

@guofei9987
Copy link
Owner

@keybat 抱歉,我没有看清楚你的第二条需求,直到今天查了邮件。
这是个好建议,顺利的话,今晚能完成这个feature

@guofei9987 guofei9987 reopened this Oct 31, 2019
guofei9987 added a commit that referenced this issue Oct 31, 2019
@guofei9987
Copy link
Owner

Enjoy!

from sko.GA import GA

demo_func = lambda x: x[0] ** 2 + (x[1] - 0.05) ** 2 + x[2] ** 2
ga = GA(func=demo_func, n_dim=3, max_iter=500, lb=[0, 0, 0], ub=[1, 1, 1], precision=[1, 1e-7, 1e-7])
best_x, best_y = ga.run()
print('best_x:', best_x, '\n', 'best_y:', best_y)

@bjmwang
Copy link
Author

bjmwang commented Nov 4, 2019

完美, 多谢!

@guofei9987
Copy link
Owner

0.5.1 版本新增了整数规划(而不是0-1规划),见于另一个issue #22

from sko.GA import GA

demo_func = lambda x: (x[0] - 1) ** 2 + (x[1] - 0.05) ** 2 + x[2] ** 2
ga = GA(func=demo_func, n_dim=3, max_iter=500, lb=[-1, -1, -1], ub=[5, 1, 1], precision=[2, 1, 1e-7])
best_x, best_y = ga.run()
print('best_x:', best_x, '\n', 'best_y:', best_y)

@guofei9987 guofei9987 added the resolved Resolved label Dec 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants