Skip to content

Commit

Permalink
fix issues raised when adding COPT supports to YALMIP
Browse files Browse the repository at this point in the history
  • Loading branch information
riis-hhd committed Jun 14, 2023
1 parent caa4382 commit 40b0dd9
Show file tree
Hide file tree
Showing 14 changed files with 6,071 additions and 4,737 deletions.
6 changes: 6 additions & 0 deletions lib/copt_defaultparams.m
@@ -0,0 +1,6 @@
% copt_defaultparams
%
% params = copt_defaultparams()
%
% This function generates default parameter settings of COPT as a MATLAB struct.
%
6 changes: 5 additions & 1 deletion src/Makefile.osx
Expand Up @@ -8,7 +8,7 @@ CFLAGS = -O2 -std=c99 -fPIC -Werror -DNDEBUG -DMATLAB_MEX_FILE
C_INCS = -I. -I$(MATLAB_HOME)/extern/include -I$(COPT_HOME)/include
C_LIBS = -L$(COPT_HOME)/lib -lcopt -L$(MATLAB_HOME)/bin/maci64 -lmx -lmex -lmat

C_MEXS = copt_read copt_solve copt_write copt_computeiis copt_feasrelax copt_tune
C_MEXS = copt_read copt_solve copt_write copt_computeiis copt_feasrelax copt_tune copt_defaultparams

all: $(C_MEXS)

Expand Down Expand Up @@ -36,5 +36,9 @@ copt_tune: copt_tune.c coptmex.c coptmex.h coptinit.c
@$(CC) $(CFLAGS) $(C_INCS) -shared -install_name @rpath/copt_tune.mexmaci64 -o copt_tune.mexmaci64 copt_tune.c coptmex.c $(C_LIBS)
@cp copt_tune.mexmaci64 ../lib

copt_defaultparams: copt_defaultparams.c coptmex.c coptmex.h coptinit.c
@$(CC) $(CFLAGS) $(C_INCS) -shared -install_name @rpath/copt_defaultparams.mexmaci64 -o copt_defaultparams.mexmaci64 copt_defaultparams.c coptmex.c $(C_LIBS)
@cp copt_defaultparams.mexmaci64 ../lib

clean:
@rm -f *.mexmaci64
6 changes: 5 additions & 1 deletion src/Makefile.unix
Expand Up @@ -8,7 +8,7 @@ CFLAGS = -O2 -std=c99 -fPIC -Werror -DNDEBUG -DMATLAB_MEX_FILE -Wno-incompatible
C_INCS = -I. -I$(MATLAB_HOME)/extern/include -I$(COPT_HOME)/include
C_LIBS = -L$(COPT_HOME)/lib -lcopt -L$(MATLAB_HOME)/bin/glnxa64 -lmx -lmex -lmat -lm -ldl

C_MEXS = copt_read copt_solve copt_write copt_computeiis copt_feasrelax copt_tune
C_MEXS = copt_read copt_solve copt_write copt_computeiis copt_feasrelax copt_tune copt_defaultparams

all: $(C_MEXS)

Expand Down Expand Up @@ -36,5 +36,9 @@ copt_tune: copt_tune.c coptmex.c coptmex.h coptinit.c
@$(CC) $(CFLAGS) $(C_INCS) -shared -o copt_tune.mexa64 copt_tune.c coptmex.c $(C_LIBS)
@cp copt_tune.mexa64 ../lib

copt_defaultparams: copt_defaultparams.c coptmex.c coptmex.h coptinit.c
@$(CC) $(CFLAGS) $(C_INCS) -shared -o copt_defaultparams.mexa64 copt_defaultparams.c coptmex.c $(C_LIBS)
@cp copt_defaultparams.mexa64 ../lib

clean:
@rm -f *.mexa64
6 changes: 5 additions & 1 deletion src/Makefile.win
Expand Up @@ -8,7 +8,7 @@ CFLAGS = /nologo /O2 /MT /WX /DNDEBUG /DMATLAB_MEX_FILE
C_INCS = -I. -I"$(MATLAB_HOME)\extern\include" -I"$(COPT_HOME)\include"
C_LIBS = /LIBPATH:"$(COPT_HOME)\lib" copt.lib /LIBPATH:"$(MATLAB_HOME)\extern\lib\win64\microsoft" libmx.lib libmex.lib libmat.lib

