Skip to content

Commit

Permalink
Update apply local search to solution
Browse files Browse the repository at this point in the history
Improve a way to find a maximal ratio clique with local search as from a solution.
  • Loading branch information
JoseMiguel92 committed Mar 6, 2020
1 parent 04d5f7f commit d22d2c7
Show file tree
Hide file tree
Showing 53 changed files with 262,791 additions and 31 deletions.
39 changes: 36 additions & 3 deletions grasp_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
# Sources
GRAPH_PATH_SETS = 'sets/'
CSV_OUTPUT_FILE = "solution_table_{0}_{1}.csv"
CSV_OUTPUT_FILE_LS = "solution_table_{0}_{1}_LS.csv"
ALL_FILES_TXT_EXT = "**/*.txt"
CSV_EXT = "csv"
CSV_OUTPUT_DIR = "output"

# Messages
MAIN_TOTAL_TIME = "Total time: {0} seconds."
MAIN_LOG_PROCESS = "Processing: {0} - Solution type: {1} - Iteration: {2} - Alpha: {3} - Time: {4:1.5f} - Clique: {5}"
MAIN_LOG_PROCESS_OLD = "Old Solution: {0} - Ratio: {1:.5f} - Cardinality: {2} - Time: {3:.5f}"
MAIN_LOG_PROCESS_LS = "New solution: {0} - Ratio: {1:.5f} - Cardinality: {2} - Time: {3:.5f}"
MAIN_SEP_NAMES = "_"

# Config
Expand All @@ -44,10 +47,11 @@ def split_name(total_path):
random_alpha = random.random()
alpha_list = [0.25, 0.5, 0.75, random_alpha]
graph = Instance()
solution_types = [SolutionGrasp.RATIO, SolutionGrasp.ADJACENT]
solution_types = [SolutionGrasp.RATIO]
for solution_type in solution_types:
for file in glob.glob(GRAPH_PATH_SETS + ALL_FILES_TXT_EXT, recursive=True):
data = dict()
data_ls = dict()
filename = os.path.splitext(os.path.basename(file))[0]
graph.read_file(file)
instance_solution = SolutionGrasp()
Expand All @@ -57,14 +61,43 @@ def split_name(total_path):
solution = instance_solution.find_grasp_solution(graph, file, solution_type, fixed_seed, alpha)
find_grasp_sol_time = time.time() - start_time
ratio, density, cardinality = GraphUtils.calculate_solution(graph, solution)

local_search_sol = instance_solution.apply_ls(graph, solution)
find_ls_sol_time = time.time() - start_time
ls_ratio, ls_density, ls_cardinality = GraphUtils.calculate_solution(graph, local_search_sol)

table_key_name = filename + MAIN_SEP_NAMES + solution_type + MAIN_SEP_NAMES + str(
iteration) + MAIN_SEP_NAMES + str(alpha)
data.update({table_key_name: [graph.get_total_nodes(), density, ratio, cardinality,
find_grasp_sol_time, alpha]})
LOGGER.debug(MAIN_LOG_PROCESS.format(file, solution_type, iteration, alpha, find_grasp_sol_time,
solution))

data_ls.update({table_key_name: [graph.get_total_nodes(), ls_density, ls_ratio, ls_cardinality,
find_ls_sol_time, alpha]})

if GraphUtils.is_clique_solution(graph, solution):
LOGGER.debug(MAIN_LOG_PROCESS.format(file, solution_type, iteration, alpha, find_grasp_sol_time,
solution))
else:
LOGGER.fatal(MAIN_LOG_PROCESS.format(file, solution_type, iteration, alpha, find_grasp_sol_time,
solution))
sys.exit()

if GraphUtils.is_clique_solution(graph, local_search_sol):
LOGGER.debug(MAIN_LOG_PROCESS_OLD.format(solution, ratio, cardinality, find_grasp_sol_time))
LOGGER.debug(MAIN_LOG_PROCESS_LS.format(local_search_sol, ls_ratio, ls_cardinality,
find_ls_sol_time))
else:
LOGGER.fatal(
MAIN_LOG_PROCESS.format(file, solution_type, iteration, alpha, find_grasp_sol_time,
local_search_sol))
sys.exit()

