def calculate_fitness(self): fitness = 0 for i in range(POPULATION_SIZE): gene = self.genes[i] # 判断树是否超出土地边界 if gene['x'] - TREE_RADIUS < 0 or gene['x'] + TREE_RADIUS > LAND_WIDTH or gene['y'] - TREE_RADIUS < 0 or gene['y'] + TREE_RADIUS > LAND_HEIGHT: continue # 判断树与其他树之间的距离是否足够 for j in range(i + 1, POPULATION_SIZE): other_gene = self.genes[j] distance = math.sqrt((gene['x'] - other_gene['x']) ** 2 + (gene['y'] - other_gene['y']) ** 2) if distance < MIN_DISTANCE: continue # 判断树的高度是否符合要求 if gene['height'] < MIN_HEIGHT or gene['height'] > MAX_HEIGHT: continue # 判断树的覆盖面积是否超出最大值 cover_area = math.pi * (0.596 * gene['height'] - 0.3) ** 2 if cover_area > MAX_COVER_AREA: continue # 计算适应度 fitness += 1 return fitness怎么缩进,缩进格式具体是什么
时间: 2023-06-14 17:05:45 浏览: 144
这段代码的缩进格式如下:
class TreePopulation:
def calculate_fitness(self):
fitness = 0
for i in range(POPULATION_SIZE):
gene = self.genes[i]
# 判断树是否超出土地边界
if gene['x'] - TREE_RADIUS < 0 or gene['x'] + TREE_RADIUS > LAND_WIDTH or gene['y'] - TREE_RADIUS < 0 or gene['y'] + TREE_RADIUS > LAND_HEIGHT:
continue
# 判断树与其他树之间的距离是否足够
for j in range(i + 1, POPULATION_SIZE):
other_gene = self.genes[j]
distance = math.sqrt((gene['x'] - other_gene['x']) ** 2 + (gene['y'] - other_gene['y']) ** 2)
if distance < MIN_DISTANCE:
continue
# 判断树的高度是否符合要求
if gene['height'] < MIN_HEIGHT or gene['height'] > MAX_HEIGHT:
continue
# 判断树的覆盖面积是否超出最大值
cover_area = math.pi * (0.596 * gene['height'] - 0.3) ** 2
if cover_area > MAX_COVER_AREA:
continue
# 计算适应度
fitness += 1
return fitness
每一层缩进使用四个空格,其中类(class)和函数(def)之间有两个空格的缩进。在函数内部的if语句、for循环等控制语句的后面有一个空格的缩进。
阅读全文