C_MEXS = copt_read copt_solve copt_write copt_computeiis copt_feasrelax copt_tune
C_MEXS = copt_read copt_solve copt_write copt_computeiis copt_feasrelax copt_tune copt_defaultparams

all: $(C_MEXS)

Expand Down Expand Up @@ -36,5 +36,9 @@ copt_tune: copt_tune.c coptmex.c coptmex.h coptinit.c
@$(CC) $(CFLAGS) $(C_INCS) copt_tune.c coptmex.c /link /dll /out:copt_tune.mexw64 $(C_LIBS) /export:mexFunction
@copy /Y copt_tune.mexw64 ..\lib > nul

copt_defaultparams: copt_defaultparams.c coptmex.c coptmex.h coptinit.c
@$(CC) $(CFLAGS) $(C_INCS) copt_defaultparams.c coptmex.c /link /dll /out:copt_defaultparams.mexw64 $(C_LIBS) /export:mexFunction
@copy /Y copt_defaultparams.mexw64 ..\lib > nul

clean:
@del /s /q *.mexw64 *.exp *.lib *.obj > nul
186 changes: 102 additions & 84 deletions src/copt_computeiis.c
@@ -1,84 +1,102 @@
#include "coptmex.h"

void COPT_CALL COPTMEX_printLog(char *msg, void *userdata) {
if (msg != NULL) {
mexPrintf("%s\n", msg);
mexEvalString("drawnow;");
}
}

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
int retcode = COPT_RETCODE_OK;
copt_env *env = NULL;
copt_prob *prob = NULL;
int retResult = 1;

// Check if inputs/outputs are valid
if (nlhs != 0 && nlhs != 1) {
COPTMEX_errorMsg(COPTMEX_ERROR_BAD_NUM, "outputs");
goto exit_cleanup;
}
if (nlhs == 0) {
retResult = 0;
}

if (nrhs == 1 || nrhs == 2) {
if (!mxIsChar(prhs[0]) && !mxIsStruct(prhs[0])) {
COPTMEX_errorMsg(COPTMEX_ERROR_BAD_TYPE, "problem/probfile");
goto exit_cleanup;
}
if (nrhs == 2) {
if (!mxIsStruct(prhs[1])) {
COPTMEX_errorMsg(COPTMEX_ERROR_BAD_TYPE, "parameter");
goto exit_cleanup;
}
}
} else {
COPTMEX_errorMsg(COPTMEX_ERROR_BAD_NUM, "inputs");
goto exit_cleanup;
}

// Create COPT environment and problem
COPTMEX_CALL(COPT_CreateEnv(&env));
COPTMEX_CALL(COPT_CreateProb(env, &prob));

// Set message callback
COPTMEX_CALL(COPT_SetLogCallback(prob, COPTMEX_printLog, NULL));

// Processing the second argument, if exists.
if (nrhs == 2) {
// Load and set parameters to problem
COPTMEX_CALL(COPTMEX_setParam(prob, prhs[1]));
} else {
COPTMEX_CALL(COPTMEX_dispBanner());
}

// Processing the first argument
// 1. 'string': a valid problem file;
// 2. 'struct': a struct that specify the problem data.
if (mxIsChar(prhs[0])) {
// Read the problem from file
COPTMEX_CALL(COPTMEX_readModel(prob, prhs[0]));
} else if (mxIsStruct(prhs[0])) {
// Extract and load data to problem
COPTMEX_CALL(COPTMEX_loadModel(prob, prhs[0]));
}

// Compute IIS for infeasible problem and save result
COPTMEX_CALL(COPTMEX_computeIIS(prob, &plhs[0], retResult));

exit_cleanup:
if (retcode != COPT_RETCODE_OK) {
char errmsg[COPT_BUFFSIZE];
char msgbuf[COPT_BUFFSIZE * 2];
COPT_GetRetcodeMsg(retcode, errmsg, COPT_BUFFSIZE);
snprintf(msgbuf, COPT_BUFFSIZE * 2, "COPT Error %d: %s", retcode, errmsg);
COPTMEX_errorMsg(COPTMEX_ERROR_BAD_API, msgbuf);
}

// Delete COPT problem and environment
COPT_DeleteProb(&prob);
COPT_DeleteEnv(&env);

return;
}
#include "coptmex.h"

void COPT_CALL COPTMEX_printLog(char* msg, void* userdata)
{
if (msg != NULL)
{
mexPrintf("%s\n", msg);
mexEvalString("drawnow;");
}
}

