├── IMP ├── Test │ ├── bitSetTest.py │ ├── 01Test.py │ ├── reciprocalTest.py │ ├── DNLibTest.py │ ├── defaultDictTest.py │ ├── numpyAppendTest.py │ ├── npZeroTest.py │ ├── elapseTimeTest.py │ ├── avgTest.py │ ├── randomPerfTest.py │ ├── npUniformTest.py │ ├── numpyEfficiencyTest.py │ └── nTest.py ├── seeds.txt ├── testseeds.txt ├── my_seeds.txt ├── seeds2.txt ├── seeds3.txt ├── timer.py ├── data_handle.py ├── graph.py ├── network.txt ├── IMP.py └── ISE.py ├── Gobang ├── Testing │ ├── heuristic_test.py │ ├── index_test.py │ ├── tostr_test.py │ ├── map_test.py │ ├── dict_test.py │ ├── re_test.py │ ├── unzip_test.py │ ├── diagonal_test.py │ ├── where_test.py │ ├── lambda_test.py │ ├── substring_test.py │ ├── slice_test.py │ ├── performance_test.py │ ├── evaluate_test.py │ └── game_test.py ├── chess_log.txt ├── pycallgraph.png ├── __pycache__ │ ├── Second.cpython-37.pyc │ ├── GobangAI.cpython-37.pyc │ ├── GobangAINext.cpython-37.pyc │ ├── Submit_pro.cpython-37.pyc │ └── code_check.cpython-37.pyc ├── code_check_test.py ├── Test.py ├── defeat.py ├── Origin.py └── code_check.py ├── CARP.pdf ├── IMP.pdf ├── SVM.pdf ├── Gobang.pdf ├── CARP ├── result.xls ├── __pycache__ │ ├── CARP_info.cpython-37.pyc │ ├── CARP_algorithm.cpython-37.pyc │ └── floyd_warshall.cpython-37.pyc ├── test.py ├── CARP_samples │ ├── gdb1.dat │ ├── gdb10.dat │ ├── val1A.dat │ ├── val7A.dat │ ├── val4A.dat │ ├── egl-e1-A.dat │ ├── eglese │ │ ├── egl-e1-A.dat │ │ ├── egl-e1-B.dat │ │ ├── egl-e1-C.dat │ │ ├── egl-e2-A.dat │ │ ├── egl-e2-B.dat │ │ ├── egl-e2-C.dat │ │ ├── egl-e3-A.dat │ │ ├── egl-e3-B.dat │ │ ├── egl-e3-C.dat │ │ ├── egl-e4-A.dat │ │ ├── egl-e4-B.dat │ │ ├── egl-e4-C.dat │ │ ├── egl-s1-A.dat │ │ ├── egl-s1-B.dat │ │ ├── egl-s1-C.dat │ │ ├── egl-s2-A.dat │ │ ├── egl-s2-B.dat │ │ ├── egl-s2-C.dat │ │ ├── egl-s3-A.dat │ │ ├── egl-s3-B.dat │ │ ├── egl-s3-C.dat │ │ ├── egl-s4-A.dat │ │ ├── egl-s4-B.dat │ │ └── egl-s4-C.dat │ └── egl-s1-A.dat ├── result.txt ├── floyd_warshall.py ├── result_gdb.txt ├── result_eglese.txt ├── result_bccm.txt ├── CARP_demo.py ├── Evaluation.py ├── CARP_solver.py └── CARP_info.py ├── .idea ├── encodings.xml ├── vcs.xml ├── other.xml ├── modules.xml ├── misc.xml └── AICourse.iml ├── SVM ├── timer.py ├── data_process.py ├── adjust_hyper_para.py ├── test_data.txt ├── iris-virginica.txt ├── SVM.py ├── algorithm_new.py └── algorithm_deprecated.py └── README.md /IMP/Test/bitSetTest.py: -------------------------------------------------------------------------------- 1 | import bitarray -------------------------------------------------------------------------------- /IMP/seeds.txt: -------------------------------------------------------------------------------- 1 | 12 2 | 1 3 | 14 4 | 15 -------------------------------------------------------------------------------- /IMP/testseeds.txt: -------------------------------------------------------------------------------- 1 | 6024 2 | 37 3 | 2119 -------------------------------------------------------------------------------- /IMP/Test/01Test.py: -------------------------------------------------------------------------------- 1 | while 2: 2 | print(1) 3 | -------------------------------------------------------------------------------- /IMP/my_seeds.txt: -------------------------------------------------------------------------------- 1 | 48 2 | 53 3 | 56 4 | 58 5 | 62 -------------------------------------------------------------------------------- /IMP/seeds2.txt: -------------------------------------------------------------------------------- 1 | 56 2 | 58 3 | 53 4 | 50 5 | 48 -------------------------------------------------------------------------------- /Gobang/Testing/heuristic_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | -------------------------------------------------------------------------------- /IMP/seeds3.txt: -------------------------------------------------------------------------------- 1 | 58 2 | 56 3 | 48 4 | 52 5 | 50 6 | 28 7 | 62 8 | 53 -------------------------------------------------------------------------------- /CARP.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zh-plus/AI-course-project/HEAD/CARP.pdf -------------------------------------------------------------------------------- /Gobang/chess_log.txt: -------------------------------------------------------------------------------- 1 | 7,7,-1 2 | 6,6,1 3 | 7,8,-1 4 | 4,7,1 5 | 7,6,1 6 | 8,6,-1 -------------------------------------------------------------------------------- /IMP.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zh-plus/AI-course-project/HEAD/IMP.pdf -------------------------------------------------------------------------------- /SVM.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zh-plus/AI-course-project/HEAD/SVM.pdf -------------------------------------------------------------------------------- /Gobang.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zh-plus/AI-course-project/HEAD/Gobang.pdf -------------------------------------------------------------------------------- /Gobang/Testing/index_test.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | t = (1, 1, 1, 0, 1) 4 | print(t.index(2)) -------------------------------------------------------------------------------- /IMP/Test/reciprocalTest.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | l = [1, 2, 4, 2] 4 | print(1 / l) -------------------------------------------------------------------------------- /CARP/result.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zh-plus/AI-course-project/HEAD/CARP/result.xls -------------------------------------------------------------------------------- /IMP/Test/DNLibTest.py: -------------------------------------------------------------------------------- 1 | import networkx as nx 2 | import ndlib.models.epidemics.SIRModel as sir 3 | 4 | -------------------------------------------------------------------------------- /Gobang/pycallgraph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zh-plus/AI-course-project/HEAD/Gobang/pycallgraph.png -------------------------------------------------------------------------------- /IMP/Test/defaultDictTest.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | 3 | 4 | d = defaultdict(float) 5 | print(d[300]) -------------------------------------------------------------------------------- /IMP/Test/numpyAppendTest.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | l = np.array([]) 5 | l = np.append(l, [1]) 6 | print(l) -------------------------------------------------------------------------------- /Gobang/Testing/tostr_test.py: -------------------------------------------------------------------------------- 1 | def to_str(array): 2 | return ''.join(map(str, array)) 3 | 4 | print(to_str([1, 23, 3])) -------------------------------------------------------------------------------- /Gobang/Testing/map_test.py: -------------------------------------------------------------------------------- 1 | l = [1, 2, 3] 2 | d = {1: 4, 2: 5, 3: 6} 3 | 4 | res = sum(map(lambda x: d[x], l)) 5 | print(res) -------------------------------------------------------------------------------- /CARP/__pycache__/CARP_info.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zh-plus/AI-course-project/HEAD/CARP/__pycache__/CARP_info.cpython-37.pyc -------------------------------------------------------------------------------- /Gobang/__pycache__/Second.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zh-plus/AI-course-project/HEAD/Gobang/__pycache__/Second.cpython-37.pyc -------------------------------------------------------------------------------- /Gobang/Testing/dict_test.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | 3 | record = defaultdict(int) 4 | 5 | record['1'] += 1 6 | print(record['1']) -------------------------------------------------------------------------------- /Gobang/__pycache__/GobangAI.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zh-plus/AI-course-project/HEAD/Gobang/__pycache__/GobangAI.cpython-37.pyc -------------------------------------------------------------------------------- /CARP/__pycache__/CARP_algorithm.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zh-plus/AI-course-project/HEAD/CARP/__pycache__/CARP_algorithm.cpython-37.pyc -------------------------------------------------------------------------------- /CARP/__pycache__/floyd_warshall.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zh-plus/AI-course-project/HEAD/CARP/__pycache__/floyd_warshall.cpython-37.pyc -------------------------------------------------------------------------------- /Gobang/Testing/re_test.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | string = '-11111]'[: -1] 4 | print(string) 5 | result = re.findall('[^-]?11111', string) 6 | print(result) -------------------------------------------------------------------------------- /Gobang/Testing/unzip_test.py: -------------------------------------------------------------------------------- 1 | l1 = [1, 2, 3] 2 | l2 = [3, 4, 5] 3 | 4 | 5 | def add(*num): 6 | return sum(num) 7 | 8 | print(add(*l1, *l2)) 9 | -------------------------------------------------------------------------------- /Gobang/__pycache__/GobangAINext.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zh-plus/AI-course-project/HEAD/Gobang/__pycache__/GobangAINext.cpython-37.pyc -------------------------------------------------------------------------------- /Gobang/__pycache__/Submit_pro.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zh-plus/AI-course-project/HEAD/Gobang/__pycache__/Submit_pro.cpython-37.pyc -------------------------------------------------------------------------------- /Gobang/__pycache__/code_check.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zh-plus/AI-course-project/HEAD/Gobang/__pycache__/code_check.cpython-37.pyc -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /IMP/Test/npZeroTest.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from timer import Timer 3 | 4 | with Timer(): 5 | x = np.zeros(100000) 6 | 7 | with Timer(): 8 | x = [0 for _ in range(100000)] -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/other.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /IMP/Test/elapseTimeTest.py: -------------------------------------------------------------------------------- 1 | from timer import Timer 2 | from time import perf_counter 3 | 4 | 5 | this_start = 0.0000001 6 | with Timer(): 7 | for _ in range(465504): 8 | this_time = perf_counter() - this_start -------------------------------------------------------------------------------- /Gobang/Testing/diagonal_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | state: np.ndarray = np.array([ 5 | [ 1, 2, 1, 1], 6 | [-1, 2, 2, 1], 7 | [ 3, 0, 3, 3], 8 | [-1, 4, 1, -4] 9 | ]) 10 | 11 | print(state[::-1]) -------------------------------------------------------------------------------- /Gobang/Testing/where_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | state: np.ndarray = np.array([ 4 | [1, 1, -1, 1], 5 | [-1, -1, 0, 1], 6 | [1, 0, 1, 1], 7 | [-1, 1, 1, -1] 8 | ]) 9 | 10 | 11 | # state[state == 1], state[state == -1] = -1, 1 12 | print(state) 13 | -------------------------------------------------------------------------------- /IMP/timer.py: -------------------------------------------------------------------------------- 1 | from time import perf_counter 2 | 3 | 4 | class Timer: 5 | def __enter__(self): 6 | self.start = perf_counter() 7 | 8 | def __exit__(self, exc_type, exc_val, exc_tb): 9 | print('{} s elapsed!'.format(perf_counter() - self.start)) 10 | -------------------------------------------------------------------------------- /SVM/timer.py: -------------------------------------------------------------------------------- 1 | from time import perf_counter 2 | 3 | 4 | class Timer: 5 | def __enter__(self): 6 | self.start = perf_counter() 7 | 8 | def __exit__(self, exc_type, exc_val, exc_tb): 9 | print('{} s elapsed!'.format(perf_counter() - self.start)) 10 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Gobang/Testing/lambda_test.py: -------------------------------------------------------------------------------- 1 | def _check_substr(string, *substrings): 2 | for substring in substrings: 3 | if substring in string: 4 | return True 5 | return False 6 | 7 | 8 | line = '1111-101110' 9 | exist = lambda *substrings: _check_substr(line, *substrings) 10 | print(exist('1111-1', '01110')) -------------------------------------------------------------------------------- /IMP/Test/avgTest.py: -------------------------------------------------------------------------------- 1 | from timer import Timer 2 | import numpy as np 3 | import random 4 | 5 | 6 | def avg(l): 7 | return sum(l) / len(l) 8 | 9 | 10 | l = random.sample(range(1, 500000), 1000) 11 | 12 | with Timer(): 13 | x = np.average(l) 14 | 15 | print(x) 16 | 17 | with Timer(): 18 | x = avg(l) 19 | 20 | print(x) -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | -------------------------------------------------------------------------------- /Gobang/Testing/substring_test.py: -------------------------------------------------------------------------------- 1 | from time import time 2 | 3 | a = '101011011311012101' 4 | 5 | test = False 6 | 7 | tic = time() 8 | 9 | for x in range(1000000): 10 | # test = a.__contains__('311011') 11 | # test = '311011' in a 12 | # test = a.find('311011') 13 | test = '311011' in str.che 14 | 15 | print(time() - tic, 's') -------------------------------------------------------------------------------- /IMP/data_handle.py: -------------------------------------------------------------------------------- 1 | read_line = lambda l: list(map(int, l.split(' ')[:2])) 2 | 3 | 4 | def graph_from_file(path): 5 | with open(path, 'r') as f: 6 | lines = f.readlines() 7 | ns = [] 8 | for line in lines[1:]: 9 | u, v = read_line(line) 10 | ns.append(u) 11 | ns.append(v) 12 | 13 | print(max(ns)) 14 | 15 | 16 | graph_from_file('Amazon.txt') 17 | -------------------------------------------------------------------------------- /Gobang/code_check_test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import sys 3 | from code_check import CodeCheck 4 | 5 | 6 | def main(): 7 | code_checker = CodeCheck("C:\\Users\\10578\\PycharmProjects\\AICourse\\Gobang\\GobangAINext.py", 15) 8 | if not code_checker.check_code(): 9 | print(code_checker.errormsg) 10 | else: 11 | print('pass') 12 | 13 | 14 | if __name__ == '__main__': 15 | main() 16 | -------------------------------------------------------------------------------- /IMP/Test/randomPerfTest.py: -------------------------------------------------------------------------------- 1 | from timer import Timer 2 | import numpy as np 3 | import random 4 | 5 | rand_happen = lambda p: True if random.random() <= p else False 6 | 7 | 8 | def rand_happen1(p): 9 | return True if random.random() <= p else False 10 | 11 | 12 | with Timer(): 13 | for _ in range(1000000): 14 | rand_happen(0.8) 15 | 16 | with Timer(): 17 | for _ in range(1000000): 18 | rand_happen1(0.8) 19 | -------------------------------------------------------------------------------- /IMP/Test/npUniformTest.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from timer import Timer 3 | import random 4 | 5 | 6 | with Timer(): 7 | for _ in range(10000): 8 | threshold1 = np.random.random(10001) 9 | 10 | with Timer(): 11 | for _ in range(10000): 12 | threshold2 = np.random.uniform(size=10001) 13 | 14 | with Timer(): 15 | x = threshold1[random.randint(1, 10001)] 16 | 17 | with Timer(): 18 | x = threshold2[random.randint(1, 10001)] -------------------------------------------------------------------------------- /Gobang/Testing/slice_test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from numpy.core.multiarray import ndarray 3 | 4 | this_state: ndarray = np.array([ 5 | [-1, 1, 0, 1], 6 | [1, 0, 1, 2], 7 | [1, -1, 1, 4], 8 | [5, 6, 7, 8] 9 | ]) 10 | 11 | move = 1, 1 12 | x, y = move 13 | board_size = 4 14 | player = -1 15 | 16 | this_state = this_state[max(0, x - 1): min(board_size, x + 2), max(0, y - 1): min(board_size, y + 2)] 17 | player_num = np.sum(this_state == player) 18 | 19 | print(this_state) 20 | print(player_num) -------------------------------------------------------------------------------- /IMP/Test/numpyEfficiencyTest.py: -------------------------------------------------------------------------------- 1 | from timer import Timer 2 | 3 | import numpy as np 4 | 5 | # append 6 | with Timer(): 7 | list1 = [] 8 | for x in range(100000): 9 | list1.append(x) 10 | # read 11 | with Timer(): 12 | for x in range(100000): 13 | y = list1[x] 14 | 15 | # append 16 | with Timer(): 17 | list2 = np.array([]) 18 | for x in range(100000): 19 | list2 = np.append(list2, x) 20 | # read 21 | with Timer(): 22 | for x in range(100000): 23 | y = list2[x] 24 | -------------------------------------------------------------------------------- /Gobang/Testing/performance_test.py: -------------------------------------------------------------------------------- 1 | from time import time 2 | 3 | 4 | string = '001121021111010' 5 | substr = '211110' 6 | 7 | test_range = range(1000000) 8 | result = None 9 | 10 | tic = time() 11 | for x in test_range: 12 | result = substr in string 13 | print('in:', time() - tic, 's') 14 | 15 | tic = time() 16 | for x in test_range: 17 | result = string.__contains__(substr) 18 | print('contains:', time() - tic, 's') 19 | 20 | tic = time() 21 | for x in test_range: 22 | result = string.find(substr) 23 | print('find:', time() - tic, 's') 24 | -------------------------------------------------------------------------------- /CARP/test.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | 4 | 5 | class Timer(object): 6 | def __init__(self): 7 | self.start_time = time.perf_counter() 8 | 9 | def stop(self): 10 | print('{:.3f} seconds elapsed'.format(time.perf_counter() - self.start_time)) 11 | 12 | def __enter__(self): 13 | return self 14 | 15 | def __exit__(self, exc_type, exc_val, exc_tb): 16 | self.stop() 17 | 18 | 19 | with Timer(): 20 | os.system('python CARP_solver.py C:\\Users\\10578\\PycharmProjects\\AICourse\\CARP\\CARP_samples\\gdb1.dat -t 10 -s 1') 21 | # print(1) -------------------------------------------------------------------------------- /IMP/Test/nTest.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from math import log 3 | 4 | 5 | E1, E2 = 159, 58891 6 | V1, V2 = 62, 15233 7 | 8 | u = (E1 * V1) 9 | v = (E2 * V2) 10 | 11 | p = v / u 12 | # q = log(200, p) 13 | q = 0.5 14 | print('q:', q) 15 | 16 | x1 = 1 / (u ** q) 17 | x2 = 1 / (v ** q) 18 | print('u ^ q:', x1) 19 | print('v ^ q:', x2) 20 | x = np.array([[x1, x2]]) 21 | x = np.row_stack((x, [1, 1])) 22 | print(x) 23 | 24 | n = [10000, 500] 25 | 26 | w = np.dot(n, np.linalg.inv(x)) 27 | print(w) 28 | a, b = w 29 | 30 | 31 | def compute_n(V, E, q): 32 | global a, b 33 | return 946368.048 / ((V * E) ** 0.5) + 468.403216 34 | 35 | 36 | print(compute_n(15233, 58891, q)) -------------------------------------------------------------------------------- /CARP/CARP_samples/gdb1.dat: -------------------------------------------------------------------------------- 1 | NAME : gdb1 2 | VERTICES : 12 3 | DEPOT : 1 4 | REQUIRED EDGES : 22 5 | NON-REQUIRED EDGES : 0 6 | VEHICLES : 5 7 | CAPACITY : 5 8 | TOTAL COST OF REQUIRED EDGES : 252 9 | NODES COST DEMAND 10 | 1 2 13 1 11 | 1 4 17 1 12 | 1 7 19 1 13 | 1 10 19 1 14 | 1 12 4 1 15 | 2 3 18 1 16 | 2 4 9 1 17 | 2 9 2 1 18 | 3 4 20 1 19 | 3 5 5 1 20 | 5 6 7 1 21 | 5 11 20 1 22 | 5 12 11 1 23 | 6 7 4 1 24 | 6 12 3 1 25 | 7 8 8 1 26 | 7 12 18 1 27 | 8 10 3 1 28 | 8 11 10 1 29 | 9 10 16 1 30 | 9 11 14 1 31 | 10 11 12 1 32 | END -------------------------------------------------------------------------------- /CARP/CARP_samples/gdb10.dat: -------------------------------------------------------------------------------- 1 | NAME : gdb10 2 | VERTICES : 12 3 | DEPOT : 1 4 | REQUIRED EDGES : 25 5 | NON-REQUIRED EDGES : 0 6 | VEHICLES : 4 7 | CAPACITY : 10 8 | TOTAL COST OF REQUIRED EDGES : 252 9 | NODES COST DEMAND 10 | 1 8 9 1 11 | 1 9 4 2 12 | 1 10 13 1 13 | 1 11 12 2 14 | 2 1 11 1 15 | 2 3 15 1 16 | 2 4 18 2 17 | 2 5 8 2 18 | 3 4 8 2 19 | 3 8 18 2 20 | 3 9 6 1 21 | 4 1 7 1 22 | 4 6 10 2 23 | 4 11 17 1 24 | 5 1 9 2 25 | 5 6 15 1 26 | 6 11 3 2 27 | 7 2 6 1 28 | 7 4 11 2 29 | 7 5 5 1 30 | 8 9 14 2 31 | 8 12 5 1 32 | 9 10 19 1 33 | 10 12 2 2 34 | 11 12 7 1 35 | END -------------------------------------------------------------------------------- /.idea/AICourse.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 17 | -------------------------------------------------------------------------------- /Gobang/Test.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from pprint import pprint 3 | 4 | import numpy as np 5 | 6 | move = (0, 2) 7 | board_size = 15 8 | board = np.random.choice((0, 1, -1), (15, 15), p=(0.8, 0.1, 0.1)) # type: np.ndarray 9 | 10 | x, y = move 11 | up, down = max(0, x - 4), min(board_size, x + 5) 12 | left, right = max(0, y - 4), min(board_size, y + 5) 13 | partial_board = board[up: down, left: right] # patial of the total board 14 | 15 | padded_board = np.zeros((9, 9), int) # the padded 9 * 9 board 16 | 17 | up, down = max(0, 4 - x), min(9, 4 + (board_size - x)) 18 | left, right = max(0, 4 - y), min(9, 4 + (board_size - y)) 19 | padded_board[up: down, left: right] = partial_board 20 | 21 | testing_lines = list(zip(*[(padded_board[x, x], padded_board[8 - x, x], padded_board[4, x], padded_board[x, 4]) for x in range(9)])) 22 | 23 | pprint(board) 24 | pprint(partial_board) 25 | pprint(padded_board) 26 | pprint(testing_lines) 27 | -------------------------------------------------------------------------------- /CARP/result.txt: -------------------------------------------------------------------------------- 1 | data: egl-e1-A.dat result: 3933 2 | data: egl-e1-B.dat result: 4924 3 | data: egl-e1-C.dat result: 5868 4 | data: egl-e2-A.dat result: 5780 5 | data: egl-e2-B.dat result: 7064 6 | data: egl-e2-C.dat result: 9383 7 | data: egl-e3-A.dat result: 6775 8 | data: egl-e3-B.dat result: 8674 9 | data: egl-e3-C.dat result: 11653 10 | data: egl-e4-A.dat result: 8191 11 | data: egl-e4-B.dat result: 10515 12 | data: egl-e4-C.dat result: 13885 13 | data: egl-s1-A.dat result: 6148 14 | data: egl-s1-B.dat result: 7352 15 | data: egl-s1-C.dat result: 9464 16 | data: egl-s2-A.dat result: 13914 17 | data: egl-s2-B.dat result: 16619 18 | data: egl-s2-C.dat result: 20999 19 | data: egl-s3-A.dat result: 13630 20 | data: egl-s3-B.dat result: 17779 21 | data: egl-s3-C.dat result: 21830 22 | data: egl-s4-A.dat result: 17133 23 | data: egl-s4-B.dat result: 21214 24 | data: egl-s4-C.dat result: 26563 25 | -------------------------------------------------------------------------------- /Gobang/defeat.py: -------------------------------------------------------------------------------- 1 | from Gobang.Submit_pro import AI as AI_pro 2 | from Gobang.Submit_final import AI as AI_next 3 | from Gobang.Submit_pro import Game 4 | import numpy as np 5 | 6 | 7 | pro_color = 1 8 | next_color = -1 9 | 10 | board = np.zeros((15, 15)) 11 | ai_pro = AI_pro(15, pro_color, 5) 12 | ai_next = AI_next(15, next_color, 5) 13 | 14 | game_pro = Game(pro_color) 15 | game_next = Game(next_color) 16 | 17 | while 1: 18 | ai_next.go(board) 19 | next_action = ai_next.candidate_list[-1] 20 | print('next_action:', next_action) 21 | if game_next.terminal_test_great(board, next_action): 22 | break 23 | board[next_action[0]][next_action[1]] = next_color 24 | print(board) 25 | 26 | ai_pro.go(board) 27 | pro_action = ai_pro.candidate_list[-1] 28 | print('pro_action:', pro_action) 29 | if game_next.terminal_test_great(board, pro_action): 30 | break 31 | board[pro_action[0]][pro_action[1]] = pro_color 32 | print(board) 33 | 34 | 35 | print(board) 36 | -------------------------------------------------------------------------------- /CARP/floyd_warshall.py: -------------------------------------------------------------------------------- 1 | from numpy import minimum, newaxis 2 | 3 | 4 | def floyd_warshall_fastest(adjacency_matrix): 5 | """floyd_warshall_fastest(adjacency_matrix) -> shortest_path_distance_matrix 6 | Input 7 | An NxN NumPy array describing the directed distances between N nodes. 8 | adjacency_matrix[i,j] = distance to travel directly from node i to node j (without passing through other nodes) 9 | Notes: 10 | * If there is no edge connecting i->j then adjacency_matrix[i,j] should be equal to numpy.inf. 11 | * The diagonal of adjacency_matrix should be zero. 12 | Output 13 | An NxN NumPy array such that result[i,j] is the shortest distance to travel between node i and node j. If no such path exists then result[i,j] == numpy.inf 14 | """ 15 | n = len(adjacency_matrix) 16 | 17 | for k in range(n): 18 | adjacency_matrix = minimum(adjacency_matrix, adjacency_matrix[newaxis, k, :] + adjacency_matrix[:, k, newaxis]) 19 | 20 | return adjacency_matrix -------------------------------------------------------------------------------- /CARP/result_gdb.txt: -------------------------------------------------------------------------------- 1 | size: 40 avg_cost: 293.9760869565217 2 | size: 45 avg_cost: 297.0086956521739 3 | size: 50 avg_cost: 300.0108695652174 4 | size: 55 avg_cost: 301.4478260869565 5 | size: 60 avg_cost: 303.59565217391304 6 | size: 65 avg_cost: 305.1717391304348 7 | size: 70 avg_cost: 306.2 8 | size: 75 avg_cost: 308.7826086956522 9 | size: 80 avg_cost: 310.8021739130435 10 | size: 85 avg_cost: 310.94565217391306 11 | size: 90 avg_cost: 312.39130434782606 12 | size: 95 avg_cost: 313.3 13 | size: 100 avg_cost: 314.9478260869565 14 | size: 105 avg_cost: 316.3282608695652 15 | size: 110 avg_cost: 317.02608695652174 16 | size: 115 avg_cost: 317.74782608695654 17 | size: 120 avg_cost: 317.6891304347826 18 | size: 125 avg_cost: 319.2130434782609 19 | size: 130 avg_cost: 320.3652173913043 20 | size: 135 avg_cost: 319.7130434782609 21 | size: 140 avg_cost: 320.89130434782606 22 | size: 145 avg_cost: 321.50217391304346 23 | size: 150 avg_cost: 321.9260869565217 24 | size: 155 avg_cost: 322.5521739130435 25 | size: 160 avg_cost: 322.71521739130435 26 | size: 165 avg_cost: 323.18695652173915 27 | size: 170 avg_cost: 324.6 28 | -------------------------------------------------------------------------------- /CARP/CARP_samples/val1A.dat: -------------------------------------------------------------------------------- 1 | NAME : val1A 2 | VERTICES : 24 3 | DEPOT : 1 4 | REQUIRED EDGES : 39 5 | NON-REQUIRED EDGES : 0 6 | VEHICLES : 2 7 | CAPACITY : 200 8 | TOTAL COST OF REQUIRED EDGES : 146 9 | NODES COST DEMAND 10 | 1 5 3 4 11 | 1 9 3 9 12 | 1 11 7 12 13 | 1 19 4 13 14 | 1 20 5 13 15 | 2 4 1 2 16 | 2 5 3 12 17 | 2 10 2 5 18 | 3 4 5 14 19 | 3 9 2 4 20 | 3 10 3 7 21 | 4 5 4 3 22 | 5 6 10 19 23 | 5 11 4 9 24 | 6 7 3 12 25 | 6 11 7 18 26 | 6 12 4 13 27 | 7 8 4 12 28 | 7 13 4 5 29 | 8 14 4 9 30 | 9 19 5 14 31 | 11 12 2 5 32 | 12 16 3 5 33 | 12 17 4 7 34 | 13 14 5 10 35 | 13 17 2 3 36 | 14 18 3 8 37 | 15 16 2 4 38 | 15 20 3 8 39 | 15 21 6 14 40 | 16 17 1 4 41 | 17 18 2 4 42 | 19 20 5 14 43 | 19 22 5 13 44 | 20 21 3 12 45 | 21 23 5 12 46 | 21 24 4 13 47 | 22 23 2 9 48 | 23 24 2 4 49 | END -------------------------------------------------------------------------------- /CARP/result_eglese.txt: -------------------------------------------------------------------------------- 1 | size: 40 avg_cost: 13358.172916666666 2 | size: 45 avg_cost: 13362.877083333333 3 | size: 50 avg_cost: 13361.81875 4 | size: 55 avg_cost: 13371.364583333334 5 | size: 60 avg_cost: 13374.595833333333 6 | size: 65 avg_cost: 13384.629166666668 7 | size: 70 avg_cost: 13380.525 8 | size: 75 avg_cost: 13375.329166666666 9 | size: 80 avg_cost: 13374.458333333334 10 | size: 85 avg_cost: 13389.883333333333 11 | size: 90 avg_cost: 13380.375 12 | size: 95 avg_cost: 13386.104166666666 13 | size: 100 avg_cost: 13381.935416666667 14 | size: 105 avg_cost: 13395.191666666668 15 | size: 110 avg_cost: 13381.139583333334 16 | size: 115 avg_cost: 13384.439583333333 17 | size: 120 avg_cost: 13378.372916666667 18 | size: 125 avg_cost: 13391.954166666666 19 | size: 130 avg_cost: 13389.81875 20 | size: 135 avg_cost: 13390.589583333332 21 | size: 140 avg_cost: 13392.427083333334 22 | size: 145 avg_cost: 13393.789583333333 23 | size: 150 avg_cost: 13391.277083333332 24 | size: 155 avg_cost: 13392.541666666666 25 | size: 160 avg_cost: 13392.291666666666 26 | size: 165 avg_cost: 13389.995833333332 27 | size: 170 avg_cost: 13391.441666666668 28 | -------------------------------------------------------------------------------- /CARP/result_bccm.txt: -------------------------------------------------------------------------------- 1 | size: 40 avg_cost: 499.9117647058824 2 | size: 45 avg_cost: 501.24264705882354 3 | size: 50 avg_cost: 502.525 4 | size: 55 avg_cost: 502.8029411764706 5 | size: 60 avg_cost: 502.8720588235294 6 | size: 65 avg_cost: 504.3926470588235 7 | size: 70 avg_cost: 504.8558823529412 8 | size: 75 avg_cost: 505.58529411764704 9 | size: 80 avg_cost: 505.24264705882354 10 | size: 85 avg_cost: 505.90294117647056 11 | size: 90 avg_cost: 506.2176470588235 12 | size: 95 avg_cost: 506.3073529411765 13 | size: 100 avg_cost: 506.86323529411766 14 | size: 105 avg_cost: 507.6411764705882 15 | size: 110 avg_cost: 507.05882352941177 16 | size: 115 avg_cost: 507.29117647058825 17 | size: 120 avg_cost: 507.47205882352944 18 | size: 125 avg_cost: 507.5882352941176 19 | size: 130 avg_cost: 507.8220588235294 20 | size: 135 avg_cost: 507.78823529411767 21 | size: 140 avg_cost: 508.1602941176471 22 | size: 145 avg_cost: 507.8426470588235 23 | size: 150 avg_cost: 507.77794117647056 24 | size: 155 avg_cost: 507.96176470588233 25 | size: 160 avg_cost: 508.53970588235296 26 | size: 165 avg_cost: 507.6426470588235 27 | size: 170 avg_cost: 508.06470588235294 28 | -------------------------------------------------------------------------------- /SVM/data_process.py: -------------------------------------------------------------------------------- 1 | from pprint import pprint 2 | import random 3 | import numpy as np 4 | 5 | # random.seed(1) 6 | 7 | 8 | def read_data(path): 9 | with open(path, 'r') as f: 10 | data = f.readlines() 11 | data = list(map(lambda s: s.split(' '), data)) 12 | for d in data: 13 | d[-1] = d[-1].strip() 14 | 15 | for i in range(len(data)): 16 | data[i] = list(map(float, data[i])) 17 | 18 | return np.array(data) 19 | 20 | 21 | def split_data(data): 22 | random.shuffle(data) 23 | # training: testing = 7: 3 24 | split_point = (len(data) * 9) // 10 25 | return data[: split_point], data[split_point:] 26 | 27 | 28 | def convert_format_to_their(): 29 | file = open('my_data.txt', 'w') 30 | with open('train_data.txt', 'r') as f: 31 | data = f.readlines() 32 | data = list(map(lambda s: s.replace(' ', ','), data)) 33 | file.writelines(data) 34 | 35 | 36 | def convert_format_to_me(): 37 | file = open('my_data_cancer.txt', 'w') 38 | with open('breast_cancer_train.txt', 'r') as f: 39 | data = f.readlines() 40 | data = list(map(lambda s: s.replace('\t', ' '), data)) 41 | file.writelines(data) 42 | 43 | 44 | if __name__ == '__main__': 45 | convert_format_to_me() 46 | -------------------------------------------------------------------------------- /Gobang/Origin.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import random 3 | import time 4 | 5 | COLOR_BLACK = -1 6 | COLOR_WHITE = 1 7 | COLOR_NONE = 0 8 | random.seed(0) 9 | 10 | 11 | # don’t change the class name 12 | class AI(object): 13 | # chessboard_size, color, time_out passed from agent 14 | def __init__(self, chessboard_size, color, time_out): 15 | self.chessboard_size = chessboard_size 16 | # You are white or black 17 | self.color = color 18 | # the max time you should use, your algorithm’s run time must not exceed the time limit. 19 | self.time_out = time_out 20 | # You need add your decision into your candidate_list. 21 | # System will get the end of your candidate_list as your decision. 22 | self.candidate_list = [] 23 | 24 | # The input is current chessboard. 25 | def go(self, chessboard): 26 | # Clear candidate_list 27 | self.candidate_list.clear() 28 | # ================================================================== 29 | # To write your algorithm here 30 | # Here is the simplest sample:Random decision 31 | idx = np.where(chessboard == 0) 32 | idx = list(zip(idx[0], idx[1])) 33 | pos_idx = random.randint(0, len(idx) - 1) 34 | new_pos = idx[pos_idx] 35 | # ==============Find new pos======================================== 36 | # Make sure that the position of your decision in chess board is empty. 37 | # If not, return error. 38 | assert chessboard[new_pos[0], new_pos[1]] == 0 39 | # Add your decision into candidate_list, Records the chess board 40 | self.candidate_list.append(new_pos) 41 | -------------------------------------------------------------------------------- /Gobang/Testing/evaluate_test.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | import numpy as np 4 | 5 | state: np.ndarray = np.array([ 6 | [ 1, 1, 1, 1], 7 | [-1, 1, 0, 1], 8 | [ 1, 0, 1, 1], 9 | [-1, 1, 1, -1] 10 | ]) 11 | np.where(state == -1) 12 | 13 | to_str = lambda array: ''.join(map(str, array)) 14 | 15 | 16 | # re_counter = lambda pattern, string: len(re.findall(pattern, string)) 17 | 18 | def re_counter(pattern, string): 19 | result = re.findall(pattern, string) 20 | 21 | if len(result) != 0: 22 | print('{:>30} : {}'.format(pattern, string)) 23 | 24 | return len(result) 25 | 26 | 27 | pattern_value = { 28 | '[^-]?(1){4}': 100000, # 连5 29 | '[^-1]?(1){3}[^(-1)]?': 10000, # 连3 #TODO 30 | } 31 | 32 | mark = 0 33 | for i in range(len(state)): 34 | horizontal = to_str(state[i]) 35 | vertical = to_str(state[..., i]) 36 | lr_diag = to_str(state.diagonal(i)) 37 | rl_diag = to_str(state.diagonal(-i)) 38 | 39 | 40 | # def counter(pattern): 41 | # a = re_counter(pattern, horizontal) 42 | # b = re_counter(pattern, vertical) 43 | # c = re_counter(pattern, lr_diag) 44 | # d = re_counter(pattern, rl_diag) 45 | # return pattern_value[pattern] * (a + b + c + d) 46 | 47 | 48 | counter = lambda pattern: pattern_value[pattern] * (re_counter(pattern, horizontal) + re_counter(pattern, vertical) 49 | + re_counter(pattern, lr_diag) + re_counter(pattern, rl_diag)) 50 | 51 | mark += sum(map(counter, pattern_value.keys())) 52 | 53 | # for pattern, value in pattern_value.items(): 54 | # mark += counter(pattern) 55 | 56 | print(mark) 57 | -------------------------------------------------------------------------------- /CARP/CARP_samples/val7A.dat: -------------------------------------------------------------------------------- 1 | NAME : val7A 2 | VERTICES : 40 3 | DEPOT : 1 4 | REQUIRED EDGES : 66 5 | NON-REQUIRED EDGES : 0 6 | VEHICLES : 3 7 | CAPACITY : 200 8 | TOTAL COST OF REQUIRED EDGES : 249 9 | NODES COST DEMAND 10 | 1 2 2 3 11 | 1 6 3 8 12 | 1 8 4 9 13 | 1 10 8 12 14 | 1 11 3 12 15 | 1 33 4 9 16 | 1 35 4 12 17 | 1 40 4 10 18 | 2 3 5 9 19 | 2 6 2 8 20 | 2 36 3 12 21 | 3 4 2 5 22 | 3 7 2 3 23 | 3 37 4 6 24 | 4 5 3 7 25 | 4 38 3 4 26 | 5 39 2 6 27 | 6 7 4 7 28 | 6 12 2 4 29 | 7 13 1 3 30 | 8 9 5 12 31 | 8 14 7 10 32 | 8 40 5 8 33 | 9 10 2 6 34 | 9 15 2 9 35 | 10 16 4 8 36 | 11 12 7 9 37 | 11 16 3 10 38 | 12 13 6 8 39 | 12 17 4 8 40 | 13 17 2 6 41 | 13 18 2 5 42 | 13 19 6 12 43 | 14 15 4 7 44 | 15 16 3 8 45 | 17 18 5 8 46 | 17 20 6 12 47 | 18 19 4 14 48 | 18 22 6 13 49 | 20 21 2 3 50 | 20 23 3 8 51 | 21 22 1 4 52 | 21 24 3 6 53 | 22 25 1 3 54 | 23 24 2 7 55 | 24 25 2 5 56 | 26 27 4 8 57 | 26 33 5 14 58 | 26 34 5 16 59 | 27 28 3 11 60 | 27 34 3 9 61 | 28 35 10 15 62 | 29 30 3 12 63 | 29 36 4 9 64 | 30 31 3 10 65 | 30 37 3 5 66 | 31 32 5 12 67 | 31 37 5 7 68 | 31 38 4 9 69 | 32 39 2 8 70 | 33 34 3 8 71 | 34 35 6 8 72 | 35 36 6 14 73 | 36 37 4 13 74 | 37 38 2 4 75 | 38 39 7 9 76 | END -------------------------------------------------------------------------------- /CARP/CARP_samples/val4A.dat: -------------------------------------------------------------------------------- 1 | NAME : val4A 2 | VERTICES : 41 3 | DEPOT : 1 4 | REQUIRED EDGES : 69 5 | NON-REQUIRED EDGES : 0 6 | VEHICLES : 3 7 | CAPACITY : 225 8 | TOTAL COST OF REQUIRED EDGES : 343 9 | NODES COST DEMAND 10 | 1 2 1 2 11 | 1 7 5 15 12 | 2 3 1 2 13 | 2 8 10 20 14 | 3 4 7 15 15 | 3 9 1 1 16 | 4 5 1 2 17 | 4 10 3 7 18 | 5 6 6 13 19 | 5 11 4 8 20 | 6 12 3 8 21 | 7 8 2 3 22 | 7 13 7 14 23 | 8 9 2 3 24 | 9 10 3 6 25 | 9 14 5 10 26 | 10 11 4 7 27 | 10 15 1 3 28 | 11 12 5 9 29 | 11 16 6 12 30 | 11 17 2 4 31 | 13 14 8 16 32 | 13 23 7 14 33 | 14 15 3 8 34 | 14 23 3 7 35 | 14 24 4 13 36 | 15 16 3 6 37 | 15 25 9 19 38 | 16 17 9 15 39 | 16 19 1 2 40 | 17 18 4 7 41 | 17 20 2 4 42 | 18 22 6 14 43 | 19 20 3 4 44 | 19 26 8 10 45 | 20 21 5 10 46 | 20 27 5 8 47 | 21 22 4 9 48 | 21 27 1 3 49 | 22 28 5 12 50 | 23 24 3 4 51 | 23 29 5 14 52 | 24 25 4 7 53 | 24 30 8 12 54 | 25 26 6 9 55 | 25 31 7 11 56 | 26 27 7 11 57 | 26 32 7 13 58 | 27 28 7 13 59 | 27 32 3 8 60 | 27 33 5 9 61 | 29 30 3 6 62 | 29 34 14 17 63 | 30 31 12 19 64 | 31 32 5 9 65 | 31 35 4 7 66 | 32 36 10 13 67 | 33 37 2 4 68 | 34 35 2 4 69 | 34 38 7 12 70 | 35 36 8 12 71 | 35 39 10 14 72 | 36 37 8 12 73 | 36 39 3 9 74 | 36 40 4 7 75 | 37 41 5 9 76 | 38 39 9 10 77 | 39 40 2 3 78 | 40 41 4 4 79 | END -------------------------------------------------------------------------------- /SVM/adjust_hyper_para.py: -------------------------------------------------------------------------------- 1 | from multiprocessing import Pool 2 | from pprint import pprint 3 | from SVM import SVM 4 | 5 | import matplotlib.pyplot as plt 6 | import numpy as np 7 | import os 8 | import time 9 | 10 | 11 | def train_and_test(C, tolerance): 12 | svm = SVM(C=C, tolerance=tolerance) 13 | svm.train() 14 | acc = svm.test() 15 | # print(os.getpid(), acc) 16 | return [acc, svm] 17 | 18 | 19 | def atomic(C, tolerance): 20 | with Pool(16) as p: 21 | future_results = [p.apply_async(train_and_test, args=(C, tolerance)) for i in range(16)] 22 | results = np.array([f.get() for f in future_results]) 23 | # svm = SVM() 24 | # pprint(results) 25 | max_acc = max(results, key=lambda x: x[0]) 26 | # print(max(results, key=lambda x: x[0])) 27 | 28 | return max_acc[0] 29 | 30 | 31 | def plot(result): 32 | x, y = zip(*result) 33 | plt.scatter(x, y) 34 | # plt.title('') 35 | plt.xlabel('parameters') 36 | plt.ylabel('accuracy') 37 | 38 | this_time = time.strftime('%d_%H_%M_%S', time.localtime(time.time())) 39 | plt.savefig('parameter_' + this_time) 40 | 41 | 42 | def optimize_C(start=8, end=13, step=1, tolerance=0.001): 43 | Cs = range(start, end, step) 44 | results = [] 45 | for C in Cs: 46 | result = [] 47 | for i in range(10): 48 | result.append(atomic(C, tolerance)) 49 | print(i, end=' ') 50 | print() 51 | 52 | mean = np.mean(result) 53 | print(f'{C}: {mean}') 54 | results.append([C, mean]) 55 | 56 | plot(results) 57 | 58 | 59 | def optimize_t(C=9, tolerance=0.001): 60 | ts = [0.1, 0.01, 0.001, 0.0001, 0.00001] 61 | results = [] 62 | for t in ts: 63 | result = [] 64 | for i in range(10): 65 | result.append(atomic(C, t)) 66 | print(i, end=' ') 67 | print() 68 | 69 | mean = np.mean(result) 70 | print(f'{t}: {mean}') 71 | results.append([t, mean]) 72 | 73 | plot(results) 74 | 75 | 76 | if __name__ == '__main__': 77 | optimize_t() 78 | -------------------------------------------------------------------------------- /IMP/graph.py: -------------------------------------------------------------------------------- 1 | from math import ceil 2 | from timer import Timer 3 | import numpy as np 4 | 5 | 6 | class Graph: 7 | """ 8 | Implemented by adjacent map 9 | """ 10 | 11 | def __init__(self, node_size, edge_size): 12 | self.node_size = node_size 13 | self.edge_size = edge_size 14 | self.adj = [set() for _ in range(node_size + 1)] 15 | self.reverse_adj = [set() for _ in range(node_size + 1)] 16 | self.weight = np.zeros(node_size + 1) 17 | 18 | def add_edge(self, u, v): 19 | self.adj[u].add(v) 20 | self.reverse_adj[v].add(u) 21 | 22 | def compute_weight(self): 23 | in_degrees = [len(s) for s in self.reverse_adj] 24 | for i, degree in enumerate(in_degrees): 25 | if degree: 26 | self.weight[i] = 1 / degree 27 | 28 | def get_children(self, node): 29 | return self.adj[node] 30 | 31 | def get_parents(self, node): 32 | return self.reverse_adj[node] 33 | 34 | def get_out_degree(self, node): 35 | return len(self.adj[node]) 36 | 37 | def get_in_degree(self, node): 38 | return len(self.reverse_adj[node]) 39 | 40 | def get_weight(self, inactivate): 41 | return self.weight[inactivate] 42 | 43 | 44 | read_line = lambda l: list(map(int, l.split(' ')[:2])) 45 | 46 | 47 | def graph_from_file(path): 48 | with open(path, 'r') as f: 49 | lines = f.readlines() 50 | V, E = read_line(lines[0]) 51 | 52 | g = Graph(V, E) 53 | for line in lines[1:]: 54 | g.add_edge(*read_line(line)) 55 | g.compute_weight() 56 | 57 | return g 58 | 59 | 60 | def seed_from_file(path): 61 | with open(path, 'r') as f: 62 | seed = set([int(x) for x in f.readlines()]) 63 | return seed 64 | 65 | 66 | def compute_n(V, E): 67 | return ceil(946368.048 / ((V * E) ** 0.5) + 468.403216) 68 | 69 | 70 | if __name__ == '__main__': 71 | path = 'network.txt' 72 | with Timer(): 73 | g = graph_from_file(path) 74 | print(g.adj) 75 | print(g.reverse_adj) 76 | print(g.get_out_degree(14)) 77 | print(g.get_in_degree(1)) 78 | -------------------------------------------------------------------------------- /IMP/network.txt: -------------------------------------------------------------------------------- 1 | 62 159 2 | 9 4 0.333333 3 | 10 6 0.25 4 | 10 7 0.166667 5 | 11 1 0.166667 6 | 11 3 0.25 7 | 14 6 0.25 8 | 14 7 0.166667 9 | 14 10 0.2 10 | 15 1 0.166667 11 | 15 4 0.333333 12 | 16 1 0.166667 13 | 17 15 0.1 14 | 18 2 0.125 15 | 18 7 0.166667 16 | 18 10 0.2 17 | 18 14 0.2 18 | 19 16 0.166667 19 | 20 2 0.125 20 | 20 8 0.2 21 | 21 9 0.2 22 | 21 17 0.2 23 | 21 19 0.166667 24 | 22 19 0.166667 25 | 23 18 0.2 26 | 25 15 0.1 27 | 25 16 0.166667 28 | 25 19 0.166667 29 | 26 18 0.2 30 | 27 2 0.125 31 | 27 26 0.5 32 | 28 2 0.125 33 | 28 8 0.2 34 | 28 18 0.2 35 | 28 26 0.5 36 | 28 27 1 37 | 29 2 0.125 38 | 29 9 0.2 39 | 29 21 0.166667 40 | 30 11 0.333333 41 | 30 19 0.166667 42 | 30 22 0.2 43 | 30 25 0.333333 44 | 31 8 0.2 45 | 31 20 0.5 46 | 31 29 0.5 47 | 32 18 0.2 48 | 33 10 0.2 49 | 33 14 0.2 50 | 34 13 1 51 | 34 15 0.1 52 | 34 17 0.2 53 | 34 22 0.2 54 | 35 15 0.1 55 | 35 34 0.166667 56 | 36 30 0.2 57 | 37 2 0.125 58 | 37 21 0.166667 59 | 37 24 0.333333 60 | 38 9 0.2 61 | 38 15 0.1 62 | 38 17 0.2 63 | 38 22 0.2 64 | 38 34 0.166667 65 | 38 35 0.333333 66 | 38 37 0.25 67 | 39 15 0.1 68 | 39 17 0.2 69 | 39 21 0.166667 70 | 39 34 0.166667 71 | 40 37 0.25 72 | 41 1 0.166667 73 | 41 8 0.2 74 | 41 15 0.1 75 | 41 16 0.166667 76 | 41 34 0.166667 77 | 41 37 0.25 78 | 41 38 0.25 79 | 42 2 0.125 80 | 42 10 0.2 81 | 42 14 0.2 82 | 43 1 0.166667 83 | 43 3 0.25 84 | 43 11 0.333333 85 | 43 31 0.5 86 | 44 15 0.1 87 | 44 30 0.2 88 | 44 34 0.166667 89 | 44 38 0.25 90 | 44 39 0.25 91 | 45 3 0.25 92 | 45 21 0.166667 93 | 45 35 0.333333 94 | 45 39 0.25 95 | 46 9 0.2 96 | 46 16 0.166667 97 | 46 19 0.166667 98 | 46 22 0.2 99 | 46 24 0.333333 100 | 46 25 0.333333 101 | 46 30 0.2 102 | 46 38 0.25 103 | 47 44 0.5 104 | 48 1 0.166667 105 | 48 11 0.333333 106 | 48 21 0.166667 107 | 48 29 0.5 108 | 48 31 0.5 109 | 48 43 0.5 110 | 50 35 0.333333 111 | 50 47 1 112 | 51 15 0.1 113 | 51 17 0.2 114 | 51 21 0.166667 115 | 51 34 0.166667 116 | 51 43 0.5 117 | 51 46 0.333333 118 | 52 5 1 119 | 52 12 1 120 | 52 19 0.166667 121 | 52 22 0.2 122 | 52 24 0.333333 123 | 52 25 0.333333 124 | 52 30 0.2 125 | 52 46 0.333333 126 | 52 51 1 127 | 53 15 0.1 128 | 53 30 0.2 129 | 53 39 0.25 130 | 53 41 1 131 | 54 44 0.5 132 | 55 2 0.125 133 | 55 7 0.166667 134 | 55 8 0.2 135 | 55 14 0.2 136 | 55 20 0.5 137 | 55 42 0.5 138 | 56 16 0.166667 139 | 56 52 1 140 | 57 6 0.25 141 | 57 7 0.166667 142 | 58 6 0.25 143 | 58 7 0.166667 144 | 58 10 0.2 145 | 58 14 0.2 146 | 58 18 0.2 147 | 58 40 1 148 | 58 42 0.5 149 | 58 49 1 150 | 58 55 1 151 | 59 39 0.25 152 | 60 4 0.333333 153 | 60 9 0.2 154 | 60 16 0.166667 155 | 60 37 0.25 156 | 60 46 0.333333 157 | 61 33 1 158 | 62 3 0.25 159 | 62 38 0.25 160 | 62 54 1 161 | -------------------------------------------------------------------------------- /CARP/CARP_samples/egl-e1-A.dat: -------------------------------------------------------------------------------- 1 | NAME : egl-e1-A 2 | VERTICES : 77 3 | DEPOT : 1 4 | REQUIRED EDGES : 51 5 | NON-REQUIRED EDGES : 47 6 | VEHICLES : 5 7 | CAPACITY : 305 8 | TOTAL COST OF REQUIRED EDGES : 1468 9 | NODES COST DEMAND 10 | 1 2 32 32 11 | 2 3 14 14 12 | 2 4 17 17 13 | 4 5 56 56 14 | 9 10 20 20 15 | 11 12 32 32 16 | 12 16 29 29 17 | 13 16 13 13 18 | 13 14 7 7 19 | 15 17 26 26 20 | 15 18 38 38 21 | 18 19 41 41 22 | 19 20 32 32 23 | 19 21 38 38 24 | 21 22 17 17 25 | 22 75 24 24 26 | 23 31 40 40 27 | 31 32 58 58 28 | 32 33 30 30 29 | 32 34 43 43 30 | 32 35 86 86 31 | 35 41 15 15 32 | 43 44 78 78 33 | 44 45 12 12 34 | 44 46 13 13 35 | 46 47 33 33 36 | 47 48 10 10 37 | 47 49 9 9 38 | 49 50 8 8 39 | 49 51 10 10 40 | 50 52 2 2 41 | 52 54 4 4 42 | 55 56 6 6 43 | 42 57 14 14 44 | 57 58 78 78 45 | 58 59 25 25 46 | 11 59 78 78 47 | 58 60 33 33 48 | 60 61 39 39 49 | 60 62 23 23 50 | 62 63 30 30 51 | 63 65 21 21 52 | 62 66 16 16 53 | 66 68 45 45 54 | 58 69 32 32 55 | 59 69 11 11 56 | 4 69 75 75 57 | 23 75 17 17 58 | 20 76 8 8 59 | 21 51 2 2 60 | 44 59 28 28 61 | 5 6 8 0 62 | 5 7 6 0 63 | 7 8 18 0 64 | 8 9 26 0 65 | 10 11 12 0 66 | 14 15 7 0 67 | 13 77 12 0 68 | 18 20 21 0 69 | 22 24 4 0 70 | 24 25 27 0 71 | 25 75 16 0 72 | 25 26 17 0 73 | 23 26 12 0 74 | 26 27 15 0 75 | 26 28 18 0 76 | 28 29 16 0 77 | 25 29 27 0 78 | 30 31 14 0 79 | 33 36 27 0 80 | 33 37 75 0 81 | 37 38 8 0 82 | 37 39 21 0 83 | 39 40 5 0 84 | 35 39 7 0 85 | 40 41 9 0 86 | 41 42 18 0 87 | 42 43 11 0 88 | 45 46 12 0 89 | 11 48 79 0 90 | 51 53 4 0 91 | 52 53 4 0 92 | 24 53 24 0 93 | 19 50 30 0 94 | 42 56 26 0 95 | 63 64 9 0 96 | 70 71 56 0 97 | 71 72 6 0 98 | 71 73 5 0 99 | 72 73 5 0 100 | 73 74 25 0 101 | 18 72 92 0 102 | 56 67 36 0 103 | 12 76 7 0 104 | 15 77 13 0 105 | 76 77 35 0 106 | 60 67 29 0 107 | 62 67 31 0 108 | END -------------------------------------------------------------------------------- /CARP/CARP_samples/eglese/egl-e1-A.dat: -------------------------------------------------------------------------------- 1 | NAME : egl-e1-A 2 | VERTICES : 77 3 | DEPOT : 1 4 | REQUIRED EDGES : 51 5 | NON-REQUIRED EDGES : 47 6 | VEHICLES : 5 7 | CAPACITY : 305 8 | TOTAL COST OF REQUIRED EDGES : 1468 9 | NODES COST DEMAND 10 | 1 2 32 32 11 | 2 3 14 14 12 | 2 4 17 17 13 | 4 5 56 56 14 | 9 10 20 20 15 | 11 12 32 32 16 | 12 16 29 29 17 | 13 16 13 13 18 | 13 14 7 7 19 | 15 17 26 26 20 | 15 18 38 38 21 | 18 19 41 41 22 | 19 20 32 32 23 | 19 21 38 38 24 | 21 22 17 17 25 | 22 75 24 24 26 | 23 31 40 40 27 | 31 32 58 58 28 | 32 33 30 30 29 | 32 34 43 43 30 | 32 35 86 86 31 | 35 41 15 15 32 | 43 44 78 78 33 | 44 45 12 12 34 | 44 46 13 13 35 | 46 47 33 33 36 | 47 48 10 10 37 | 47 49 9 9 38 | 49 50 8 8 39 | 49 51 10 10 40 | 50 52 2 2 41 | 52 54 4 4 42 | 55 56 6 6 43 | 42 57 14 14 44 | 57 58 78 78 45 | 58 59 25 25 46 | 11 59 78 78 47 | 58 60 33 33 48 | 60 61 39 39 49 | 60 62 23 23 50 | 62 63 30 30 51 | 63 65 21 21 52 | 62 66 16 16 53 | 66 68 45 45 54 | 58 69 32 32 55 | 59 69 11 11 56 | 4 69 75 75 57 | 23 75 17 17 58 | 20 76 8 8 59 | 21 51 2 2 60 | 44 59 28 28 61 | 5 6 8 0 62 | 5 7 6 0 63 | 7 8 18 0 64 | 8 9 26 0 65 | 10 11 12 0 66 | 14 15 7 0 67 | 13 77 12 0 68 | 18 20 21 0 69 | 22 24 4 0 70 | 24 25 27 0 71 | 25 75 16 0 72 | 25 26 17 0 73 | 23 26 12 0 74 | 26 27 15 0 75 | 26 28 18 0 76 | 28 29 16 0 77 | 25 29 27 0 78 | 30 31 14 0 79 | 33 36 27 0 80 | 33 37 75 0 81 | 37 38 8 0 82 | 37 39 21 0 83 | 39 40 5 0 84 | 35 39 7 0 85 | 40 41 9 0 86 | 41 42 18 0 87 | 42 43 11 0 88 | 45 46 12 0 89 | 11 48 79 0 90 | 51 53 4 0 91 | 52 53 4 0 92 | 24 53 24 0 93 | 19 50 30 0 94 | 42 56 26 0 95 | 63 64 9 0 96 | 70 71 56 0 97 | 71 72 6 0 98 | 71 73 5 0 99 | 72 73 5 0 100 | 73 74 25 0 101 | 18 72 92 0 102 | 56 67 36 0 103 | 12 76 7 0 104 | 15 77 13 0 105 | 76 77 35 0 106 | 60 67 29 0 107 | 62 67 31 0 108 | END -------------------------------------------------------------------------------- /CARP/CARP_samples/eglese/egl-e1-B.dat: -------------------------------------------------------------------------------- 1 | NAME : egl-e1-B 2 | VERTICES : 77 3 | DEPOT : 1 4 | REQUIRED EDGES : 51 5 | NON-REQUIRED EDGES : 47 6 | VEHICLES : 7 7 | CAPACITY : 220 8 | TOTAL COST OF REQUIRED EDGES : 1468 9 | NODES COST DEMAND 10 | 1 2 32 32 11 | 2 3 14 14 12 | 2 4 17 17 13 | 4 5 56 56 14 | 9 10 20 20 15 | 11 12 32 32 16 | 12 16 29 29 17 | 13 16 13 13 18 | 13 14 7 7 19 | 15 17 26 26 20 | 15 18 38 38 21 | 18 19 41 41 22 | 19 20 32 32 23 | 19 21 38 38 24 | 21 22 17 17 25 | 22 75 24 24 26 | 23 31 40 40 27 | 31 32 58 58 28 | 32 33 30 30 29 | 32 34 43 43 30 | 32 35 86 86 31 | 35 41 15 15 32 | 43 44 78 78 33 | 44 45 12 12 34 | 44 46 13 13 35 | 46 47 33 33 36 | 47 48 10 10 37 | 47 49 9 9 38 | 49 50 8 8 39 | 49 51 10 10 40 | 50 52 2 2 41 | 52 54 4 4 42 | 55 56 6 6 43 | 42 57 14 14 44 | 57 58 78 78 45 | 58 59 25 25 46 | 11 59 78 78 47 | 58 60 33 33 48 | 60 61 39 39 49 | 60 62 23 23 50 | 62 63 30 30 51 | 63 65 21 21 52 | 62 66 16 16 53 | 66 68 45 45 54 | 58 69 32 32 55 | 59 69 11 11 56 | 4 69 75 75 57 | 23 75 17 17 58 | 20 76 8 8 59 | 21 51 2 2 60 | 44 59 28 28 61 | 5 6 8 0 62 | 5 7 6 0 63 | 7 8 18 0 64 | 8 9 26 0 65 | 10 11 12 0 66 | 14 15 7 0 67 | 13 77 12 0 68 | 18 20 21 0 69 | 22 24 4 0 70 | 24 25 27 0 71 | 25 75 16 0 72 | 25 26 17 0 73 | 23 26 12 0 74 | 26 27 15 0 75 | 26 28 18 0 76 | 28 29 16 0 77 | 25 29 27 0 78 | 30 31 14 0 79 | 33 36 27 0 80 | 33 37 75 0 81 | 37 38 8 0 82 | 37 39 21 0 83 | 39 40 5 0 84 | 35 39 7 0 85 | 40 41 9 0 86 | 41 42 18 0 87 | 42 43 11 0 88 | 45 46 12 0 89 | 11 48 79 0 90 | 51 53 4 0 91 | 52 53 4 0 92 | 24 53 24 0 93 | 19 50 30 0 94 | 42 56 26 0 95 | 63 64 9 0 96 | 70 71 56 0 97 | 71 72 6 0 98 | 71 73 5 0 99 | 72 73 5 0 100 | 73 74 25 0 101 | 18 72 92 0 102 | 56 67 36 0 103 | 12 76 7 0 104 | 15 77 13 0 105 | 76 77 35 0 106 | 60 67 29 0 107 | 62 67 31 0 108 | END -------------------------------------------------------------------------------- /CARP/CARP_samples/eglese/egl-e1-C.dat: -------------------------------------------------------------------------------- 1 | NAME : egl-e1-C 2 | VERTICES : 77 3 | DEPOT : 1 4 | REQUIRED EDGES : 51 5 | NON-REQUIRED EDGES : 47 6 | VEHICLES : 10 7 | CAPACITY : 160 8 | TOTAL COST OF REQUIRED EDGES : 1468 9 | NODES COST DEMAND 10 | 1 2 32 32 11 | 2 3 14 14 12 | 2 4 17 17 13 | 4 5 56 56 14 | 9 10 20 20 15 | 11 12 32 32 16 | 12 16 29 29 17 | 13 16 13 13 18 | 13 14 7 7 19 | 15 17 26 26 20 | 15 18 38 38 21 | 18 19 41 41 22 | 19 20 32 32 23 | 19 21 38 38 24 | 21 22 17 17 25 | 22 75 24 24 26 | 23 31 40 40 27 | 31 32 58 58 28 | 32 33 30 30 29 | 32 34 43 43 30 | 32 35 86 86 31 | 35 41 15 15 32 | 43 44 78 78 33 | 44 45 12 12 34 | 44 46 13 13 35 | 46 47 33 33 36 | 47 48 10 10 37 | 47 49 9 9 38 | 49 50 8 8 39 | 49 51 10 10 40 | 50 52 2 2 41 | 52 54 4 4 42 | 55 56 6 6 43 | 42 57 14 14 44 | 57 58 78 78 45 | 58 59 25 25 46 | 11 59 78 78 47 | 58 60 33 33 48 | 60 61 39 39 49 | 60 62 23 23 50 | 62 63 30 30 51 | 63 65 21 21 52 | 62 66 16 16 53 | 66 68 45 45 54 | 58 69 32 32 55 | 59 69 11 11 56 | 4 69 75 75 57 | 23 75 17 17 58 | 20 76 8 8 59 | 21 51 2 2 60 | 44 59 28 28 61 | 5 6 8 0 62 | 5 7 6 0 63 | 7 8 18 0 64 | 8 9 26 0 65 | 10 11 12 0 66 | 14 15 7 0 67 | 13 77 12 0 68 | 18 20 21 0 69 | 22 24 4 0 70 | 24 25 27 0 71 | 25 75 16 0 72 | 25 26 17 0 73 | 23 26 12 0 74 | 26 27 15 0 75 | 26 28 18 0 76 | 28 29 16 0 77 | 25 29 27 0 78 | 30 31 14 0 79 | 33 36 27 0 80 | 33 37 75 0 81 | 37 38 8 0 82 | 37 39 21 0 83 | 39 40 5 0 84 | 35 39 7 0 85 | 40 41 9 0 86 | 41 42 18 0 87 | 42 43 11 0 88 | 45 46 12 0 89 | 11 48 79 0 90 | 51 53 4 0 91 | 52 53 4 0 92 | 24 53 24 0 93 | 19 50 30 0 94 | 42 56 26 0 95 | 63 64 9 0 96 | 70 71 56 0 97 | 71 72 6 0 98 | 71 73 5 0 99 | 72 73 5 0 100 | 73 74 25 0 101 | 18 72 92 0 102 | 56 67 36 0 103 | 12 76 7 0 104 | 15 77 13 0 105 | 76 77 35 0 106 | 60 67 29 0 107 | 62 67 31 0 108 | END -------------------------------------------------------------------------------- /CARP/CARP_samples/eglese/egl-e2-A.dat: -------------------------------------------------------------------------------- 1 | NAME : egl-e2-7 2 | VERTICES : 77 3 | DEPOT : 1 4 | REQUIRED EDGES : 72 5 | NON-REQUIRED EDGES : 26 6 | VEHICLES : 7 7 | CAPACITY : 280 8 | TOTAL COST OF REQUIRED EDGES : 1879 9 | NODES COST DEMAND 10 | 1 2 32 32 11 | 2 3 14 14 12 | 2 4 17 17 13 | 4 5 56 56 14 | 5 7 6 6 15 | 7 8 18 18 16 | 8 9 26 26 17 | 9 10 20 20 18 | 11 12 32 32 19 | 12 16 29 29 20 | 13 16 13 13 21 | 13 14 7 7 22 | 15 17 26 26 23 | 15 18 38 38 24 | 18 19 41 41 25 | 19 20 32 32 26 | 19 21 38 38 27 | 21 22 17 17 28 | 22 75 24 24 29 | 22 24 4 4 30 | 24 25 27 27 31 | 25 75 16 16 32 | 25 26 17 17 33 | 23 26 12 12 34 | 26 27 15 15 35 | 26 28 18 18 36 | 25 29 27 27 37 | 23 31 40 40 38 | 31 32 58 58 39 | 32 33 30 30 40 | 32 34 43 43 41 | 32 35 86 86 42 | 33 36 27 27 43 | 35 41 15 15 44 | 43 44 78 78 45 | 44 45 12 12 46 | 44 46 13 13 47 | 46 47 33 33 48 | 47 48 10 10 49 | 47 49 9 9 50 | 49 50 8 8 51 | 49 51 10 10 52 | 50 52 2 2 53 | 51 53 4 4 54 | 52 53 4 4 55 | 52 54 4 4 56 | 24 53 24 24 57 | 19 50 30 30 58 | 55 56 6 6 59 | 42 57 14 14 60 | 57 58 78 78 61 | 58 59 25 25 62 | 11 59 78 78 63 | 58 60 33 33 64 | 60 61 39 39 65 | 60 62 23 23 66 | 62 63 30 30 67 | 63 64 9 9 68 | 63 65 21 21 69 | 62 66 16 16 70 | 66 68 45 45 71 | 58 69 32 32 72 | 59 69 11 11 73 | 4 69 75 75 74 | 71 73 5 5 75 | 72 73 5 5 76 | 73 74 25 25 77 | 18 72 92 92 78 | 23 75 17 17 79 | 20 76 8 8 80 | 21 51 2 2 81 | 44 59 28 28 82 | 5 6 8 0 83 | 10 11 12 0 84 | 14 15 7 0 85 | 13 77 12 0 86 | 18 20 21 0 87 | 28 29 16 0 88 | 30 31 14 0 89 | 33 37 75 0 90 | 37 38 8 0 91 | 37 39 21 0 92 | 39 40 5 0 93 | 35 39 7 0 94 | 40 41 9 0 95 | 41 42 18 0 96 | 42 43 11 0 97 | 45 46 12 0 98 | 11 48 79 0 99 | 42 56 26 0 100 | 70 71 56 0 101 | 71 72 6 0 102 | 56 67 36 0 103 | 12 76 7 0 104 | 15 77 13 0 105 | 76 77 35 0 106 | 60 67 29 0 107 | 62 67 31 0 108 | END -------------------------------------------------------------------------------- /CARP/CARP_samples/eglese/egl-e2-B.dat: -------------------------------------------------------------------------------- 1 | NAME : egl-e2-B 2 | VERTICES : 77 3 | DEPOT : 1 4 | REQUIRED EDGES : 72 5 | NON-REQUIRED EDGES : 26 6 | VEHICLES : 10 7 | CAPACITY : 200 8 | TOTAL COST OF REQUIRED EDGES : 1879 9 | NODES COST DEMAND 10 | 1 2 32 32 11 | 2 3 14 14 12 | 2 4 17 17 13 | 4 5 56 56 14 | 5 7 6 6 15 | 7 8 18 18 16 | 8 9 26 26 17 | 9 10 20 20 18 | 11 12 32 32 19 | 12 16 29 29 20 | 13 16 13 13 21 | 13 14 7 7 22 | 15 17 26 26 23 | 15 18 38 38 24 | 18 19 41 41 25 | 19 20 32 32 26 | 19 21 38 38 27 | 21 22 17 17 28 | 22 75 24 24 29 | 22 24 4 4 30 | 24 25 27 27 31 | 25 75 16 16 32 | 25 26 17 17 33 | 23 26 12 12 34 | 26 27 15 15 35 | 26 28 18 18 36 | 25 29 27 27 37 | 23 31 40 40 38 | 31 32 58 58 39 | 32 33 30 30 40 | 32 34 43 43 41 | 32 35 86 86 42 | 33 36 27 27 43 | 35 41 15 15 44 | 43 44 78 78 45 | 44 45 12 12 46 | 44 46 13 13 47 | 46 47 33 33 48 | 47 48 10 10 49 | 47 49 9 9 50 | 49 50 8 8 51 | 49 51 10 10 52 | 50 52 2 2 53 | 51 53 4 4 54 | 52 53 4 4 55 | 52 54 4 4 56 | 24 53 24 24 57 | 19 50 30 30 58 | 55 56 6 6 59 | 42 57 14 14 60 | 57 58 78 78 61 | 58 59 25 25 62 | 11 59 78 78 63 | 58 60 33 33 64 | 60 61 39 39 65 | 60 62 23 23 66 | 62 63 30 30 67 | 63 64 9 9 68 | 63 65 21 21 69 | 62 66 16 16 70 | 66 68 45 45 71 | 58 69 32 32 72 | 59 69 11 11 73 | 4 69 75 75 74 | 71 73 5 5 75 | 72 73 5 5 76 | 73 74 25 25 77 | 18 72 92 92 78 | 23 75 17 17 79 | 20 76 8 8 80 | 21 51 2 2 81 | 44 59 28 28 82 | 5 6 8 0 83 | 10 11 12 0 84 | 14 15 7 0 85 | 13 77 12 0 86 | 18 20 21 0 87 | 28 29 16 0 88 | 30 31 14 0 89 | 33 37 75 0 90 | 37 38 8 0 91 | 37 39 21 0 92 | 39 40 5 0 93 | 35 39 7 0 94 | 40 41 9 0 95 | 41 42 18 0 96 | 42 43 11 0 97 | 45 46 12 0 98 | 11 48 79 0 99 | 42 56 26 0 100 | 70 71 56 0 101 | 71 72 6 0 102 | 56 67 36 0 103 | 12 76 7 0 104 | 15 77 13 0 105 | 76 77 35 0 106 | 60 67 29 0 107 | 62 67 31 0 108 | END -------------------------------------------------------------------------------- /CARP/CARP_samples/eglese/egl-e2-C.dat: -------------------------------------------------------------------------------- 1 | NAME : egl-e2-C 2 | VERTICES : 77 3 | DEPOT : 1 4 | REQUIRED EDGES : 72 5 | NON-REQUIRED EDGES : 26 6 | VEHICLES : 14 7 | CAPACITY : 140 8 | TOTAL COST OF REQUIRED EDGES : 1879 9 | NODES COST DEMAND 10 | 1 2 32 32 11 | 2 3 14 14 12 | 2 4 17 17 13 | 4 5 56 56 14 | 5 7 6 6 15 | 7 8 18 18 16 | 8 9 26 26 17 | 9 10 20 20 18 | 11 12 32 32 19 | 12 16 29 29 20 | 13 16 13 13 21 | 13 14 7 7 22 | 15 17 26 26 23 | 15 18 38 38 24 | 18 19 41 41 25 | 19 20 32 32 26 | 19 21 38 38 27 | 21 22 17 17 28 | 22 75 24 24 29 | 22 24 4 4 30 | 24 25 27 27 31 | 25 75 16 16 32 | 25 26 17 17 33 | 23 26 12 12 34 | 26 27 15 15 35 | 26 28 18 18 36 | 25 29 27 27 37 | 23 31 40 40 38 | 31 32 58 58 39 | 32 33 30 30 40 | 32 34 43 43 41 | 32 35 86 86 42 | 33 36 27 27 43 | 35 41 15 15 44 | 43 44 78 78 45 | 44 45 12 12 46 | 44 46 13 13 47 | 46 47 33 33 48 | 47 48 10 10 49 | 47 49 9 9 50 | 49 50 8 8 51 | 49 51 10 10 52 | 50 52 2 2 53 | 51 53 4 4 54 | 52 53 4 4 55 | 52 54 4 4 56 | 24 53 24 24 57 | 19 50 30 30 58 | 55 56 6 6 59 | 42 57 14 14 60 | 57 58 78 78 61 | 58 59 25 25 62 | 11 59 78 78 63 | 58 60 33 33 64 | 60 61 39 39 65 | 60 62 23 23 66 | 62 63 30 30 67 | 63 64 9 9 68 | 63 65 21 21 69 | 62 66 16 16 70 | 66 68 45 45 71 | 58 69 32 32 72 | 59 69 11 11 73 | 4 69 75 75 74 | 71 73 5 5 75 | 72 73 5 5 76 | 73 74 25 25 77 | 18 72 92 92 78 | 23 75 17 17 79 | 20 76 8 8 80 | 21 51 2 2 81 | 44 59 28 28 82 | 5 6 8 0 83 | 10 11 12 0 84 | 14 15 7 0 85 | 13 77 12 0 86 | 18 20 21 0 87 | 28 29 16 0 88 | 30 31 14 0 89 | 33 37 75 0 90 | 37 38 8 0 91 | 37 39 21 0 92 | 39 40 5 0 93 | 35 39 7 0 94 | 40 41 9 0 95 | 41 42 18 0 96 | 42 43 11 0 97 | 45 46 12 0 98 | 11 48 79 0 99 | 42 56 26 0 100 | 70 71 56 0 101 | 71 72 6 0 102 | 56 67 36 0 103 | 12 76 7 0 104 | 15 77 13 0 105 | 76 77 35 0 106 | 60 67 29 0 107 | 62 67 31 0 108 | END -------------------------------------------------------------------------------- /CARP/CARP_samples/eglese/egl-e3-A.dat: -------------------------------------------------------------------------------- 1 | NAME : egl-e3-A 2 | VERTICES : 77 3 | DEPOT : 1 4 | REQUIRED EDGES : 87 5 | NON-REQUIRED EDGES : 11 6 | VEHICLES : 8 7 | CAPACITY : 280 8 | TOTAL COST OF REQUIRED EDGES : 2188 9 | NODES COST DEMAND 10 | 1 2 32 32 11 | 2 3 14 14 12 | 2 4 17 17 13 | 4 5 56 56 14 | 5 6 8 8 15 | 5 7 6 6 16 | 7 8 18 18 17 | 8 9 26 26 18 | 9 10 20 20 19 | 11 12 32 32 20 | 12 16 29 29 21 | 13 16 13 13 22 | 13 14 7 7 23 | 13 77 12 12 24 | 15 17 26 26 25 | 15 18 38 38 26 | 18 19 41 41 27 | 19 20 32 32 28 | 18 20 21 21 29 | 19 21 38 38 30 | 21 22 17 17 31 | 22 75 24 24 32 | 22 24 4 4 33 | 24 25 27 27 34 | 25 75 16 16 35 | 25 26 17 17 36 | 23 26 12 12 37 | 26 27 15 15 38 | 26 28 18 18 39 | 28 29 16 16 40 | 25 29 27 27 41 | 30 31 14 14 42 | 23 31 40 40 43 | 31 32 58 58 44 | 32 33 30 30 45 | 32 34 43 43 46 | 32 35 86 86 47 | 33 36 27 27 48 | 33 37 75 75 49 | 37 38 8 8 50 | 37 39 21 21 51 | 39 40 5 5 52 | 35 39 7 7 53 | 35 41 15 15 54 | 43 44 78 78 55 | 44 45 12 12 56 | 45 46 12 12 57 | 44 46 13 13 58 | 46 47 33 33 59 | 47 48 10 10 60 | 47 49 9 9 61 | 49 50 8 8 62 | 49 51 10 10 63 | 50 52 2 2 64 | 51 53 4 4 65 | 52 53 4 4 66 | 52 54 4 4 67 | 24 53 24 24 68 | 19 50 30 30 69 | 55 56 6 6 70 | 42 57 14 14 71 | 57 58 78 78 72 | 58 59 25 25 73 | 11 59 78 78 74 | 58 60 33 33 75 | 60 61 39 39 76 | 60 62 23 23 77 | 62 63 30 30 78 | 63 64 9 9 79 | 63 65 21 21 80 | 62 66 16 16 81 | 66 68 45 45 82 | 58 69 32 32 83 | 59 69 11 11 84 | 4 69 75 75 85 | 70 71 56 56 86 | 71 72 6 6 87 | 71 73 5 5 88 | 72 73 5 5 89 | 73 74 25 25 90 | 18 72 92 92 91 | 23 75 17 17 92 | 20 76 8 8 93 | 15 77 13 13 94 | 76 77 35 35 95 | 21 51 2 2 96 | 44 59 28 28 97 | 10 11 12 0 98 | 14 15 7 0 99 | 40 41 9 0 100 | 41 42 18 0 101 | 42 43 11 0 102 | 11 48 79 0 103 | 42 56 26 0 104 | 56 67 36 0 105 | 12 76 7 0 106 | 60 67 29 0 107 | 62 67 31 0 108 | END -------------------------------------------------------------------------------- /CARP/CARP_samples/eglese/egl-e3-B.dat: -------------------------------------------------------------------------------- 1 | NAME : egl-e3-B 2 | VERTICES : 77 3 | DEPOT : 1 4 | REQUIRED EDGES : 87 5 | NON-REQUIRED EDGES : 11 6 | VEHICLES : 12 7 | CAPACITY : 190 8 | TOTAL COST OF REQUIRED EDGES : 2188 9 | NODES COST DEMAND 10 | 1 2 32 32 11 | 2 3 14 14 12 | 2 4 17 17 13 | 4 5 56 56 14 | 5 6 8 8 15 | 5 7 6 6 16 | 7 8 18 18 17 | 8 9 26 26 18 | 9 10 20 20 19 | 11 12 32 32 20 | 12 16 29 29 21 | 13 16 13 13 22 | 13 14 7 7 23 | 13 77 12 12 24 | 15 17 26 26 25 | 15 18 38 38 26 | 18 19 41 41 27 | 19 20 32 32 28 | 18 20 21 21 29 | 19 21 38 38 30 | 21 22 17 17 31 | 22 75 24 24 32 | 22 24 4 4 33 | 24 25 27 27 34 | 25 75 16 16 35 | 25 26 17 17 36 | 23 26 12 12 37 | 26 27 15 15 38 | 26 28 18 18 39 | 28 29 16 16 40 | 25 29 27 27 41 | 30 31 14 14 42 | 23 31 40 40 43 | 31 32 58 58 44 | 32 33 30 30 45 | 32 34 43 43 46 | 32 35 86 86 47 | 33 36 27 27 48 | 33 37 75 75 49 | 37 38 8 8 50 | 37 39 21 21 51 | 39 40 5 5 52 | 35 39 7 7 53 | 35 41 15 15 54 | 43 44 78 78 55 | 44 45 12 12 56 | 45 46 12 12 57 | 44 46 13 13 58 | 46 47 33 33 59 | 47 48 10 10 60 | 47 49 9 9 61 | 49 50 8 8 62 | 49 51 10 10 63 | 50 52 2 2 64 | 51 53 4 4 65 | 52 53 4 4 66 | 52 54 4 4 67 | 24 53 24 24 68 | 19 50 30 30 69 | 55 56 6 6 70 | 42 57 14 14 71 | 57 58 78 78 72 | 58 59 25 25 73 | 11 59 78 78 74 | 58 60 33 33 75 | 60 61 39 39 76 | 60 62 23 23 77 | 62 63 30 30 78 | 63 64 9 9 79 | 63 65 21 21 80 | 62 66 16 16 81 | 66 68 45 45 82 | 58 69 32 32 83 | 59 69 11 11 84 | 4 69 75 75 85 | 70 71 56 56 86 | 71 72 6 6 87 | 71 73 5 5 88 | 72 73 5 5 89 | 73 74 25 25 90 | 18 72 92 92 91 | 23 75 17 17 92 | 20 76 8 8 93 | 15 77 13 13 94 | 76 77 35 35 95 | 21 51 2 2 96 | 44 59 28 28 97 | 10 11 12 0 98 | 14 15 7 0 99 | 40 41 9 0 100 | 41 42 18 0 101 | 42 43 11 0 102 | 11 48 79 0 103 | 42 56 26 0 104 | 56 67 36 0 105 | 12 76 7 0 106 | 60 67 29 0 107 | 62 67 31 0 108 | END -------------------------------------------------------------------------------- /CARP/CARP_samples/eglese/egl-e3-C.dat: -------------------------------------------------------------------------------- 1 | NAME : egl-e3-C 2 | VERTICES : 77 3 | DEPOT : 1 4 | REQUIRED EDGES : 87 5 | NON-REQUIRED EDGES : 11 6 | VEHICLES : 17 7 | CAPACITY : 135 8 | TOTAL COST OF REQUIRED EDGES : 2188 9 | NODES COST DEMAND 10 | 1 2 32 32 11 | 2 3 14 14 12 | 2 4 17 17 13 | 4 5 56 56 14 | 5 6 8 8 15 | 5 7 6 6 16 | 7 8 18 18 17 | 8 9 26 26 18 | 9 10 20 20 19 | 11 12 32 32 20 | 12 16 29 29 21 | 13 16 13 13 22 | 13 14 7 7 23 | 13 77 12 12 24 | 15 17 26 26 25 | 15 18 38 38 26 | 18 19 41 41 27 | 19 20 32 32 28 | 18 20 21 21 29 | 19 21 38 38 30 | 21 22 17 17 31 | 22 75 24 24 32 | 22 24 4 4 33 | 24 25 27 27 34 | 25 75 16 16 35 | 25 26 17 17 36 | 23 26 12 12 37 | 26 27 15 15 38 | 26 28 18 18 39 | 28 29 16 16 40 | 25 29 27 27 41 | 30 31 14 14 42 | 23 31 40 40 43 | 31 32 58 58 44 | 32 33 30 30 45 | 32 34 43 43 46 | 32 35 86 86 47 | 33 36 27 27 48 | 33 37 75 75 49 | 37 38 8 8 50 | 37 39 21 21 51 | 39 40 5 5 52 | 35 39 7 7 53 | 35 41 15 15 54 | 43 44 78 78 55 | 44 45 12 12 56 | 45 46 12 12 57 | 44 46 13 13 58 | 46 47 33 33 59 | 47 48 10 10 60 | 47 49 9 9 61 | 49 50 8 8 62 | 49 51 10 10 63 | 50 52 2 2 64 | 51 53 4 4 65 | 52 53 4 4 66 | 52 54 4 4 67 | 24 53 24 24 68 | 19 50 30 30 69 | 55 56 6 6 70 | 42 57 14 14 71 | 57 58 78 78 72 | 58 59 25 25 73 | 11 59 78 78 74 | 58 60 33 33 75 | 60 61 39 39 76 | 60 62 23 23 77 | 62 63 30 30 78 | 63 64 9 9 79 | 63 65 21 21 80 | 62 66 16 16 81 | 66 68 45 45 82 | 58 69 32 32 83 | 59 69 11 11 84 | 4 69 75 75 85 | 70 71 56 56 86 | 71 72 6 6 87 | 71 73 5 5 88 | 72 73 5 5 89 | 73 74 25 25 90 | 18 72 92 92 91 | 23 75 17 17 92 | 20 76 8 8 93 | 15 77 13 13 94 | 76 77 35 35 95 | 21 51 2 2 96 | 44 59 28 28 97 | 10 11 12 0 98 | 14 15 7 0 99 | 40 41 9 0 100 | 41 42 18 0 101 | 42 43 11 0 102 | 11 48 79 0 103 | 42 56 26 0 104 | 56 67 36 0 105 | 12 76 7 0 106 | 60 67 29 0 107 | 62 67 31 0 108 | END -------------------------------------------------------------------------------- /CARP/CARP_samples/eglese/egl-e4-A.dat: -------------------------------------------------------------------------------- 1 | NAME : egl-e4-A 2 | VERTICES : 77 3 | DEPOT : 1 4 | REQUIRED EDGES : 98 5 | NON-REQUIRED EDGES : 0 6 | VEHICLES : 9 7 | CAPACITY : 280 8 | TOTAL COST OF REQUIRED EDGES : 2453 9 | NODES COST DEMAND 10 | 1 2 32 32 11 | 2 3 14 14 12 | 2 4 17 17 13 | 4 5 56 56 14 | 5 6 8 8 15 | 5 7 6 6 16 | 7 8 18 18 17 | 8 9 26 26 18 | 9 10 20 20 19 | 10 11 12 12 20 | 11 12 32 32 21 | 12 16 29 29 22 | 13 16 13 13 23 | 13 14 7 7 24 | 14 15 7 7 25 | 13 77 12 12 26 | 15 17 26 26 27 | 15 18 38 38 28 | 18 19 41 41 29 | 19 20 32 32 30 | 18 20 21 21 31 | 19 21 38 38 32 | 21 22 17 17 33 | 22 75 24 24 34 | 22 24 4 4 35 | 24 25 27 27 36 | 25 75 16 16 37 | 25 26 17 17 38 | 23 26 12 12 39 | 26 27 15 15 40 | 26 28 18 18 41 | 28 29 16 16 42 | 25 29 27 27 43 | 30 31 14 14 44 | 23 31 40 40 45 | 31 32 58 58 46 | 32 33 30 30 47 | 32 34 43 43 48 | 32 35 86 86 49 | 33 36 27 27 50 | 33 37 75 75 51 | 37 38 8 8 52 | 37 39 21 21 53 | 39 40 5 5 54 | 35 39 7 7 55 | 35 41 15 15 56 | 40 41 9 9 57 | 41 42 18 18 58 | 42 43 11 11 59 | 43 44 78 78 60 | 44 45 12 12 61 | 45 46 12 12 62 | 44 46 13 13 63 | 46 47 33 33 64 | 47 48 10 10 65 | 11 48 79 79 66 | 47 49 9 9 67 | 49 50 8 8 68 | 49 51 10 10 69 | 50 52 2 2 70 | 51 53 4 4 71 | 52 53 4 4 72 | 52 54 4 4 73 | 24 53 24 24 74 | 19 50 30 30 75 | 55 56 6 6 76 | 42 56 26 26 77 | 42 57 14 14 78 | 57 58 78 78 79 | 58 59 25 25 80 | 11 59 78 78 81 | 58 60 33 33 82 | 60 61 39 39 83 | 60 62 23 23 84 | 62 63 30 30 85 | 63 64 9 9 86 | 63 65 21 21 87 | 62 66 16 16 88 | 66 68 45 45 89 | 58 69 32 32 90 | 59 69 11 11 91 | 4 69 75 75 92 | 70 71 56 56 93 | 71 72 6 6 94 | 71 73 5 5 95 | 72 73 5 5 96 | 73 74 25 25 97 | 18 72 92 92 98 | 56 67 36 36 99 | 23 75 17 17 100 | 12 76 7 7 101 | 20 76 8 8 102 | 15 77 13 13 103 | 76 77 35 35 104 | 21 51 2 2 105 | 44 59 28 28 106 | 60 67 29 29 107 | 62 67 31 31 108 | END -------------------------------------------------------------------------------- /CARP/CARP_samples/eglese/egl-e4-B.dat: -------------------------------------------------------------------------------- 1 | NAME : egl-e4-B 2 | VERTICES : 77 3 | DEPOT : 1 4 | REQUIRED EDGES : 98 5 | NON-REQUIRED EDGES : 0 6 | VEHICLES : 14 7 | CAPACITY : 180 8 | TOTAL COST OF REQUIRED EDGES : 2453 9 | NODES COST DEMAND 10 | 1 2 32 32 11 | 2 3 14 14 12 | 2 4 17 17 13 | 4 5 56 56 14 | 5 6 8 8 15 | 5 7 6 6 16 | 7 8 18 18 17 | 8 9 26 26 18 | 9 10 20 20 19 | 10 11 12 12 20 | 11 12 32 32 21 | 12 16 29 29 22 | 13 16 13 13 23 | 13 14 7 7 24 | 14 15 7 7 25 | 13 77 12 12 26 | 15 17 26 26 27 | 15 18 38 38 28 | 18 19 41 41 29 | 19 20 32 32 30 | 18 20 21 21 31 | 19 21 38 38 32 | 21 22 17 17 33 | 22 75 24 24 34 | 22 24 4 4 35 | 24 25 27 27 36 | 25 75 16 16 37 | 25 26 17 17 38 | 23 26 12 12 39 | 26 27 15 15 40 | 26 28 18 18 41 | 28 29 16 16 42 | 25 29 27 27 43 | 30 31 14 14 44 | 23 31 40 40 45 | 31 32 58 58 46 | 32 33 30 30 47 | 32 34 43 43 48 | 32 35 86 86 49 | 33 36 27 27 50 | 33 37 75 75 51 | 37 38 8 8 52 | 37 39 21 21 53 | 39 40 5 5 54 | 35 39 7 7 55 | 35 41 15 15 56 | 40 41 9 9 57 | 41 42 18 18 58 | 42 43 11 11 59 | 43 44 78 78 60 | 44 45 12 12 61 | 45 46 12 12 62 | 44 46 13 13 63 | 46 47 33 33 64 | 47 48 10 10 65 | 11 48 79 79 66 | 47 49 9 9 67 | 49 50 8 8 68 | 49 51 10 10 69 | 50 52 2 2 70 | 51 53 4 4 71 | 52 53 4 4 72 | 52 54 4 4 73 | 24 53 24 24 74 | 19 50 30 30 75 | 55 56 6 6 76 | 42 56 26 26 77 | 42 57 14 14 78 | 57 58 78 78 79 | 58 59 25 25 80 | 11 59 78 78 81 | 58 60 33 33 82 | 60 61 39 39 83 | 60 62 23 23 84 | 62 63 30 30 85 | 63 64 9 9 86 | 63 65 21 21 87 | 62 66 16 16 88 | 66 68 45 45 89 | 58 69 32 32 90 | 59 69 11 11 91 | 4 69 75 75 92 | 70 71 56 56 93 | 71 72 6 6 94 | 71 73 5 5 95 | 72 73 5 5 96 | 73 74 25 25 97 | 18 72 92 92 98 | 56 67 36 36 99 | 23 75 17 17 100 | 12 76 7 7 101 | 20 76 8 8 102 | 15 77 13 13 103 | 76 77 35 35 104 | 21 51 2 2 105 | 44 59 28 28 106 | 60 67 29 29 107 | 62 67 31 31 108 | END -------------------------------------------------------------------------------- /CARP/CARP_samples/eglese/egl-e4-C.dat: -------------------------------------------------------------------------------- 1 | NAME : egl-e4-C 2 | VERTICES : 77 3 | DEPOT : 1 4 | REQUIRED EDGES : 98 5 | NON-REQUIRED EDGES : 0 6 | VEHICLES : 19 7 | CAPACITY : 130 8 | TOTAL COST OF REQUIRED EDGES : 2453 9 | NODES COST DEMAND 10 | 1 2 32 32 11 | 2 3 14 14 12 | 2 4 17 17 13 | 4 5 56 56 14 | 5 6 8 8 15 | 5 7 6 6 16 | 7 8 18 18 17 | 8 9 26 26 18 | 9 10 20 20 19 | 10 11 12 12 20 | 11 12 32 32 21 | 12 16 29 29 22 | 13 16 13 13 23 | 13 14 7 7 24 | 14 15 7 7 25 | 13 77 12 12 26 | 15 17 26 26 27 | 15 18 38 38 28 | 18 19 41 41 29 | 19 20 32 32 30 | 18 20 21 21 31 | 19 21 38 38 32 | 21 22 17 17 33 | 22 75 24 24 34 | 22 24 4 4 35 | 24 25 27 27 36 | 25 75 16 16 37 | 25 26 17 17 38 | 23 26 12 12 39 | 26 27 15 15 40 | 26 28 18 18 41 | 28 29 16 16 42 | 25 29 27 27 43 | 30 31 14 14 44 | 23 31 40 40 45 | 31 32 58 58 46 | 32 33 30 30 47 | 32 34 43 43 48 | 32 35 86 86 49 | 33 36 27 27 50 | 33 37 75 75 51 | 37 38 8 8 52 | 37 39 21 21 53 | 39 40 5 5 54 | 35 39 7 7 55 | 35 41 15 15 56 | 40 41 9 9 57 | 41 42 18 18 58 | 42 43 11 11 59 | 43 44 78 78 60 | 44 45 12 12 61 | 45 46 12 12 62 | 44 46 13 13 63 | 46 47 33 33 64 | 47 48 10 10 65 | 11 48 79 79 66 | 47 49 9 9 67 | 49 50 8 8 68 | 49 51 10 10 69 | 50 52 2 2 70 | 51 53 4 4 71 | 52 53 4 4 72 | 52 54 4 4 73 | 24 53 24 24 74 | 19 50 30 30 75 | 55 56 6 6 76 | 42 56 26 26 77 | 42 57 14 14 78 | 57 58 78 78 79 | 58 59 25 25 80 | 11 59 78 78 81 | 58 60 33 33 82 | 60 61 39 39 83 | 60 62 23 23 84 | 62 63 30 30 85 | 63 64 9 9 86 | 63 65 21 21 87 | 62 66 16 16 88 | 66 68 45 45 89 | 58 69 32 32 90 | 59 69 11 11 91 | 4 69 75 75 92 | 70 71 56 56 93 | 71 72 6 6 94 | 71 73 5 5 95 | 72 73 5 5 96 | 73 74 25 25 97 | 18 72 92 92 98 | 56 67 36 36 99 | 23 75 17 17 100 | 12 76 7 7 101 | 20 76 8 8 102 | 15 77 13 13 103 | 76 77 35 35 104 | 21 51 2 2 105 | 44 59 28 28 106 | 60 67 29 29 107 | 62 67 31 31 108 | END -------------------------------------------------------------------------------- /CARP/CARP_demo.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # written by mark zeng 2018-11-14 3 | 4 | import multiprocessing as mp 5 | import time 6 | import sys 7 | import numpy as np 8 | 9 | 10 | class Worker(mp.Process): 11 | def __init__(self, inQ, outQ, random_seed): 12 | super(Worker, self).__init__(target=self.start) 13 | self.inQ = inQ 14 | self.outQ = outQ 15 | np.random.seed(random_seed) # 如果子进程的任务是有随机性的,一定要给每个子进程不同的随机数种子,否则就在重复相同的结果了 16 | 17 | def run(self): 18 | while True: 19 | task = self.inQ.get() # 取出任务, 如果队列为空, 这一步会阻塞直到队列有元素 20 | x, y = task # 解析任务 21 | sum, product = sum_and_product(x, y) # 执行任务 22 | self.outQ.put((sum, product)) # 返回结果 23 | 24 | 25 | def create_worker(num): 26 | ''' 27 | 创建子进程备用 28 | :param num: 多线程数量 29 | ''' 30 | for i in range(num): 31 | worker.append(Worker(mp.Queue(), mp.Queue(), np.random.randint(0, 10 ** 9))) 32 | worker[i].start() 33 | 34 | 35 | def finish_worker(): 36 | ''' 37 | 关闭所有子线程 38 | ''' 39 | for w in worker: 40 | w.terminate() 41 | 42 | 43 | def sum_and_product(x, y): 44 | ''' 45 | 计算两个数的和与积 46 | ''' 47 | return x + y, x * y 48 | 49 | 50 | def s_format(s): 51 | s_print = [] 52 | for p in s: 53 | s_print.append(0) 54 | s_print.extend(p) 55 | s_print.append(0) 56 | return s_print 57 | 58 | 59 | if __name__ == '__main__': 60 | ''' 61 | 从命令行读参数示例 62 | ''' 63 | time_limit = 60 64 | file_name = 'gdb10.dat' 65 | seed = 1 66 | 67 | if len(sys.argv) == 6: 68 | print("从命令行读参数示例") 69 | print(sys.argv) 70 | file_name = sys.argv[1] 71 | time_limit = int(sys.argv[3]) 72 | seed = int(sys.argv[5]) 73 | 74 | ''' 75 | 多进程示例 76 | ''' 77 | print("多进程示例") 78 | np.random.seed(seed) 79 | worker = [] 80 | worker_num = 8 81 | create_worker(worker_num) 82 | Task = [np.random.randint(0, 10, 2) for i in range(16)] # 生成16个随机任务, 每个任务是2个整数, 需要计算两数之和与积 83 | print('Task', Task) 84 | for i, t in enumerate(Task): 85 | worker[i % worker_num].inQ.put(t) # 根据编号取模, 将任务平均分配到子进程上 86 | result = [] 87 | for i, t in enumerate(Task): 88 | result.append(worker[i % worker_num].outQ.get()) # 用同样的规则取回结果, 如果任务尚未完成,此处会阻塞等待子进程完成任务 89 | print('result', result) 90 | finish_worker() 91 | 92 | ''' 93 | 输出示例 94 | ''' 95 | print("输出示例") 96 | s = [[(1, 2), (2, 3), (3, 8), (8, 12), (12, 10), (10, 9), (9, 1)], [(1, 4), (4, 2), (2, 7), (7, 4), (4, 6), (6, 11)], 97 | [(1, 10), (12, 11), (11, 4), (4, 3), (3, 9), (9, 8), (8, 1)], [(1, 11), (6, 5), (5, 2), (7, 5), (5, 1)]] 98 | cost = 275 99 | print("s", (",".join(str(d) for d in s_format(s))).replace(" ", "")) 100 | print("q", cost) 101 | -------------------------------------------------------------------------------- /CARP/Evaluation.py: -------------------------------------------------------------------------------- 1 | from CARP_solver import CARPHandler 2 | import os 3 | import time 4 | import xlwt 5 | from time import perf_counter 6 | 7 | 8 | def evaluate(data, population_size): 9 | cost = CARPHandler(data, 3, 1, population_size=population_size, test=False).run() 10 | result = (data.split('\\')[-1], cost) 11 | return result 12 | # time.sleep(1) 13 | # print('pid: {} \tdata: {}'.format(os.getpid(), data)) 14 | 15 | 16 | def write_to_excel(result): 17 | wbk = xlwt.Workbook() 18 | sheet = wbk.add_sheet('size tuning') 19 | sheet.write(0, 1, 'eglese') 20 | sheet.write(0, 2, 'bccm') 21 | sheet.write(0, 3, 'gdb') 22 | for x in range(len(result[1])): 23 | sheet.write(x + 1, 0, x * 5 + 10) 24 | 25 | for i in range(len(result)): 26 | for j in range(len(result[i])): 27 | sheet.write(j + 1, i + 1, result[i][j][1]) 28 | wbk.save('result.xls') 29 | 30 | 31 | if __name__ == '__main__': 32 | from os import listdir 33 | from os.path import isfile, join, abspath 34 | from multiprocessing import Pool 35 | 36 | start_time = perf_counter() 37 | 38 | data_paths = ['CARP_samples/eglese', 'CARP_samples/bccm', 'CARP_samples/gdb'] 39 | # data_paths = ['CARP_samples/eglese', 'CARP_samples/bccm'] 40 | p_sizes = range(40, 171, 5) 41 | 42 | size_result = [[], [], []] 43 | for i, data_path in enumerate(data_paths): 44 | for population_size in p_sizes: 45 | test_data = [abspath(join(data_path, f)) for f in listdir(data_path) if isfile(join(data_path, f))] 46 | 47 | pool = Pool(processes=16) 48 | result = [] 49 | for _ in range(20): 50 | for j in range(len(test_data)): 51 | r = pool.apply_async(evaluate, (test_data[j], population_size,)) 52 | result.append(r) 53 | 54 | pool.close() 55 | pool.join() 56 | 57 | size_result[i].append((population_size, sum(map(lambda x: x.get()[1], result)) / len(result))) 58 | 59 | print('data: {} \tsize: {} \tdone\n'.format(data_path, population_size)) 60 | 61 | result_file = open('result_{}.txt'.format(data_path.split('/')[1]), 'w') 62 | 63 | result_file.writelines(['size: {} \tavg_cost: {} \n'.format(r[0], r[1]) for r in size_result[i]]) 64 | 65 | print(size_result) 66 | print('time consumed: {} s'.format(perf_counter() - start_time)) 67 | write_to_excel(size_result) 68 | 69 | # if len(sys.argv) == 1: 70 | # sys.argv = ['CARP_solver.py', 'E:\Python\AICourse\CARP\CARP_samples\egl-e1-A.dat', '-t', '120', '-s', '1'] 71 | # 72 | # path, termination, seed = [sys.argv[i] for i in range(len(sys.argv)) if i % 2 == 1] 73 | # termination, seed = int(termination), int(seed) 74 | # # print(file, termination, seed) 75 | # handler = CARPHandler(path, int(termination), seed, test=True) 76 | # handler.run() 77 | # sys.exit(0) 78 | -------------------------------------------------------------------------------- /CARP/CARP_solver.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from time import perf_counter 3 | import random 4 | import os 5 | 6 | from CARP_algorithm import CARPAlgorithm 7 | from CARP_info import CARPInfo, get_cost 8 | 9 | 10 | class CARPHandler: 11 | def __init__(self, instance_path, termination, seed, population_size=100, test=False): 12 | self.info = CARPInfo(instance_path) 13 | self.termination = termination 14 | self.test = test 15 | self.path = instance_path 16 | self.population_size = population_size 17 | 18 | # print(self.info) 19 | 20 | random.seed(seed) 21 | np.random.seed(seed) 22 | 23 | @staticmethod 24 | def handle_output(solution): 25 | def s_format(s): 26 | s_print = [] 27 | for p in s: 28 | s_print.append(0) 29 | s_print.extend(p) 30 | s_print.append(0) 31 | return s_print 32 | 33 | routes = solution.routes 34 | print("s", (",".join(str(d) for d in s_format(routes))).replace(" ", "")) 35 | print("q", solution.total_cost) 36 | 37 | def run(self): 38 | avg_time = 0 39 | total_time = 0 40 | time_remain = self.termination - 2 41 | start_time = perf_counter() 42 | 43 | solver = CARPAlgorithm(self.info, self.population_size) 44 | time_remain -= perf_counter() - start_time 45 | 46 | iter_num = 0 47 | best = None # result dict 48 | while time_remain > 2 * avg_time: 49 | iter_start = perf_counter() 50 | best = solver.step() 51 | iter_end = perf_counter() 52 | 53 | iter_time = iter_end - iter_start 54 | iter_num += 1 55 | total_time += iter_time 56 | avg_time = 0.6 * avg_time + 0.4 * iter_time 57 | time_remain -= iter_time 58 | if self.test: 59 | print('pid: {} \tsample: {} \titer {} \t\tpopulation: {} \ttime: {:5.3} s \tavg: {:5.3} s \tremain: {:.3f} s \tcost: {}'.format( 60 | os.getpid(), 61 | self.path.split('\\')[-1], 62 | iter_num, 63 | len(solver.population), 64 | iter_time, 65 | avg_time, 66 | time_remain, 67 | best.total_cost) 68 | ) 69 | 70 | # self.handle_output(best) 71 | return int(best.total_cost) 72 | 73 | 74 | if __name__ == '__main__': 75 | import sys 76 | 77 | if len(sys.argv) == 1: 78 | sys.argv = ['CARP_solver.py', 'CARP_samples\egl-s1-A.dat', '-t', '10', '-s', '1'] 79 | 80 | path, termination, seed = [sys.argv[i] for i in range(len(sys.argv)) if i % 2 == 1] 81 | termination, seed = int(termination), int(seed) 82 | # print(file, termination, seed) 83 | handler = CARPHandler(path, int(termination), seed, test=True) 84 | handler.run() 85 | sys.exit(0) 86 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Course projects from SUSTech CS303 Artificial Intelligence, Fall 2018 2 | 3 | Textbook: [Artificial Intelligence: A Modern Approach](http://aima.cs.berkeley.edu/) 4 | 5 | 6 | 7 | *Attention! All the coding is on the basis of given API form course requirements, so the code here are platform specific, which means you cannot take it and apply for your own purpose.* 8 | 9 | **Focus on the details of algorithm implementation is more reasonable. These four .pdf reports may help understanding the algorithm and how it is implemented.** 10 | 11 | - [Gobang AI project report](https://github.com/zh-plus/AI-course-project/blob/master/Gobang.pdf) 12 | - [CARP project report](https://github.com/zh-plus/AI-course-project/blob/master/CARP.pdf) 13 | - [IMP project report](https://github.com/zh-plus/AI-course-project/blob/master/IMP.pdf) 14 | - [SVM optimization project report](https://github.com/zh-plus/AI-course-project/blob/master/SVM.pdf) 15 | 16 | 17 | 18 | ### Gobang AI project 19 | 20 | Algorithm: Minimax search + Minimax pruning algorithm 21 | 22 | *I'm sorry that the structure of this project is terrible. Because I make a mistake in estimating the workload of this project, which make me hurrying up to finish this project. I'll try to refactor it in my spare time.* 23 | 24 | Usage: No simple usage here, because the code is API specific. 25 | 26 | ###### ref: 27 | 28 | [*Searching for Solutions in Games and Artificial Intelligence*](http://www.dphu.org/uploads/attachements/books/books_3721_0.pdf) 29 | 30 | 31 | 32 | ### Capacitated Arc Routing Problem Project 33 | 34 | Algorithm: Heuristic search + Genetic Algorithm 35 | 36 | Usage: `$ python3 CARP_solver.py -t