def route_local_search(iterations): t0 = time.clock() minvalue = float('+Inf') min_path_list = None min_students_dict = None print('Local search: {0} iterations'.format(iterations)) for i1 in range(iterations): global_path_list, global_students_dict = router.route_local_search() if global_path_list == None or global_students_dict == None: i1 = i1 - 1 dist = router.get_distance() if dist < minvalue: print('dist:', dist) minvalue = dist min_path_list = global_path_list min_students_dict = global_students_dict print('{0:.5f}s'.format(time.clock() - t0)) return [min_path_list, min_students_dict]
def route_local_search(iterations, foutput): t0 = process_time() minvalue = float('+Inf') min_path_list = None min_students_dict = None foutput.write( "\n--------------------INSIDE ROUTE_LOCAL_SEARCH-----------------------" ) a = '\nLocal search: ' + str(iterations) + ' iterations' foutput.write(a) for i1 in range(iterations): b = "\n\n`````ITERATION no: " + str(i1) + "`````" global_path_list, global_students_dict = router.route_local_search() c = "\nintial global_path_list: " + str(global_path_list) d = "\ninitial global_students_dict: " + str(global_students_dict) foutput.write(b) foutput.write(c) foutput.write(d) #if either is 0 go back to the previous iteration if global_path_list == None or global_students_dict == None: i1 = i1 - 1 #get the total length of the route dist = router.get_distance() #now check if this length if lesser than the min length -> make that the min length if dist < minvalue: w = '\ndist:' + str(dist) minvalue = dist x = "\nminvalue: " + str(minvalue) min_path_list = global_path_list y = "\nmin_path_list: " + str(min_path_list) min_students_dict = global_students_dict z = "\nmin_students_dict: " + str(min_students_dict) foutput.write(w) foutput.write(x) foutput.write(y) foutput.write(z) print('{0:.5f}s'.format(process_time() - t0)) return [min_path_list, min_students_dict]
#!/usr/bin/env python3 import router import time if __name__ == '__main__': fn = 'instances/sbr3.txt' print('Router init', end=' ') t0 = time.perf_counter() router = router.Router() stops = router.get_stops() students = router.get_students() maxwalk = router.get_maxwalk() capacity = router.get_capacity() print('{0:.5f}s'.format(time.perf_counter() - t0)) print() nf = 0 print('loop') it = 1000 for i in range(it): print('{0}/{1} ({2}%)'.format(i, it, 100 * (i / it))) t0 = time.perf_counter() global_path_list, global_students_dict = router.route_local_search() if global_path_list is None and global_students_dict is None: nf += 1 print('not feasible solutions: {0}/{1} ({2}%)'.format( nf, it, 100 * (nf / it)))