def test_compute_score():
    s1 = score.compute_score(cb, 6, 0)
    s2 = score.compute_score(cb, 6, 1)
    s3 = score.compute_score(cb, 2, 2)
    s4 = score.compute_score(cb, 3, 3)
    s5 = score.compute_score(cb, 4, 4)
    s6 = score.compute_score(cb, 6, 5)
    s7 = score.compute_score(cb, 6, 6)
    assert s1 == 651
    assert s2 == 1202
    assert s3 == 653
    assert s4 == 704
    assert s5 == 1503
    assert s6 == 9702
    assert s7 == 651
Example #2
0
def main():
    parser = argparse.ArgumentParser(description="hashcode-2020 solver")
    parser.add_argument("input", nargs='+', help="input file")
    parser.add_argument("--output",
                        "-o",
                        default="tmp.out",
                        help="output file (ignored if multiple input file)")
    args = parser.parse_args()
    total_score = 0
    multiple = len(args.input) > 1
    for input_file_path in args.input:
        print(input_file_path)
        with open(input_file_path) as input_file:
            input_data_set = parse_input_file(input_file)
        output_data = solve(input_data_set)
        score = compute_score(input_data_set, output_data)
        total_score += score
        print(f"score: {score:,}")
        output_file_path = f"{input_file_path}.out" if multiple else args.output
        with open(output_file_path, "w+") as output_file:
            write_output_file(output_data, output_file)
    if multiple:
        print(f"total score: {total_score:,}")
Example #3
0
def worker(best_score, best_solution, l, loaded_input, q, output):
    """General
    q.get() must be a loaded_input with the method generate_solution(int seed)
    """
    # We get the seed that will generate our solution
    # STOP stops the worker
    for seed in iter(q.get, 'STOP'):

        # We calculate a solution from a seed and evaluate its score
        solution = loaded_input.generate_solution(seed)
        score = compute_score(solution)

        # Lock state to prevent race condition
        l.acquire()
        #print(score)
        # If our solution is better we save it in the shared variable
        if score > best_score.value:
            
            best_score.value = score
            
            best_solution[0] = solution
        # End of atomic operation
        l.release()
        output.put(True)
Example #4
0
		# read test topic proportions
		test_theta = np.loadtxt('dirlda/test-gamma.dat')
		sumtheta = np.sum(test_theta,1)
		test_theta = test_theta/sumtheta.reshape(-1,1)

		# clustering
		group_assgnmt = np.argmax(test_theta,1)

		# write group file
		np.savetxt(tgroupfile,group_assgnmt.reshape(-1,1),'%d')

		# Run M4
		seed = np.random.randint(seed0)
		cmdtxt = M4Path + '/M4 '+str(seed) + ' test '+ testfile + ' ' + tgroupfile + ' '+str(Gtest)+ ' '+path+'/M4/dirM4/final ' + path + '/M4/dirM4' 
		os.system(cmdtxt + ' > /dev/null')

		# compute score
		seed = np.random.randint(seed0)
		f1scores = score.compute_score(seed, path, tlblfile, tgroupfile, resfile, anomlist)
		fpres = open(resfile,'a')
		fpres.write('****** M = %d, T = %d, F1-score = %f\n'%(M,T,np.mean(f1scores)))
		fpres.close()

fpres = open('results.txt','a')
fpres.write('############################################\n')
amax = np.argmax(f1scores)
m1 = amax/len(Tlist)
n1 = amax%len(Tlist)
fpres.write('Best F1-score: M = %d, K = %d, F1-score = %f' %(Mlist[m1],Tlist[n1],f1scores[m1,n1]))
fpres.close()
Example #5
0
    ###########################
    ## Find best solution
    ###########################

    start = time()
    solution = solve(loaded_input, seeds, number_cpu)
    end = time()
    computing_time = end - start

    ###########################
    ## Post-processing
    ###########################

    # Improves the solution
    solution = improve_solution(solution, loaded_input)

    ###########################
    ## Checks solution and writes it out
    ###########################

    # Check if solution is valid
    valid = is_valid_solution(solution, loaded_input)
    display(solution, loaded_input)

    # Compute score and display
    score = compute_score(solution) * valid
    print_score(score, loaded_input, end - start)

    # Writing to output
    write_output(args.input, "algo1", score, solution)
Example #6
0
		# read test topic proportions
		test_theta = np.loadtxt('dirlda/test-gamma.dat')
		sumtheta = np.sum(test_theta,1)
		test_theta = test_theta/sumtheta.reshape(-1,1)

		# clustering
		group_assgnmt = np.argmax(test_theta,1)

		# write group file
		np.savetxt(tgroupfile,group_assgnmt.reshape(-1,1),'%d')

		# Run M4
		seed = np.random.randint(seed0)
		cmdtxt = M4Path + '/M4 '+str(seed) + ' test '+ testfile + ' ' + tgroupfile + ' '+str(Gtest)+ ' '+path+'/M4/dirM4/final ' + path + '/M4/dirM4' 
		os.system(cmdtxt + ' > /dev/null')

		# compute score
		seed = np.random.randint(seed0)
		f1scores = score.compute_score(seed, path, tlblfile, tgroupfile, resfile, anomlist)
		fpres = open(resfile,'a')
		fpres.write('****** M = %d, T = %d, F1-score = %f\n'%(M,T,np.mean(f1scores)))
		fpres.close()

fpres = open('results.txt','a')
fpres.write('############################################\n')
amax = np.argmax(f1scores)
m1 = amax/len(Tlist)
n1 = amax%len(Tlist)
fpres.write('Best F1-score: M = %d, K = %d, F1-score = %f' %(Mlist[m1],Tlist[n1],f1scores[m1,n1]))
fpres.close()
Example #7
0
 def test_score_b_should_be_easy(self):
     score = compute_score(RES_FOLDER + "b_should_be_easy.in",
                           RES_FOLDER + "b_should_be_easy.out")
     self.assertEqual(score.raw_score, 169677)
     self.assertEqual(score.bonus_score, 7200)
Example #8
0
 def test_score_a_example(self):
     score = compute_score(RES_FOLDER + "a_example.in",
                           RES_FOLDER + "a_example.out")
     self.assertEqual(score.raw_score, 8)
     self.assertEqual(score.bonus_score, 2)
Example #9
0
 def test_score_b_should_be_easy(self):
     (raw_score,
      bonus_score) = compute_score(res_folder + "b_should_be_easy.in",
                                   res_folder + "b_should_be_easy.out")
     self.assertEqual(raw_score, 169677)
     self.assertEqual(bonus_score, 7200)
Example #10
0
 def test_score_a_example(self):
     (raw_score, bonus_score) = compute_score(res_folder + "a_example.in",
                                              res_folder + "a_example.out")
     self.assertEqual(raw_score, 8)
     self.assertEqual(bonus_score, 2)