output_name = split_name(file)

export_filename = CSV_OUTPUT_FILE.format(solution_type, output_name)
GraphUtils.export_solution(CSV_OUTPUT_DIR, data, export_filename)

export_filename_ls = CSV_OUTPUT_FILE_LS.format(solution_type, output_name)
GraphUtils.export_solution(CSV_OUTPUT_DIR, data_ls, export_filename_ls)

sys.exit()
401 changes: 401 additions & 0 deletions output/solution_table_adjacent_set-a_random-1.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_adjacent_set-a_random-1_LS.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_adjacent_set-b_random-9.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_adjacent_set-b_random-9_LS.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_adjacent_set-c_market-3.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_adjacent_set-c_market-3_LS.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_adjacent_set-d_wind-2005.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_adjacent_set-d_wind-2005_LS.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_adjacent_set-e_johnson8-2-4.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_adjacent_set-e_johnson8-2-4_LS.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_adjacent_set-e_karate.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_adjacent_set-e_karate_LS.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_adjacent_set-f_email.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_adjacent_set-f_email_LS.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_adjacent_set-f_p_hat1000-1.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_adjacent_set-f_p_hat1000-1_LS.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_ratio_set-a_random-1.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_ratio_set-a_random-1_LS.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_ratio_set-b_random-9.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_ratio_set-b_random-9_LS.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_ratio_set-c_market-3.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_ratio_set-c_market-3_LS.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_ratio_set-d_wind-2005.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_ratio_set-d_wind-2005_LS.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_ratio_set-e_johnson8-2-4.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_ratio_set-e_johnson8-2-4_LS.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_ratio_set-e_karate.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_ratio_set-e_karate_LS.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_ratio_set-f_email.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_ratio_set-f_email_LS.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_ratio_set-f_p_hat1000-1.csv

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions output/solution_table_ratio_set-f_p_hat1000-1_LS.csv

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Mako==1.1.1
Markdown==3.2.1
MarkupSafe==1.1.1
numpy==1.18.0
pandas==0.25.1
pdoc3==0.7.4
python-dateutil==2.8.0
pytz==2019.3
six==1.13.0
101 changes: 101 additions & 0 deletions sets/set-a/random-1.txt

Large diffs are not rendered by default.

401 changes: 401 additions & 0 deletions sets/set-b/random-9.txt

Large diffs are not rendered by default.

501 changes: 501 additions & 0 deletions sets/set-c/market-3.txt

Large diffs are not rendered by default.

501 changes: 501 additions & 0 deletions sets/set-d/wind-2005.txt

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions sets/set-e/DIMACS10/karate.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
34 78 0
16 2 3 4 5 6 7 8 9 11 12 13 14 18 20 22 32
9 1 3 4 8 14 18 20 22 31
10 1 2 4 8 9 10 14 28 29 33
6 1 2 3 8 13 14
3 1 7 11
4 1 7 11 17
4 1 5 6 17
4 1 2 3 4
5 1 3 31 33 34
2 3 34
3 1 5 6
1 1
2 1 4
5 1 2 3 4 34
2 33 34
2 33 34
2 6 7
2 1 2
2 33 34
3 1 2 34
2 33 34
2 1 2
2 33 34
5 26 28 30 33 34
3 26 28 32
3 24 25 32
2 30 34
4 3 24 25 34
3 3 32 34
4 24 27 33 34
4 2 9 33 34
6 1 25 26 29 33 34
12 3 9 15 16 19 21 23 24 30 31 32 34
17 9 10 14 15 16 19 20 21 23 24 27 28 29 30 31 32 33
File renamed without changes.
Loading

0 comments on commit d22d2c7

Please sign in to comment.