guofei9987 / scikit-opt

Genetic Algorithm, Particle Swarm Optimization, Simulated Annealing, Ant Colony Optimization Algorithm,Immune Algorithm, Artificial Fish Swarm Algorithm, Differential Evolution and TSP(Traveling salesman)
https://scikit-opt.github.io/scikit-opt/#/en/
MIT License
5.31k stars 992 forks source link

利用循环的方式写约束没有约束成功 #193

Closed lvjiamei closed 2 years ago

lvjiamei commented 2 years ago

约束:33的矩阵,每一行的和小于等于1 方式1: constraint_ueq = [\ lambda var: sum(np.array(var)[user_num: 2 user_num]) - 1, lambda var: sum(np.array(var)[user_num + 1 user_num: user_num + 2 user_num]) - 1, lambda var: sum(np.array(var)[user_num + 2 user_num: user_num + 3 user_num]) - 1 ] 输出:[0. 1. 0. 0. 1. 0. 0. 1. 0.]

方式2: for j in range(0, 3): constraint_ueq.append(lambda var: sum(np.array(var)[(1 + j) user_num: (j + 2) user_num]) - 1) 输出:[0. 1. 1. 1. 1. 1. 0. 1. 0.] 请问这是为什么呢?

linkedlist771 commented 2 years ago

因为没有实现,这个问题我好久前就问过了,你可以看看源代码,。

lvjiamei commented 2 years ago

谢谢谢谢回答!我搞了一个其他的方法搞定。 原因解释链接: https://www.codenong.com/3431676/ 下面是现在成功的代码:

constraint_ueq = [] def gene_constraint1(j): constraint_ueq.append(lambda var : sum(np.array(var)[(1 + j) user_num: (j + 2) user_num]) - 1) for j in range(0,3): gene_constraint1(j)