Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to run a demo because of bad alloc #168

Open
muawijhe opened this issue Jul 4, 2019 · 10 comments
Open

Unable to run a demo because of bad alloc #168

muawijhe opened this issue Jul 4, 2019 · 10 comments
Assignees
Labels
help Questions about SYMPHONY or help using it.

Comments

@muawijhe
Copy link

muawijhe commented Jul 4, 2019

Hi!
Please have a look into the following code:

`#include

/===========================================================================/
/* /
/
This file is part of the SYMPHONY MILP Solver Framework. /
/
/
/
SYMPHONY was jointly developed by Ted Ralphs ted@lehigh.edu and /
/
Laci Ladanyi ladanyi@us.ibm.com. /
/
/
/
The author of this file is Menal Guzelsoy /
/
/
/
(c) Copyright 2005-2019 Lehigh University. All Rights Reserved. /
/
/
/
This software is licensed under the Eclipse Public License. Please see /
/
accompanying file for terms. /
/
/
/
===========================================================================*/

/-------------------------------------------------------------------------/
/* Matlab Code */
//clear all, close all, clc

//f = [1.3, 0, 2.5]
//A = [1.0, 2.0, 0.0; 0.0, 4.0, 5.0]
//b = [ 10; 10]
//lb = [1.5; 0.0; 1.0]
//ub = [inf; inf; inf]

//x = sdpvar(numel(f),1)

//C = [A*x<=b; lb<=x; x<=ub];

//sol = solvesdp(C, f*x)

//assert(~sol.problem)
//x = double(x)

//% f =
//%
//% 1.3000 0 2.5000
//%
//%
//% A =
//%
//% 1 2 0
//% 0 4 5
//%
//%
//% b =
//%
//% 10
//% 10
//%
//%
//% lb =
//%
//% 1.5000
//% 0
//% 1.0000
//%
//%
//% ub =
//%
//% Inf
//% Inf
//% Inf
//%
//% Linear matrix variable 3x1 (full, real, 3 variables)
//% Coeffiecient range: 1 to 1
//% * 0: obj = 1.800000000e+01 infeas = 0.000e+00 (0)
//% * 2: obj = 4.450000000e+00 infeas = 0.000e+00 (0)
//%
//% sol =
//%
//% struct with fields:
//%
//% yalmipversion: '20181012'
//% yalmiptime: 0.3979
//% solvertime: 0.0011
//% info: 'Successfully solved (GLPK-GLPKMEX-CC)'
//% problem: 0
//%
//%
//% x =
//%
//% 1.5000
//% 1.2500
//% 1.0000
/-------------------------------------------------------------------------/

#include <coin/symphony.h>
#include
#include <malloc.h>

int demo_symphony(int argc, char* argv[]){

/* Create a SYMPHONY environment */

sym_environment *env = sym_open_environment();

int n_cols = 3; //number of columns
double * objective =
(double *) malloc(sizeof(double) * n_cols);//the objective coefficients
double * col_lb =
(double *) malloc(sizeof(double) * n_cols);//the column lower bounds
double * col_ub =
(double *) malloc(sizeof(double) * n_cols);//the column upper bounds

// f = [1.3, 0, 2.5]
objective[0] = 1.3;
objective[1] = 0.0;
objective[2] = 2.5;

//Define the variable lower/upper bounds.
// lb = [1.5; 0.0; 1.0];
// ub = [inf; inf; inf];
col_lb[0] = 1.5;
col_lb[1] = 0.0;
col_lb[2] = 1.0;
col_ub[0] = sym_get_infinity();
col_ub[1] = sym_get_infinity();
col_ub[2] = sym_get_infinity();

// A = [ 1.0, 2.0, 0.0;
// 0.0, 4.0, 5.0]
// b = [ 10; 10]

int n_rows = 2;
char * row_sense =
(char *) malloc (sizeof(char) * n_rows); //the row senses
double * row_rhs =
(double *) malloc (sizeof(double) * n_rows); //the row right-hand-sides
double * row_range = NULL; //the row ranges
row_sense[0] = 'L';
row_rhs[0] = 10;
row_sense[1] = 'L';
row_rhs[1] = 10;

/* Constraint matrix definitions */
int non_zeros = 4;
int * start = (int *) malloc (sizeof(int) * (n_cols + 1));
int * index = (int *) malloc (sizeof(int) * non_zeros);
double * value = (double *) malloc (sizeof(double) *non_zeros);

start[0] = 0;
start[1] = 3;
start[2] = 6;

index[0] = 0;
index[1] = 1;
index[2] = 1;
index[3] = 2;

value[0] = 1;
value[1] = 2;
value[2] = 4;
value[3] = 5;

//define the integer variables

char * int_vars = (char *) malloc (sizeof(char) * n_cols);

int_vars[0] = FALSE;
int_vars[1] = FALSE;
int_vars[2] = FALSE;

//load the problem to environment
sym_explicit_load_problem(env, n_cols, n_rows, start, index, value, col_lb,
col_ub, int_vars, objective, NULL, row_sense,
row_rhs, row_range, TRUE);

//solve the integer program

sym_solve(env);

//get, print the solution
double * solution = (double *) malloc (sizeof(double) * n_cols);
double objective_value = 0.0;

sym_get_col_solution(env, solution);
sym_get_obj_val(env, &objective_value);

printf("%s\n%s%f\n%s%f\n%s%f\n%s%f\n","The optimal solution is",
" x0 = ",solution[0],
" x1 = ",solution[1],
" x2 = ",solution[2],
" with objective value = ",objective_value);

//free the memory
sym_close_environment(env);

if(objective){free(objective);}
if(col_lb) {free(col_lb);}
if(col_ub) {free(col_ub);}
if(row_rhs) {free(row_rhs);}
if(row_sense){free(row_sense);}
if(row_range){free(row_range);}
if(index) {free(index);}
if(start) {free(start);}
if(value) {free(value);}
if(int_vars) {free(int_vars);}
if(solution) {free(solution);}
return 0;

}

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

demo_symphony(argc, argv);
printf("solver complete execution\n");
return a.exec();

}
`

