def energy(self, state=None):
state = self.state if state is None else state<EOL>route = state<EOL>e = <NUM_LIT:0><EOL>if self.distance_matrix:<EOL><INDENT>for i in range(len(route)):<EOL><INDENT>e += self.distance_matrix["<STR_LIT>".format(route[i-<NUM_LIT:1>], route[i])]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for i in range(len(route)):<EOL><INDENT>e += distance(self.cities[route[i-<NUM_LIT:1>]], self.cities[route[i]])<EOL><DEDENT><DEDENT>return e<EOL>
Calculates the length of the route.