Craft duplicate check before insert
This commit is contained in:
		
							parent
							
								
									5b63b993df
								
							
						
					
					
						commit
						6b67e6db34
					
				@ -7,11 +7,18 @@ def get_first_random_solution(m, data):
 | 
				
			|||||||
    return data.loc[random_indexes]
 | 
					    return data.loc[random_indexes]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def element_in_dataframe(solution, element):
 | 
				
			||||||
 | 
					    duplicates = solution.query(
 | 
				
			||||||
 | 
					        f"(source == {element.source} and destination == {element.destination}) or (source == {element.destination} and destination == {element.source})"
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    return not duplicates.empty
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def replace_worst_element(previous, data):
 | 
					def replace_worst_element(previous, data):
 | 
				
			||||||
    solution = previous.copy()
 | 
					    solution = previous.copy()
 | 
				
			||||||
    worst_index = solution["distance"].astype(float).idxmin()
 | 
					    worst_index = solution["distance"].astype(float).idxmin()
 | 
				
			||||||
    random_element = data.sample().squeeze()
 | 
					    random_element = data.sample().squeeze()
 | 
				
			||||||
    while solution.isin(random_element.values.ravel()).any().any():
 | 
					    while element_in_dataframe(solution=solution, element=random_element):
 | 
				
			||||||
        random_element = data.sample().squeeze()
 | 
					        random_element = data.sample().squeeze()
 | 
				
			||||||
    solution.loc[worst_index] = random_element
 | 
					    solution.loc[worst_index] = random_element
 | 
				
			||||||
    return solution, worst_index
 | 
					    return solution, worst_index
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user