コード例 #1
0
int main (void)
{
#ifdef INIT
    init_weights();
#endif

    tbb::tick_count t0 = tbb::tick_count::now();
    for (int c=0; c<TIMES; c++) {
        solution_weight = MAX_INT;
        solve_tsp(weights);
    }
    tbb::tick_count t1 = tbb::tick_count::now();
    printf("Ticks = %f\n", (t1-t0).seconds());
    printf("Solution Weight = %d\n", solution_weight);

    // Dump result
    hexdump_var("result.hex", &solution_weight, 1);

#ifdef PRINT_RESULT
    printf("Solution weight = %d\n", solution_weight);
    int i;
    for(i=0;i<SIZE;i++) { 
        printf("solution[%d] = %d\n", i, solution[i]);
    }
#endif
    return 0;
}
コード例 #2
0
ファイル: tsp.c プロジェクト: pbos/tsp
int main(int argc, char *argv[])
{
	graph g = read_kattis();
	if(g.n <= 3) // all solutions identical
	{
		for(size_t i = 0 ; i < g.n; ++i)
			printf("%d\n", i);
		graph_free(g);
		return 0;
	}

	// k neighbours can't be more than neighbours available
	if(g.n-1 < k)
		k = g.n-1;

/*
	fputs("[distance matrix]\n", stderr);
	for(size_t i = 0; i < g.n; ++i)
	{
		for(size_t j = 0; j < g.n; ++j)
			fprintf(stderr, "%3d ", graph_get_dist(g, i, j));
		fputc('\n', stderr);
	}

	fputs("\n[neighbour matrix]\n", stderr);
	for(size_t i = 0; i < g.n; ++i)
	{
		for(size_t j = 0; j < g.n; ++j)
			fprintf(stderr, "%3d ", graph_get_neighbour(g, i, j));
		fputc('\n', stderr);
	}*/

	size_t greedy[g.n];
	greedy_tsp(g, greedy);
	greedy_tour = greedy;
	fprintf(stderr, "\n[greedy tour]\n");
	print_tour(g, greedy);
	size_t tour[g.n];
	size_t tour2[g.n];
	for(size_t i = 0; i < g.n; ++i)
		tour2[i] = greedy[i];
	size_t *tour_ptr = tour;
	size_t *best = tour2;

	fprintf(stderr, "\n[tours]\n");
	for(size_t i = 0; i < 14; ++i)
	{
		solve_tsp(g, tour_ptr);
		print_tour(g, tour_ptr);
		if(tour_length(g, tour_ptr) < tour_length(g, best))
		{
			size_t *tmp = best;
			best = tour_ptr;
			tour_ptr = tmp;
		}
	}

	fprintf(stderr, "\n[kattis (best)]\n");
	print_kattis(g, best);
	fprintf(stderr, "[kattis length: %d]\n", tour_length(g,best));

	graph_free(g);

	return 0;
}
コード例 #3
0
ファイル: tsp2.c プロジェクト: mrdapotts/pgrouting
Datum
tsp_matrix(PG_FUNCTION_ARGS)
{
    FuncCallContext     *funcctx;
    int                  call_cntr;
    int                  max_calls;
    TupleDesc            tuple_desc;
    AttInMetadata       *attinmeta;

    DTYPE               *matrix;
    int                 *tsp_res;
    int                  num;

    /* stuff done only on the first call of the function */
    if (SRF_IS_FIRSTCALL()) {
        MemoryContext   oldcontext;
        //int path_count;
        int ret=-1;

        /* create a function context for cross-call persistence */
        funcctx = SRF_FIRSTCALL_INIT();

        /* switch to memory context appropriate for multiple function calls */
        oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);

        matrix = get_pgarray(&num, PG_GETARG_ARRAYTYPE_P(0));

        ret = solve_tsp(matrix, num,
                        PG_GETARG_INT32(1), // start index
                        PG_GETARG_INT32(2), // end  index
                        &tsp_res);

        pfree(matrix);
        if (ret < 0) {
            elog(ERROR, "Error, failed to solve TSP.");
        }

        funcctx->max_calls = num;
        funcctx->user_fctx = tsp_res;

        /* Build a tuple descriptor for our result type */
        if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("function returning record called in context "
                            "that cannot accept type record")));

        funcctx->tuple_desc = BlessTupleDesc(tuple_desc);

        /*
         * generate attribute metadata needed later to produce tuples from raw
         * C strings
         */
        //attinmeta = TupleDescGetAttInMetadata(tuple_desc);
        //funcctx->attinmeta = attinmeta;

        MemoryContextSwitchTo(oldcontext);
    }

    /* stuff done on every call of the function */
    funcctx = SRF_PERCALL_SETUP();

    call_cntr  = funcctx->call_cntr;
    max_calls  = funcctx->max_calls;
    tuple_desc = funcctx->tuple_desc;
    tsp_res    = funcctx->user_fctx;

    DBG("Trying to allocate some memory");
    DBG("call_cntr = %i, max_calls = %i", call_cntr, max_calls);

    if (call_cntr < max_calls) {   /* do when there is more left to send */
        HeapTuple    tuple;
        Datum        result;
        Datum *values;
        char* nulls;

        values = palloc(2 * sizeof(Datum));
        nulls = palloc(2 * sizeof(char));

        values[0] = Int32GetDatum(call_cntr);
        nulls[0] = ' ';
        values[1] = Int32GetDatum(tsp_res[call_cntr]);
        nulls[1] = ' ';

        DBG("RESULT: %d, %d", call_cntr, tsp_res[call_cntr]);

        DBG("Heap making");

        tuple = heap_formtuple(tuple_desc, values, nulls);

        DBG("Datum making");

        /* make the tuple into a datum */
        result = HeapTupleGetDatum(tuple);

        DBG("RESULT: seq:%d, id:%d", call_cntr, tsp_res[call_cntr]);
        DBG("Trying to free some memory");

        /* clean up (this is not really necessary) */
        pfree(values);
        pfree(nulls);


        SRF_RETURN_NEXT(funcctx, result);
    }
    else {   /* do when there is no more left */
        DBG("Freeing tsp_res");
        free(tsp_res);

        DBG("Ending function");
        SRF_RETURN_DONE(funcctx);
    }
}
コード例 #4
0
int main(int argc, char* argv[])
{
    int nth=-1; // number of (hardware) threads (-1 means undefined)
    if (argc > 1) { //command line with parameters
        if (argc > 2) {
            printf("ERROR: wrong use of command line arguments. Usage %s <#threads>\n", argv[0]);
            return 1;
        } else {
            nth = atoi( argv[1] );
        }
    }
    int defth = tbb::task_scheduler_init::default_num_threads();
    if (nth<0) nth=defth;
    printf("Default #Threads=%d. Using %d threads\n", defth, nth);
    tbb::task_scheduler_init init(nth);

    // init lock
#ifdef DEBUG_VERBOSE
    printf("test1\n");
#endif

#ifdef INIT
    init_weights();
#endif
#ifdef DEBUG_VERBOSE
    printf("release_failed = %d\n", release_failed);
    printf("out_of_memory  = %d\n", out_of_memory);
    printf("test2\n");
#endif
    tbb::tick_count t0 = tbb::tick_count::now();
    solution_weight = MAX_INT;
    solve_tsp(weights);
#ifdef NO_LOCKS
    //FunctorMin<int> my_min;
    solution_weight = my_weight.combine (min);


#endif
    tbb::tick_count t1 = tbb::tick_count::now();
    printf("Ticks = %f\n", (t1-t0).seconds());
    printf("Solution Weight = %d\n", solution_weight);

    // Dump result
    int solution = solution_weight;
    hexdump_var("result.hex", &solution, 1);

#ifdef DEBUG_VERBOSE
    printf("release_failed = %d\n", release_failed);
    printf("out_of_memory  = %d\n", out_of_memory);
#endif
#ifdef DEBUG
    // if there is an error invalidate solution_weight
    if (release_failed || out_of_memory) solution_weight = -1;
#endif 
#ifdef PRINT_RESULT
    printf("Solution weight = %d\n", solution_weight);
    list_node* tmp_p = solution;
    int i=SIZE;
    while(tmp_p != NULL && i!=0) {
        i--;
        printf("solution[%d] = %d\n", i, tmp_p->val);
        tmp_p = tmp_p->next;
    }
#endif
    return 0;
}