Example #1
0
File: demo.c Project: zhangrj7/scs
int main(int argc, char **argv) {
	FILE * fp;
	Cone * k;
	Data * d;
	Work * w;
	Sol * sol;
	Info info = { 0 };
	scs_int i;

	if (openFile(argc, argv, 1, DEMO_PATH, &fp) < 0)
		return -1;

	k = scs_calloc(1, sizeof(Cone));
	d = scs_calloc(1, sizeof(Data));
	sol = scs_calloc(1, sizeof(Sol));
	if (readInData(fp, d, k) == -1) {
		printf("Error reading in data, aborting.\n");
		return -1;
	}
	fclose(fp);
	scs_printf("solve once using scs\n");
	scs(d, k, sol, &info);

	if (TEST_WARM_START) {
		scs_printf("solve %i times with warm-start and (if applicable) factorization caching.\n", NUM_TRIALS);
		/* warm starts stored in Sol */
		w = scs_init(d, k, &info);
		if (w) {
			for (i = 0; i < NUM_TRIALS; i++) {
				/* perturb b and c */
				perturbVector(d->b, d->m);
				perturbVector(d->c, d->n);
				d->stgs->warm_start = 1;
				d->stgs->cg_rate = 4;
				scs_solve(w, d, k, sol, &info);
				d->stgs->warm_start = 0;
				d->stgs->cg_rate = 2;
				scs_solve(w, d, k, sol, &info);
			}
		}
		scs_printf("finished\n");
		scs_finish(w);
	}

	freeData(d, k);
	freeSol(sol);
	return 0;
}
Example #2
0
File: scsr.c Project: akshayka/scs
SEXP scsr(SEXP data, SEXP cone, SEXP params) {
    scs_int len, num_protected = 0;
    SEXP ret, retnames, infor, xr, yr, sr;

    /* allocate memory */
    Data *d = scs_malloc(sizeof(Data));
    Cone *k = scs_malloc(sizeof(Cone));
    Settings *stgs = scs_malloc(sizeof(Settings));
    AMatrix *A = scs_malloc(sizeof(AMatrix));
    Info *info = scs_calloc(1, sizeof(Info));
    Sol *sol = scs_calloc(1, sizeof(Sol));

    d->b = getFloatVectorFromList(data, "b", &len);
    d->c = getFloatVectorFromList(data, "c", &len);
    d->n = getIntFromListWithDefault(data, "n", 0);
    d->m = getIntFromListWithDefault(data, "m", 0);

    A->m = d->m;
    A->n = d->n;
    A->x = getFloatVectorFromList(data, "Ax", &len);
    A->i = getIntVectorFromList(data, "Ai", &len);
    A->p = getIntVectorFromList(data, "Ap", &len);
    d->A = A;

    stgs->max_iters = getIntFromListWithDefault(params, "max_iters", MAX_ITERS);
    stgs->normalize = getIntFromListWithDefault(params, "normalize", NORMALIZE);
    stgs->verbose = getIntFromListWithDefault(params, "verbose", VERBOSE);
    stgs->cg_rate = getFloatFromListWithDefault(params, "cg_rate", CG_RATE);
    stgs->scale = getFloatFromListWithDefault(params, "scale", SCALE);
    stgs->rho_x = getFloatFromListWithDefault(params, "rho_x", RHO_X);
    stgs->alpha = getFloatFromListWithDefault(params, "alpha", ALPHA);
    stgs->eps = getFloatFromListWithDefault(params, "eps", EPS);
    /* TODO add warm starting */
    stgs->warm_start =
        getIntFromListWithDefault(params, "warm_start", WARM_START);
    d->stgs = stgs;

    k->f = getIntFromListWithDefault(cone, "f", 0);
    k->l = getIntFromListWithDefault(cone, "l", 0);
    k->ep = getIntFromListWithDefault(cone, "ep", 0);
    k->ed = getIntFromListWithDefault(cone, "ed", 0);
    k->q = getIntVectorFromList(cone, "q", &(k->qsize));
    k->s = getIntVectorFromList(cone, "s", &(k->ssize));
    k->p = getFloatVectorFromList(cone, "p", &(k->psize));

    /* solve! */
    scs(d, k, sol, info);

    infor = populateInfoR(info, &num_protected);

    PROTECT(ret = NEW_LIST(4));
    num_protected++;
    PROTECT(retnames = NEW_CHARACTER(4));
    num_protected++;
    SET_NAMES(ret, retnames);

    xr = floatVec2R(d->n, sol->x);
    num_protected++;
    yr = floatVec2R(d->m, sol->y);
    num_protected++;
    sr = floatVec2R(d->m, sol->s);
    num_protected++;

    SET_STRING_ELT(retnames, 0, mkChar("x"));
    SET_VECTOR_ELT(ret, 0, xr);
    SET_STRING_ELT(retnames, 1, mkChar("y"));
    SET_VECTOR_ELT(ret, 1, yr);
    SET_STRING_ELT(retnames, 2, mkChar("s"));
    SET_VECTOR_ELT(ret, 2, sr);
    SET_STRING_ELT(retnames, 3, mkChar("info"));
    SET_VECTOR_ELT(ret, 3, infor);

    /* free memory */
    scs_free(info);
    scs_free(d);
    scs_free(k);
    scs_free(stgs);
    scs_free(A);
    freeSol(sol);
    UNPROTECT(num_protected);
    return ret;
}
Example #3
0
int main(int argc, char **argv) {
	scs_int n, m, col_nnz, nnz, i, q_total, q_num_rows, max_q;
	Cone * k;
	Data * d;
	Sol * sol, * opt_sol;
	Info info = { 0 };
	scs_float p_f, p_l;
	int seed = 0;

	/* default parameters */
	p_f = 0.1;
	p_l = 0.3;
	seed = time(SCS_NULL);

	switch (argc) {
	case 5:
		seed = atoi(argv[4]);
		/* no break */
	case 4:
		p_f = atof(argv[2]);
		p_l = atof(argv[3]);
		/* no break */
	case 2:
		n = atoi(argv[1]);
		break;
	default:
		scs_printf("usage:\t%s n p_f p_l s\n"
				"\tcreates an SOCP with n variables where p_f fraction of rows correspond\n"
				"\tto equality constraints, p_l fraction of rows correspond to LP constraints,\n"
				"\tand the remaining percentage of rows are involved in second-order\n"
				"\tcone constraints. the random number generator is seeded with s.\n"
				"\tnote that p_f + p_l should be less than or equal to 1, and that\n"
				"\tp_f should be less than .33, since that corresponds to as many equality\n"
				"\tconstraints as variables.\n", argv[0]);
		scs_printf("\nusage:\t%s n p_f p_l\n"
				"\tdefaults the seed to the system time\n", argv[0]);
		scs_printf("\nusage:\t%s n\n"
				"\tdefaults to using p_f = 0.1 and p_l = 0.3\n", argv[0]);
		return 0;
	}
	srand(seed);
	scs_printf("seed : %i\n", seed);

    k = scs_calloc(1, sizeof(Cone));
    d = scs_calloc(1, sizeof(Data));
    d->stgs = scs_calloc(1, sizeof(Settings));
    sol = scs_calloc(1, sizeof(Sol));
    opt_sol = scs_calloc(1, sizeof(Sol));

	m = 3 * n;
	col_nnz = (int) ceil(sqrt(n));
	nnz = n * col_nnz;

	max_q = (scs_int) ceil(3 * n / log(3 * n));

	if (p_f + p_l > 1.0) {
		printf("error: p_f + p_l > 1.0!\n");
		return 1;
	}

	k->f = (scs_int) floor(3 * n * p_f);
	k->l = (scs_int) floor(3 * n * p_l);

	k->qsize = 0;
	q_num_rows = 3 * n - k->f - k->l;
	k->q = scs_malloc(q_num_rows * sizeof(scs_int));

	while (q_num_rows > max_q) {
		int size;
		size = (rand() % max_q) + 1;
		k->q[k->qsize] = size;
		k->qsize++;
		q_num_rows -= size;
	}
	if (q_num_rows > 0) {
		k->q[k->qsize] = q_num_rows;
		k->qsize++;
	}

	q_total = 0;
	for (i = 0; i < k->qsize; i++) {
		q_total += k->q[i];
	}

	k->s = SCS_NULL;
	k->ssize = 0;
	k->ep = 0;
	k->ed = 0;

	scs_printf("\nA is %ld by %ld, with %ld nonzeros per column.\n", (long) m, (long) n, (long) col_nnz);
	scs_printf("A has %ld nonzeros (%f%% dense).\n", (long) nnz, 100 * (scs_float) col_nnz / m);
	scs_printf("Nonzeros of A take %f GB of storage.\n", ((scs_float) nnz * sizeof(scs_float)) / POWF(2, 30));
	scs_printf("Row idxs of A take %f GB of storage.\n", ((scs_float) nnz * sizeof(scs_int)) / POWF(2, 30));
	scs_printf("Col ptrs of A take %f GB of storage.\n\n", ((scs_float) n * sizeof(scs_int)) / POWF(2, 30));

	printf("Cone information:\n");
	printf("Zero cone rows: %ld\n", (long) k->f);
	printf("LP cone rows: %ld\n", (long) k->l);
	printf("Number of second-order cones: %ld, covering %ld rows, with sizes\n[", (long) k->qsize, (long) q_total);
	for (i = 0; i < k->qsize; i++) {
		printf("%ld, ", (long) k->q[i]);
	}
	printf("]\n");
	printf("Number of rows covered is %ld out of %ld.\n\n", (long) (q_total + k->f + k->l), (long) m);

	/* set up SCS structures */
	d->m = m;
	d->n = n;
	genRandomProbData(nnz, col_nnz, d, k, opt_sol);
	setDefaultSettings(d);

	scs_printf("true pri opt = %4f\n", innerProd(d->c, opt_sol->x, d->n));
	scs_printf("true dua opt = %4f\n", -innerProd(d->b, opt_sol->y, d->m));
    /* solve! */
	scs(d, k, sol, &info);
	scs_printf("true pri opt = %4f\n", innerProd(d->c, opt_sol->x, d->n));
	scs_printf("true dua opt = %4f\n", -innerProd(d->b, opt_sol->y, d->m));

    if (sol->x) { scs_printf("scs pri obj= %4f\n", innerProd(d->c, sol->x, d->n)); }
    if (sol->y) { scs_printf("scs dua obj = %4f\n", -innerProd(d->b, sol->y, d->m)); }

    freeData(d, k);
	freeSol(sol);
    freeSol(opt_sol);

    return 0;
}