I cannot execute the code because of a bad alloc exception. I guess is due to the form of the sparse matrix of the constraints. Could you let me understand how to prepare the data? I didn't find proper examples or documentation to get rid of the problem and complete the execution of such a simple case.

Thanks so much

@tkralphs
Copy link
Member

tkralphs commented Jul 4, 2019

One thing I can see by inspection is that your start array is wrong. It should have length n_cols+1 start[n_cols+1] should be equal to non_zeros. In your case, it would be.

start[0] = 0;
start[1] = 1;
start[3] = 3;
start[4] = 4;

@tkralphs tkralphs added Help question Further information is requested help Questions about SYMPHONY or help using it. and removed help labels Jul 4, 2019
@muawijhe
Copy link
Author

muawijhe commented Jul 5, 2019 via email

@tkralphs
Copy link
Member

tkralphs commented Jul 6, 2019

Oops, sorry, I made a mistake in my response above that has now been corrected. The format used here is what is called Compressed Sparse Column format. It is explained in more detail here:
https://en.wikipedia.org/wiki/Sparse_matrix
The start vector is the start position of each column in the other two arrays, which contain a list of the row induces and values of all nonzeros in the matrix. Let me know if you have more questions!

@muawijhe
Copy link
Author

muawijhe commented Jul 6, 2019 via email

@muawijhe
Copy link
Author

muawijhe commented Jul 7, 2019 via email

@muawijhe
Copy link
Author

muawijhe commented Jul 8, 2019 via email

@tkralphs
Copy link
Member

tkralphs commented Jul 8, 2019

I can't tell you anything from just the fact that it does not work, especially not without seeing your (modified) code. This could be for a multitude of reasons. Please provide your code (preferably in a gist), the complete output and details, such as what version of SYMPHONY you are using, what compiler, what OS, how you built it, etc. If the code crashes, it would be ideal if you could send a backtrace. Please help me to help you.

@muawijhe
Copy link
Author

muawijhe commented Jul 9, 2019 via email

@muawijhe
Copy link
Author

Any suggestion so far?

@tkralphs
Copy link
Member

Sorry, I haven't had a chance to look at this. I will do so as soon as I can.

@tkralphs tkralphs removed the question Further information is requested label Jul 30, 2019
@tkralphs tkralphs self-assigned this Jul 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help Questions about SYMPHONY or help using it.
Projects
None yet
Development

No branches or pull requests

2 participants