def evaluate_constraints(self, solution: FloatSolution) -> None:
constraints : [float] = [0.0 for x in range(self.number_of_constraints)]
x1 = solution.variables[0]
x2 = solution.variables[1]
constraints[0] = (x1 * x1 + x2 * x2 - 1.0 - 0.1 * cos(16.0 * atan(x1 / x2)))
constraints[1] = -2.0 * ((x1 - 0.5) * (x1 - 0.5) + (x2 - 0.5) * (x2 - 0.5) - 0.5)
overall_constraint_violation = 0.0
number_of_violated_constraints = 0.0
for constrain in constraints:
if constrain < 0.0:
overall_constraint_violation += constrain
number_of_violated_constraints += 1
solution.attributes["overall_constraint_violation"] = overall_constraint_violation
solution.attributes["number_of_violated_constraints"] = number_of_violated_constraints
评论列表
文章目录