示例#1
0
def test_solution():
    matrix = [[0, 1, 0, 0, 0, 1], [4, 0, 0, 3, 2, 0], [0, 0, 0, 0, 0, 0],
              [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
    expected = [0, 3, 2, 9, 14]

    result = solution(matrix)
    assert expected == result
def test_solution():
	f = open('samples', 'r')
	data = f.read()
	for i in data.split("<="):
		if i:
			actual, expected = map(lambda x : x.replace("\n", ""), i.split("=>"))
			print "Actual   : ", actual
			print "Expected : ", expected
			result = solution(actual)
			print "Solution : ", result		
			assert expected == result
示例#3
0
def test_solution():
    f = open('samples', 'r')
    data = f.read()
    for i in data.split("<="):
        if i:
            actual, expected = map(lambda x: x.replace("\n", ""),
                                   i.split("=>"))
            print "Actual   : ", actual
            print "Expected : ", expected
            result = solution(actual)
            print "Solution : ", result
            assert expected == result
示例#4
0
def read_test(line):
    input_lines = []
    output_lines = []
    if line == "<=":
        line = next_line()
        while line != "=>":
            input_lines.append(line)
            line = next_line()
        line = next_line()
        while line != "<=" and line != "END":
            output_lines.append(line)
            line = next_line()

        result = solution(input_lines[0], input_lines[1])
        assert output_lines[0] == result
        return line
示例#5
0
def init_population(number, N):  # number指AP数量,N是种群规模
    """
    初始化
    """
    solutions = []
    for k in range(N):
        pos = []
        for i in range(number):
            while True:
                row = random.randint(0, 49)
                col = random.randint(0, 119)
                if env.structure[row][col] and ([col, row] in pos):
                    continue
                break
            pos.append([col, row])
        solutions.append(solution(pos))
    return solutions
 def test_medium_range(self):
     A = [x + 1 for x in range(-1000, 1000)]
     self.assertEqual(solution(A), 999000000)
 def test_example1(self):
     A = [-3, 1, 2, -2, 5, 6]
     self.assertEqual(solution(A), 60, msg="example test")
示例#8
0
import solution

# Not confusing at all.
solution = solution.solution

# Test Time!
assert (solution('4') == 2)
assert (solution('15') == 5)

# My Tests
assert (solution('1') == 0)
assert (solution('2') == 1)
assert (solution('3') == 2)
assert (solution('4') == 2)

assert (solution('8') == 3)
assert (solution('16') == 4)
assert (solution('32') == 5)
assert (solution('32') == 5)

for k in range(1100):
    assert (solution(str(2**k)) == k)
    print(f'2**{k} == {2 ** k} PASSED')
示例#9
0
 def test_basic(self):
     """ Test from the task """
     self.assertEqual(solution(""""""), 0)
示例#10
0
from solution import *

P = np.array([[45, 25, 35, 20], [30, 20, 20, 25], [8, 16, 20, 24],
              [32, 16, 14, 12]])
C = np.array([[10, 8, 10, 12], [20, 7, 12, 10], [3, 5, 8, 10], [10, 8, 10, 5]])
a = np.array([[25, 20, 50, 50], [20, 12, 15, 45], [15, 10, 20, 40],
              [10, 40, 10, 25]])
A = [500, 200, 100, 1000]
b = [50, 80, 30, 45]
task = "pessimist"
alpha = 1

if __name__ == "__main__":
    solution(P=P, C=C, a=a, A=A, b=b, task=task, alpha=alpha)
示例#11
0
def test_1(m, f, result):
    assert solution(m, f) == result
示例#12
0
 while count <= 1000:
     #根据需要也可以改动
     average = 0
     population_After_selection = selection(init_sol)
     # print("1111", len(population_After_selection))
     population_After_cross = cross(population_After_selection, 0.8)
     # print("2222", len(population_After_cross))
     population_After_mutation = mutation(population_After_cross,
                                          0.001)
     pos = []
     init_sol = []
     for gene in population_After_mutation:
         pos = gene2pos(gene)
         if pos is None:
             continue
         solution_new = solution(pos)
         results_in_background_new = solution_new.fitness(env)
         solution_new.judgePopulation(results_in_background_new)
         init_sol.append(solution_new)
     quick_sort(init_sol, 0, len(init_sol) - 1)
     for k in range(0, 5):
         average = average + init_sol[len(init_sol) - 1 -
                                      k].fit_number
     average = average // 5
     optimal_solutions.append(average)
     if -0.01 < (optimal_solutions[count] -
                 optimal_solutions[count - 1]) / optimal_solutions[
                     count - 1] < 0.01 and count > 200:
         #print(optimal_solutions[count])
         #print(optimal_solutions[count - 1])
         break
示例#13
0
from solution import *
from code import P, C, a, A, b

y1, y2 = [], []
alpha_list = np.arange(0.49, 1, 0.01).tolist()
alpha_list.append(1)

task = "pessimist"
for alpha in alpha_list:
    y1.append(solution(P=P, C=C, a=a, A=A, b=b, task=task, alpha=alpha))

task = "optimist"
for alpha in alpha_list:
    y2.append(solution(P=P, C=C, a=a, A=A, b=b, task=task, alpha=alpha))

plt.plot(alpha_list, y1, label="pessimist")
plt.plot(alpha_list, y2, label="optimist")

plt.xlabel('alpha')
plt.ylabel('F')
plt.legend()
plt.show()