Skip to content

Commit

Permalink
Restructure logger and add timeout signal
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseMiguel92 committed Apr 7, 2020
1 parent afc8e95 commit 96d0a34
Show file tree
Hide file tree
Showing 45 changed files with 568 additions and 138,020 deletions.
8 changes: 3 additions & 5 deletions graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@

from logger.logger import Logger

# Logs
Logger.init_log()
LOGGER = logging.getLogger(__name__)

LOG_ERROR_WRITE_DOC = 'Error when writing a file.'


Expand All @@ -21,6 +17,8 @@ class GraphUtils:
TYPE_DIMACS10 = 'DIMACS10'
SET_SET_E = 'set-e'
SET_SET_F = 'set-f'
# Logs
LOGGER = Logger.init_log()

@staticmethod
def are_adjacent(node_1, node_2):
Expand Down Expand Up @@ -91,7 +89,7 @@ def export_solution(output, data, name):
path = output + file_sep + name
GraphUtils.create_mrcp_csv_table(path, data)
except IOError:
LOGGER.error(LOG_ERROR_WRITE_DOC)
GraphUtils.LOGGER.error(LOG_ERROR_WRITE_DOC)

@staticmethod
def calculate_clique_ratio(graph, clique):
Expand Down
90 changes: 52 additions & 38 deletions grasp_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
import logging
import os
import random
import signal
import sys
import time

from graph_utils import GraphUtils
from instance import Instance
from logger.logger import Logger
from solution_grasp import SolutionGrasp

# Logs
Logger.init_log()
LOGGER = logging.getLogger(__name__)

# Sources
Expand All @@ -28,28 +27,39 @@

# 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 = "Processed: {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 = "_"
MAIN_LOG_TIMEOUT = "Timeout: {0} - {1}"

# Config
TOTAL_ITERATIONS = 100
TIMEOUT_VALUE = 1800


def split_name(total_path):
name_splitted = os.path.splitext(total_path)[0].split(os.sep)
return "{0}_{1}".format(name_splitted[1], name_splitted[-1])


class Timeout(Exception):
pass


def handler(sig, frame):
raise Timeout


if __name__ == '__main__':
signal.signal(signal.SIGALRM, handler)
fixed_seed = None
random_alpha = random.random()
alpha_list = [0.25, 0.5, 0.75, random_alpha]
graph = Instance()
solution_types = [SolutionGrasp.RATIO, SolutionGrasp.ADJACENT]
for solution_type in solution_types:
for file in glob.glob(GRAPH_PATH_SETS + ALL_FILES_TXT_EXT, recursive=True):
for file in glob.glob(GRAPH_PATH_SETS + ALL_FILES_TXT_EXT, recursive=True):
for solution_type in solution_types:
data = dict()
data_ls = dict()
filename = os.path.splitext(os.path.basename(file))[0]
Expand All @@ -58,39 +68,43 @@ def split_name(total_path):
for alpha in alpha_list:
for iteration in range(1, TOTAL_ITERATIONS + 1):
start_time = time.time()
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]})

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()
signal.alarm(TIMEOUT_VALUE)
try:
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]})

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()

except Timeout:
LOGGER.fatal(MAIN_LOG_TIMEOUT.format(iteration, file))

output_name = split_name(file)

Expand Down
2 changes: 2 additions & 0 deletions logger/logger.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging
import logging.handlers
import sys
import datetime


class Logger:
Expand Down
Loading

0 comments on commit 96d0a34

Please sign in to comment.