Return the best solution when you can't explore
This commit is contained in:
		
							parent
							
								
									2eb84a6883
								
							
						
					
					
						commit
						8b5029645f
					
				@ -17,26 +17,35 @@ def replace_worst_element(previous, data):
 | 
				
			|||||||
    return solution, worst_index
 | 
					    return solution, worst_index
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def choose_best_solution(previous, current, index):
 | 
				
			||||||
 | 
					    if previous.loc[index].distance >= current.loc[index].distance:
 | 
				
			||||||
 | 
					        return previous
 | 
				
			||||||
 | 
					    return current
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_random_solution(previous, data):
 | 
					def get_random_solution(previous, data):
 | 
				
			||||||
    candidates = []
 | 
					    candidates = []
 | 
				
			||||||
    candidates.append(previous)
 | 
					    candidates.append(previous)
 | 
				
			||||||
    solution, worst_index = replace_worst_element(previous, data)
 | 
					    solution, worst_index = replace_worst_element(previous, data)
 | 
				
			||||||
    previous_worst_distance = previous["distance"].loc[worst_index]
 | 
					    previous_worst_distance = previous["distance"].loc[worst_index]
 | 
				
			||||||
    while candidates[-1].distance.loc[worst_index] <= previous_worst_distance:
 | 
					    last_solution = candidates[-1]
 | 
				
			||||||
        last_solution = candidates[-1]
 | 
					    while last_solution.distance.loc[worst_index] <= previous_worst_distance:
 | 
				
			||||||
        solution, _ = replace_worst_element(previous=solution, data=data)
 | 
					        solution, _ = replace_worst_element(previous=solution, data=data)
 | 
				
			||||||
        if solution.equals(last_solution):
 | 
					        if solution.equals(last_solution):
 | 
				
			||||||
            return last_solution
 | 
					            best_solution = choose_best_solution(
 | 
				
			||||||
 | 
					                previous=previous, current=solution, index=worst_index
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            return best_solution
 | 
				
			||||||
        candidates.append(solution)
 | 
					        candidates.append(solution)
 | 
				
			||||||
    return solution
 | 
					        last_solution = candidates[-1]
 | 
				
			||||||
 | 
					    return last_solution
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def explore_neighbourhood(element, data, max_iterations=100000):
 | 
					def explore_neighbourhood(element, data, max_iterations=100000):
 | 
				
			||||||
    neighbourhood = []
 | 
					    neighbourhood = []
 | 
				
			||||||
    neighbourhood.append(element)
 | 
					    neighbourhood.append(element)
 | 
				
			||||||
    for _ in range(max_iterations):
 | 
					    for i in range(max_iterations):
 | 
				
			||||||
 | 
					        print(f"Iteration {i}")
 | 
				
			||||||
        previous_solution = neighbourhood[-1]
 | 
					        previous_solution = neighbourhood[-1]
 | 
				
			||||||
        neighbour = get_random_solution(previous=previous_solution, data=data)
 | 
					        neighbour = get_random_solution(previous=previous_solution, data=data)
 | 
				
			||||||
        if neighbour.equals(previous_solution):
 | 
					        if neighbour.equals(previous_solution):
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user