23 #define LB_HAT_IN prhs[4]
24 #define UB_HAT_IN prhs[5]
28 #define OPT_IN prhs[9]
29 #define INFO_IN prhs[10]
32 #define H_HAT_IN prhs[11]
33 #define A2_IN prhs[12]
34 #define RHO_AT_B_IN prhs[13]
37 #define ZOPT_OUT plhs[0]
38 #define FOPT_OUT plhs[1]
39 #define EXITFLAG_OUT plhs[2]
40 #define OUTPUT_STRUCT_OUT plhs[3]
41 #define LAMBDA1_OUT plhs[4]
42 #define LAMBDA2_OUT plhs[5]
46 static void init_Info();
47 static void init_Options();
48 static void init_Problem();
50 static void make_output();
51 static void make_output_fail_safe();
52 static void make_Output_struct();
54 static void clean_up_Output_M();
55 static void clean_up_Result();
57 static void init_DGM();
58 static void allocate_DGM();
59 static void clean_up_DGM_matlab();
61 static void init_DFGM();
62 static void allocate_DFGM();
63 static void clean_up_DFGM_matlab();
65 static void init_ALM();
66 static void allocate_ALM();
67 static void clean_up_ALM_matlab();
69 static void init_FALM();
70 static void allocate_FALM();
71 static void clean_up_FALM_matlab();
74 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
79 int algorithm = mxGetScalar(mxGetField(
OPT_IN, 0,
"algorithm"));
82 if (algorithm == 1 || algorithm == 2)
85 init_DGM(nrhs, prhs, &s);
88 ERROR(
"Error in DGM()\n");
89 make_output_fail_safe(nlhs, plhs);
90 clean_up_DGM_matlab(&s);
93 make_output(nlhs, plhs, s.
res);
94 clean_up_DGM_matlab(&s);
96 else if (algorithm == 3 || algorithm == 4)
99 init_DFGM(nrhs, prhs, &s);
101 ERROR(
"Error in DFGM()\n");
102 make_output_fail_safe(nlhs, plhs);
103 clean_up_DFGM_matlab(&s);
106 make_output(nlhs, plhs, s.
res);
107 clean_up_DFGM_matlab(&s);
109 else if (algorithm == 5 || algorithm == 6)
112 init_ALM(nrhs, prhs, &s);
114 ERROR(
"Error in ALM()\n");
115 make_output_fail_safe(nlhs, plhs);
116 clean_up_ALM_matlab(&s);
119 make_output(nlhs, plhs, s.
res);
120 clean_up_ALM_matlab(&s);
123 else if (algorithm == 7 || algorithm == 8)
126 init_FALM(nrhs, prhs, &s);
128 ERROR(
"Error in FALM()\n");
129 make_output_fail_safe(nlhs, plhs);
130 clean_up_FALM_matlab(&s);
133 make_output(nlhs, plhs, s.
res);
134 clean_up_FALM_matlab(&s);
138 _SDEBUG(
"*** Main finish**\n");
146 static void init_Info(
int nrhs,
const mxArray *prhs[],
struct Info *p)
153 p->
Ld = mxGetScalar(mxGetField(
INFO_IN, 0,
"Ld"));
177 printf(
"Could not resolve problem case\n");
182 static void init_Options(
int nrhs,
const mxArray *prhs[],
struct Options *
opt)
187 opt->
eps_ds = mxGetScalar(mxGetField(
OPT_IN, 0,
"eps_ds"));
188 opt->
eps_pf = mxGetScalar(mxGetField(
OPT_IN, 0,
"eps_pf"));
193 static void init_Problem(
int nrhs,
const mxArray *prhs[],
struct Problem *p)
197 b1 = mxDuplicateArray(
H_IN);
198 b2 = mxDuplicateArray(
A_IN);
206 mxArray *a1,*a2,*a3,*a4,*a5,*a6,*a7;
207 a1 = mxDuplicateArray(
C_IN);
208 a2 = mxDuplicateArray(
B_IN);
211 a5 = mxDuplicateArray(
LB_IN);
212 a6 = mxDuplicateArray(
UB_IN);
213 a7 = mxDuplicateArray(
Z0_IN);
223 static void allocate_DGM(
struct Struct_DGM *s)
267 static void allocate_ALM(
struct Struct_ALM *s)
303 static void init_DGM(
int nrhs,
const mxArray *prhs[],
struct Struct_DGM *s)
306 N = mxGetScalar(mxGetField(
INFO_IN, 0,
"N"));
307 M = mxGetScalar(mxGetField(
INFO_IN, 0,
"M"));
310 init_Info(nrhs, prhs, s->
info);
312 s->
opt = malloc(
sizeof * s->
opt);
313 init_Options(nrhs, prhs, s->
opt);
316 init_Problem(nrhs, prhs, s->
prob);
318 s->
res = malloc(
sizeof * s->
res);
325 static void init_DFGM(
int nrhs,
const mxArray *prhs[],
struct Struct_DFGM *s)
329 N = mxGetScalar(mxGetField(
INFO_IN, 0,
"N"));
330 M = mxGetScalar(mxGetField(
INFO_IN, 0,
"M"));
333 init_Info(nrhs, prhs, s->
info);
335 s->
opt = malloc(
sizeof * s->
opt);
336 init_Options(nrhs, prhs, s->
opt);
339 init_Problem(nrhs, prhs, s->
prob);
341 s->
res = malloc(
sizeof * s->
res);
348 static void init_ALM(
int nrhs,
const mxArray *prhs[],
struct Struct_ALM *s)
351 N = mxGetScalar(mxGetField(
INFO_IN, 0,
"N"));
352 M = mxGetScalar(mxGetField(
INFO_IN, 0,
"M"));
355 init_Info(nrhs, prhs, s->
info);
357 s->
opt = malloc(
sizeof * s->
opt);
358 init_Options(nrhs, prhs, s->
opt);
359 s->
opt->
rho = mxGetScalar(mxGetField(
OPT_IN, 0,
"rho"));
362 init_Problem(nrhs, prhs, s->
prob);
364 s->
res = malloc(
sizeof * s->
res);
373 a2 = mxDuplicateArray(
A2_IN);
376 s->
H_hat = mxGetPr(a1);
381 static void init_FALM(
int nrhs,
const mxArray *prhs[],
struct Struct_FALM *s)
384 N = mxGetScalar(mxGetField(
INFO_IN, 0,
"N"));
385 M = mxGetScalar(mxGetField(
INFO_IN, 0,
"M"));
388 init_Info(nrhs, prhs, s->
info);
390 s->
opt = malloc(
sizeof * s->
opt);
391 init_Options(nrhs, prhs, s->
opt);
392 s->
opt->
rho = mxGetScalar(mxGetField(
OPT_IN, 0,
"rho"));
395 init_Problem(nrhs, prhs, s->
prob);
397 s->
res = malloc(
sizeof * s->
res);
406 a2 = mxDuplicateArray(
A2_IN);
409 s->
H_hat = mxGetPr(a1);
414 static void clean_up_Output_M(
struct Output *s)
422 static void clean_up_Result(
struct Result *
res)
424 clean_up_Output_M(res->
out);
432 static void clean_up_DGM_matlab (
struct Struct_DGM *s)
435 free(s->
opt); s->
opt = NULL;
449 clean_up_Result(s->
res);
452 static void clean_up_DFGM_matlab (
struct Struct_DFGM *s)
455 free(s->
opt); s->
opt = NULL;
469 clean_up_Result(s->
res);
480 static void clean_up_ALM_matlab (
struct Struct_ALM *s)
483 free(s->
opt); s->
opt = NULL;
494 clean_up_Result(s->
res);
497 static void clean_up_FALM_matlab (
struct Struct_FALM *s)
500 free(s->
opt); s->
opt = NULL;
511 clean_up_Result(s->
res);
520 static void make_output(
int nlhs, mxArray *plhs[],
const struct Result *
res)
526 MEX_zopt =
ZOPT_OUT = mxCreateDoubleMatrix(
N,1,mxREAL);
527 real_t *ZOPT = mxGetPr(MEX_zopt);
529 ZOPT[i] = res->
zopt[i];
533 MEX_fopt=
FOPT_OUT = mxCreateDoubleMatrix(1,1,mxREAL);
534 real_t *FOPT = mxGetPr(MEX_fopt);
538 mxArray *MEX_exitflag;
539 MEX_exitflag =
EXITFLAG_OUT = mxCreateDoubleMatrix(1,1,mxREAL);
540 real_t *EXITFLAG = mxGetPr(MEX_exitflag);
544 make_Output_struct(nlhs, plhs, res->
out);
547 mxArray *MEX_lambda1;
548 MEX_lambda1 =
LAMBDA1_OUT = mxCreateDoubleMatrix(N,1,mxREAL);
549 real_t *LAMBDA1 = mxGetPr(MEX_lambda1);
555 mxArray *MEX_lambda2;
556 MEX_lambda2 =
LAMBDA2_OUT = mxCreateDoubleMatrix(N,1,mxREAL);
557 real_t *LAMBDA2 = mxGetPr(MEX_lambda2);
563 static void make_output_fail_safe(
int nlhs, mxArray *plhs[])
569 MEX_zopt =
ZOPT_OUT = mxCreateDoubleMatrix(1,1,mxREAL);
570 real_t *ZOPT = mxGetPr(MEX_zopt);
575 MEX_fopt=
FOPT_OUT = mxCreateDoubleMatrix(1,1,mxREAL);
576 real_t *FOPT = mxGetPr(MEX_fopt);
580 mxArray *MEX_exitflag;
581 MEX_exitflag =
EXITFLAG_OUT = mxCreateDoubleMatrix(1,1,mxREAL);
582 real_t *EXITFLAG = mxGetPr(MEX_exitflag);
588 real_t *OUTPUT = mxGetPr(MEX_output);
592 mxArray *MEX_lambda1;
593 MEX_lambda1 =
LAMBDA1_OUT = mxCreateDoubleMatrix(N,1,mxREAL);
594 real_t *LAMBDA1 = mxGetPr(MEX_lambda1);
598 mxArray *MEX_lambda2;
599 MEX_lambda2 =
LAMBDA2_OUT = mxCreateDoubleMatrix(N,1,mxREAL);
600 real_t *LAMBDA2 = mxGetPr(MEX_lambda2);
606 static void make_Output_struct(
int nlhs, mxArray *plhs[],
const struct Output * out)
608 #define NUM_FIELDS 11 // Number of fields in struct
609 #define NAME_LENGT 30
612 #define ITERATION_INNER_TOT 1
614 #define TIME_TOT_INNER 3
615 #define FLAG_LAST_SATISFIED 4
616 #define NITER_FEASIBLE_DS 5
617 #define NITER_FEASIBLE_PF 6
618 #define EXITFLAG_INNER 7
619 #define NUM_EXCEEDED_MAX_NITER_INNER 8
632 mxArray *MEX_iterations = mxCreateDoubleMatrix (1,1,mxREAL);
633 real_t *iterations = mxGetPr(MEX_iterations);
637 mxArray *MEX_iterations_inner_tot = mxCreateDoubleMatrix (1,1,mxREAL);
638 real_t *iterations_inner_tot = mxGetPr(MEX_iterations_inner_tot);
642 mxArray *MEX_time = mxCreateDoubleMatrix (1,1,mxREAL);
643 real_t *time = mxGetPr(MEX_time);
647 mxArray *MEX_time_tot_inner = mxCreateDoubleMatrix (1,1,mxREAL);
648 real_t *time_tot_inner = mxGetPr(MEX_time_tot_inner);
652 mxArray *MEX_flag_last_satisfied = mxCreateDoubleMatrix (1,1,mxREAL);
653 real_t *flag_last_satisfied = mxGetPr(MEX_flag_last_satisfied);
657 mxArray *MEX_niter_feasible_ds = mxCreateDoubleMatrix (1,1,mxREAL);
658 real_t *niter_feasible_ds = mxGetPr(MEX_niter_feasible_ds);
662 mxArray *MEX_niter_feasible_pf = mxCreateDoubleMatrix (1,1,mxREAL);
663 real_t *niter_feasible_pf = mxGetPr(MEX_niter_feasible_pf);
667 mxArray *MEX_exitflag_inner = mxCreateDoubleMatrix (1,1,mxREAL);
668 real_t *exitflag_inner = mxGetPr(MEX_exitflag_inner);
672 mxArray *MEX_num_exceeded_max_niter_inner = mxCreateDoubleMatrix (1,1,mxREAL);
673 real_t *num_exceeded_max_niter_inner = mxGetPr(MEX_num_exceeded_max_niter_inner);
677 mxArray *MEX_pf_vector = mxCreateDoubleMatrix (out->
iterations,1,mxREAL);
678 real_t *pf_vector = mxGetPr(MEX_pf_vector);
683 mxArray *MEX_ds_vector = mxCreateDoubleMatrix (out->
iterations,1,mxREAL);
684 real_t *ds_vector = mxGetPr(MEX_ds_vector);
693 mxFree((
void*) fieldnames[i] );
698 mxSetFieldByNumber(
OUTPUT_STRUCT_OUT,0,ITERATION_INNER_TOT, MEX_iterations_inner_tot);
701 mxSetFieldByNumber(
OUTPUT_STRUCT_OUT,0,FLAG_LAST_SATISFIED, MEX_flag_last_satisfied);
702 mxSetFieldByNumber(
OUTPUT_STRUCT_OUT,0,NITER_FEASIBLE_DS, MEX_niter_feasible_ds);
703 mxSetFieldByNumber(
OUTPUT_STRUCT_OUT,0,NITER_FEASIBLE_PF, MEX_niter_feasible_pf);
705 mxSetFieldByNumber(
OUTPUT_STRUCT_OUT,0,NUM_EXCEEDED_MAX_NITER_INNER, MEX_num_exceeded_max_niter_inner);
void free_pointer(real_t *pointer)
#define NITER_FEASIBLE_DS
real_t * vector_alloc(uint32_t size)
uint32_t iterations_inner_tot
int32_t DFGM(struct Struct_DFGM *s)
#define FLAG_LAST_SATISFIED
uint32_t flag_last_satisfied
uint32_t niter_feasible_pf
void mtx_transpose(const real_t *mtx, real_t *mtx_t, const uint32_t rows, const uint32_t cols)
int32_t DGM(struct Struct_DGM *s)
uint32_t num_exceeded_max_niter_inner
#define NITER_FEASIBLE_PF
int32_t FALM(struct Struct_FALM *s)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
#define NUM_EXCEEDED_MAX_NITER_INNER
#define ITERATION_INNER_TOT
#define OUTPUT_STRUCT_OUT
uint32_t niter_feasible_ds
int32_t ALM(struct Struct_ALM *s)