if ((i!=j) and (abs(pop.indv[i].x - pop.indv[j].x)<min_d) and (pop.indv[i].x - pop.indv[j].x != 0))
时间: 2023-06-11 16:06:30 浏览: 96
这是一个条件语句的布尔表达式,它包含了三个条件:
1. i 不等于 j
2. pop.indv[i].x 和 pop.indv[j].x 之差的绝对值小于 min_d
3. pop.indv[i].x 和 pop.indv[j].x 不相等
如果这三个条件都成立,那么整个布尔表达式的值为真(True),否则为假(False)。在编程中,这种类型的语句通常用于控制程序的流程,例如在某些条件下执行一些特定的操作,或跳过某些代码块等。
相关问题
理解这段逻辑BEGIN o_code := 1; o_note := '查询成功'; IF i_pd_cd IS NULL THEN o_code := -501; o_note := '错误:产品不可为空!'; RETURN; END IF; OPEN o_cursor FOR SELECT a.pd_name, a.pd_abbr, a.pd_cd, to_char(to_date(sale_strt_date, 'yyyy-mm-dd'), 'yyyy-mm-dd') scrp_strt_date, to_char(to_date(sale_end_date, 'yyyy-mm-dd'), 'yyyy-mm-dd') scrp_end_date, b.sale_scal, 0 lim_ivst_num, c.mino_lmt_agmt_vol, 0 ntrl_prsn_lim_num, nvl(b.indv_scrp_orgn, 0) indv_scrp_orgn, nvl(b.indv_scrp_base, 0) indv_scrp_base, nvl(b.ins_scrp_orgn, 0) ins_scrp_orgn, nvl(b.ins_scrp_base, 0) ins_scrp_base, nvl(b.indv_purs_orgn, 0) indv_purs_orgn, nvl(b.indv_purs_base, 0) indv_purs_base, nvl(b.ins_purs_orgn, 0) ins_purs_orgn, nvl(b.ins_purs_base, 0) ins_purs_base FROM product.tpd_trus a LEFT JOIN product.tpd_trd_para b ON a.pd_cd = b.pd_cd LEFT JOIN crm.tmkt_pd_lmt_assn_mag c ON a.pd_cd = c.pd_cd WHERE b.pd_cd = i_pd_cd;
这段代码是一个存储过程的逻辑。它的功能是根据给定的产品代码(i_pd_cd),查询相关的产品信息并将结果返回。
首先,它将输出参数 o_code 设置为1,表示查询成功,将 o_note 设置为"查询成功"。
然后,它检查输入参数 i_pd_cd 是否为空。如果为空,将 o_code 设置为-501,表示错误,将 o_note 设置为"错误:产品不可为空!",并终止执行。
如果 i_pd_cd 不为空,则使用一个游标(o_cursor)来执行查询操作。查询语句从 product.tpd_trus 表中选择相关列,并左连接 product.tpd_trd_para 表和 crm.tmkt_pd_lmt_assn_mag 表。其中,使用 pd_cd 进行表之间的关联。
最后,将查询的结果集赋值给 o_cursor 游标,并返回给调用者。
请注意,这只是代码的一部分,可能还有其他部分未包含在内。如果您需要更多的帮助或有其他问题,请随时提问。
nsga-ii 的Python 代码
以下是 NSGA-II 算法的 Python 代码实现:
```python
import random
import numpy as np
class NSGA2():
def __init__(self, problem, pop_size=100, max_gen=100, pc=0.9, pm=0.1):
self.problem = problem
self.pop_size = pop_size
self.max_gen = max_gen
self.pc = pc
self.pm = pm
self.min_bounds, self.max_bounds = problem.bounds()
def run(self):
pop = self._init_pop()
for i in range(self.max_gen):
pop = self._selection(pop)
pop = self._crossover(pop)
pop = self._mutation(pop)
pop = self._crowding_distance(pop)
pop = self._fast_nondominated_sort(pop)
pop = self._environmental_selection(pop)
print("Gen:", i, " Best Fitness: ", self.problem.evaluate(pop[0])[0])
return pop
def _init_pop(self):
pop = []
for i in range(self.pop_size):
indv = np.random.uniform(self.min_bounds, self.max_bounds, size=self.problem.n_var)
fitness = self.problem.evaluate(indv)
pop.append({"indv": indv, "fitness": fitness})
return pop
def _selection(self, pop):
parents = []
for i in range(self.pop_size):
a, b = np.random.randint(self.pop_size, size=2)
if pop[a]["fitness"][0] < pop[b]["fitness"][0]:
parents.append(pop[a])
else:
parents.append(pop[b])
return parents
def _crossover(self, pop):
offsprings = []
for i in range(0, self.pop_size, 2):
if i+1 < self.pop_size and random.random() < self.pc:
p1, p2 = pop[i], pop[i+1]
c1, c2 = self._sbx(p1["indv"], p2["indv"])
offsprings.append({"indv": c1, "fitness": self.problem.evaluate(c1)})
offsprings.append({"indv": c2, "fitness": self.problem.evaluate(c2)})
else:
offsprings.append(pop[i])
offsprings.append(pop[i+1])
return offsprings
def _mutation(self, pop):
mutants = []
for i in range(self.pop_size):
if random.random() < self.pm:
indv = self._polynomial_mutation(pop[i]["indv"])
mutants.append({"indv": indv, "fitness": self.problem.evaluate(indv)})
else:
mutants.append(pop[i])
return mutants
def _sbx(self, p1, p2):
c1, c2 = np.copy(p1), np.copy(p2)
for i in range(len(p1)):
if random.random() < 0.5:
if abs(p1[i] - p2[i]) > 1e-7:
if p1[i] < p2[i]:
c1[i], c2[i] = self._simulated_binary_crossover(p1[i], p2[i])
else:
c2[i], c1[i] = self._simulated_binary_crossover(p1[i], p2[i])
return c1, c2
def _simulated_binary_crossover(self, u, v, eta=20):
if random.random() < 0.5:
if u > v:
u, v = v, u
y1 = ((2*u + v) / 3) + ((v - u) * random.random() * eta)
y2 = ((u + 2*v) / 3) + ((u - v) * random.random() * eta)
else:
y1, y2 = u, v
return y1, y2
def _polynomial_mutation(self, indv, eta=20):
mutant = np.copy(indv)
for i in range(len(indv)):
if random.random() < 1.0/len(indv):
y = indv[i]
delta = min((y - self.min_bounds[i]), (self.max_bounds[i] - y))
y += delta * random.uniform(-1, 1)
mutant[i] = y
return mutant
def _crowding_distance(self, pop):
for i in range(self.pop_size):
pop[i]["distance"] = 0
for obj in range(self.problem.n_obj):
pop = sorted(pop, key=lambda k: k["fitness"][obj])
fmin, fmax = pop[0]["fitness"][obj], pop[-1]["fitness"][obj]
pop[0]["distance"] = pop[-1]["distance"] = np.inf
for i in range(1, self.pop_size-1):
pop[i]["distance"] += (pop[i+1]["fitness"][obj] - pop[i-1]["fitness"][obj]) / (fmax - fmin)
return pop
def _fast_nondominated_sort(self, pop):
fronts = [[]]
for indv in pop:
indv["dominated"] = []
indv["dominates"] = 0
for other in pop:
if np.all(indv["fitness"] <= other["fitness"]) and not np.all(indv["fitness"] == other["fitness"]):
indv["dominates"] += 1
elif np.all(indv["fitness"] >= other["fitness"]) and not np.all(indv["fitness"] == other["fitness"]):
indv["dominated"].append(other)
if indv["dominates"] == 0:
indv["rank"] = 0
fronts[0].append(indv)
i = 0
while len(fronts[i]) > 0:
next_front = []
for indv in fronts[i]:
for other in indv["dominated"]:
other["dominates"] -= 1
if other["dominates"] == 0:
other["rank"] = i+1
next_front.append(other)
i += 1
fronts.append(next_front)
return fronts[:-1]
def _environmental_selection(self, pop):
selected = []
rank = 0
while len(selected) + len(pop[rank]) <= self.pop_size:
for indv in pop[rank]:
selected.append(indv)
rank += 1
if len(selected) < self.pop_size:
dist = sorted(pop[rank], key=lambda k: k["distance"], reverse=True)
while len(selected) < self.pop_size:
selected.append(dist.pop(0))
return selected
```
其中,`problem` 是问题的类,包含了目标函数和约束条件的定义,`pop_size` 是种群大小,`max_gen` 是最大迭代次数,`pc` 是交叉概率,`pm` 是变异概率。该算法实现了 SBX 交叉、多项式变异、快速非支配排序、拥挤度计算和环境选择等操作。算法的运行函数是 `run()`。
阅读全文