Return best solution or the same if there's none
This commit is contained in:
		
							parent
							
								
									3e08b67371
								
							
						
					
					
						commit
						028db4ba91
					
				@ -15,11 +15,16 @@ def replace_worst_element(previous, data):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_random_solution(previous, data):
 | 
					def get_random_solution(previous, data):
 | 
				
			||||||
    solution = replace_worst_element(previous, data)
 | 
					    candidates = []
 | 
				
			||||||
    while solution["distance"].sum() <= previous["distance"].sum():
 | 
					    candidates.append(previous)
 | 
				
			||||||
        if solution.equals(previous):
 | 
					    solution, worst_index = replace_worst_element(previous, data)
 | 
				
			||||||
            break
 | 
					    previous_worst_distance = previous["distance"].loc[worst_index]
 | 
				
			||||||
        solution = replace_worst_element(previous=solution, data=data)
 | 
					    while candidates[-1].distance.loc[worst_index] <= previous_worst_distance:
 | 
				
			||||||
 | 
					        last_solution = candidates[-1]
 | 
				
			||||||
 | 
					        solution, _ = replace_worst_element(previous=solution, data=data)
 | 
				
			||||||
 | 
					        if solution.equals(last_solution):
 | 
				
			||||||
 | 
					            return last_solution
 | 
				
			||||||
 | 
					        candidates.append(solution)
 | 
				
			||||||
    return solution
 | 
					    return solution
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user