void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
int retcode = COPT_RETCODE_OK;
copt_env* env = NULL;
copt_prob* prob = NULL;
int retResult = 1;

// Check if inputs/outputs are valid
if (nlhs != 0 && nlhs != 1)
{
COPTMEX_errorMsg(COPTMEX_ERROR_BAD_NUM, "outputs");
goto exit_cleanup;
}
if (nlhs == 0)
{
retResult = 0;
}

if (nrhs == 1 || nrhs == 2)
{
if (!mxIsChar(prhs[0]) && !mxIsStruct(prhs[0]))
{
COPTMEX_errorMsg(COPTMEX_ERROR_BAD_TYPE, "problem/probfile");
goto exit_cleanup;
}
if (nrhs == 2)
{
if (!mxIsStruct(prhs[1]))
{
COPTMEX_errorMsg(COPTMEX_ERROR_BAD_TYPE, "parameter");
goto exit_cleanup;
}
}
}
else
{
COPTMEX_errorMsg(COPTMEX_ERROR_BAD_NUM, "inputs");
goto exit_cleanup;
}

// Create COPT environment and problem
COPTMEX_CALL(COPT_CreateEnv(&env));
COPTMEX_CALL(COPT_CreateProb(env, &prob));

// Set message callback
COPTMEX_CALL(COPT_SetLogCallback(prob, COPTMEX_printLog, NULL));

// Processing the second argument, if exists.
if (nrhs == 2)
{
// Load and set parameters to problem
COPTMEX_CALL(COPTMEX_setParam(prob, prhs[1]));
}
else
{
COPTMEX_CALL(COPTMEX_dispBanner());
}

// Processing the first argument
// 1. 'string': a valid problem file;
// 2. 'struct': a struct that specify the problem data.
if (mxIsChar(prhs[0]))
{
// Read the problem from file
COPTMEX_CALL(COPTMEX_readModel(prob, prhs[0]));
}
else if (mxIsStruct(prhs[0]))
{
// Extract and load data to problem
COPTMEX_CALL(COPTMEX_loadModel(prob, prhs[0]));
}

// Compute IIS for infeasible problem and save result
COPTMEX_CALL(COPTMEX_computeIIS(prob, &plhs[0], retResult));

exit_cleanup:
if (retcode != COPT_RETCODE_OK)
{
char errmsg[COPT_BUFFSIZE];
char msgbuf[COPT_BUFFSIZE * 2];
COPT_GetRetcodeMsg(retcode, errmsg, COPT_BUFFSIZE);
snprintf(msgbuf, COPT_BUFFSIZE * 2, "COPT Error %d: %s", retcode, errmsg);
COPTMEX_errorMsg(COPTMEX_ERROR_BAD_API, msgbuf);
}

// Delete COPT problem and environment
COPT_DeleteProb(&prob);
COPT_DeleteEnv(&env);

return;
}
46 changes: 46 additions & 0 deletions src/copt_defaultparams.c
@@ -0,0 +1,46 @@
#include "coptmex.h"

void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
int retcode = COPT_RETCODE_OK;
copt_env* env = NULL;
copt_prob* prob = NULL;

// Check if arguments are valid
if (nlhs > 1)
{
COPTMEX_errorMsg(COPTMEX_ERROR_BAD_NUM, "outputs");
goto exit_cleanup;
}
if (nrhs > 0)
{
COPTMEX_errorMsg(COPTMEX_ERROR_BAD_NUM, "inputs");
goto exit_cleanup;
}

// Create COPT environment and problem
COPTMEX_CALL(COPT_CreateEnv(&env));
COPTMEX_CALL(COPT_CreateProb(env, &prob));

// Generate default parameters
if (nlhs == 1)
{
COPTMEX_CALL(COPTMEX_getDefaultParams(prob, &plhs[0]));
}

exit_cleanup:
if (retcode != COPT_RETCODE_OK)
{
char errmsg[COPT_BUFFSIZE];
char msgbuf[COPT_BUFFSIZE * 2];
COPT_GetRetcodeMsg(retcode, errmsg, COPT_BUFFSIZE);
snprintf(msgbuf, COPT_BUFFSIZE * 2, "COPT Error %d: %s", retcode, errmsg);
COPTMEX_errorMsg(COPTMEX_ERROR_BAD_API, msgbuf);
}

// Delete COPT problem and environment
COPT_DeleteProb(&prob);
COPT_DeleteEnv(&env);

return;
}

0 comments on commit 40b0dd9

Please sign in to comment.