示例#1
0
def scenario_A(file_in, file_out=None):
    print("Input file: {}".format(file_in))

    reader = FileReader(file_in)
    problem = reader.read()

    solver = Pizza(problem)
    print("Description:")
    print(solver.description())

    slices = solver.solve()

    is_valid, result = problem.validate_solution(slices)

    if is_valid:
        print("Solution for problem {} is correct. Score is {}".format(
            file_in, result))
        solution = Solution(problem)
        solution.load_slices(slices)
        if DEBUG:
            solution.print_solution()
        if file_out:
            writer = FileWriter(file_in + ".out")
            writer.write(solution)
    else:
        print("Incorrect solution. Please check the error messages below")
        for msg in result:
            print(msg)
示例#2
0
    def run_instance(self, id, parameters):
        init_solver = self.slave_solver(parameters)
        solution = init_solver.run(self.problem)
        score = solution.score()
        print("Initial solution score: {}, parameters: {}".
              format(score, ", ".join(["{}: {}".format(k, v) for k, v in parameters.items()])))

        if self.file_output:
            writer = FileWriter("{}.{}.{}.out".format(self.file_output, id, score))
            writer.write(solution)

        return solution, score
示例#3
0
def scenario_A(file_in, file_out):
    global DEBUG
    reader = FileReader(file_in)
    problem = reader.read()

    init_solver = InitSolverSilly()
    solution = init_solver.run(problem)
    print("Initial solution score: {}".format(solution.score()))
    if DEBUG:
        solution.print_solution()

    writer = FileWriter(file_out)
    writer.write(solution)
示例#4
0
def scenario_C(file_in, file_out, file_par=None):
    global DEBUG
    reader = FileReader(file_in)
    problem = reader.read()

    init_solver = ParallelInitSolver(InitSolverSillyParameterized,
                                     file_output=file_out)
    solution = init_solver.run(problem, file_par)
    print("Initial solution score: {}".format(solution.score()))
    if DEBUG:
        solution.print_solution()

    writer = FileWriter(file_out)
    writer.write(solution)
示例#5
0
 def __enter__(self):
     self.db.connect()
     self.cursor = self.db.connection.cursor()
     if OUTPUT_TYPE == 'FILE':
         self.writer = FileWriter()
     elif OUTPUT_TYPE == 'DB':
         self.writer = DbWriter(self.connection)
     else:
         raise IOError()
     return self
示例#6
0
class SpiderWorker(object):
    def __init__(self, url, size=20):
        self.url = url
        self.pool = ProxiesPool()
        self.parser = HtmlParser(url)
        self.url_manager = URLSManager(url_pattern=url, size=size)
        self.writer = FileWriter()

    @Decorator.time
    def start(self):
        self.url_manager.add_url(self.url)
        while self.url_manager.has_next():
            hd = HtmlDownloader(proxies=self.pool.get_proxy_ip())
            url = self.url_manager.get_url()
            data = hd.download(url)
            urls = self.parser.simple_tags(data, 'a', attributes=['href'])
            self.url_manager.add_urls([url_.get('href') for url_ in urls])
            title = self.parser.element(data, 'title')
            title = title.getText() if title else 'unknown'
            self.writer.load_data('[%s] %s' % (title, url))
        self.writer.writer()
示例#7
0
def scenario_B(file_in, file_out):
    global DEBUG
    reader = FileReader(file_in)
    problem = reader.read()

    init_solver = InitSolverSilly()
    solution = init_solver.run(problem)
    print("Initial solution score: {}".format(solution.score()))
    if DEBUG:
        solution.print_solution()

    optimizer = Tabu(problem,
                     solution,
                     Neighbourhood_ChangeFormats,
                     debug=True)
    optimized_solution = optimizer.run(time_limit=100)

    print("optimized solution score: {}".format(optimized_solution.score()))
    if DEBUG:
        optimized_solution.print_solution()

    writer = FileWriter(file_out)
    writer.write(optimized_solution)
示例#8
0
def scenario_D(file_in, file_out, file_par=None):
    global DEBUG
    reader = FileReader(file_in)
    problem = reader.read()

    init_solver = ParallelInitSolver(InitSolverSillyParameterized,
                                     file_output=file_out)
    solution = init_solver.run(problem, file_par)
    print("Initial solution score: {}".format(solution.score()))
    if DEBUG:
        solution.print_solution()

    optimizer = ParallelTabu(problem,
                             solution,
                             Neighbourhood_ChangeFormats,
                             debug=True)
    optimized_solution = optimizer.run(time_limit=1000)

    print("optimized solution score: {}".format(optimized_solution.score()))
    if DEBUG:
        optimized_solution.print_solution()

    writer = FileWriter(file_out)
    writer.write(solution)
示例#9
0
 def __init__(self, url, size=20):
     self.url = url
     self.pool = ProxiesPool()
     self.parser = HtmlParser(url)
     self.url_manager = URLSManager(url_pattern=url, size=size)
     self.writer = FileWriter()