Skip to content

Commit 5160b25

Browse files
committed
Merge branch 'fix-lp-exact' into 'master'
Fix lp writer See merge request integer/scip!3817
2 parents c99b961 + c2a6218 commit 5160b25

File tree

1 file changed

+197
-108
lines changed

1 file changed

+197
-108
lines changed

src/scip/reader_lp.c

+197-108
Original file line numberDiff line numberDiff line change
@@ -3467,84 +3467,6 @@ void appendLine(
34673467
endLine(scip, file, linebuffer, linecnt);
34683468
}
34693469

3470-
/* print row in LP format to file stream */
3471-
static
3472-
SCIP_RETCODE printRowExact(
3473-
SCIP* scip, /**< SCIP data structure */
3474-
FILE* file, /**< output file (or NULL for standard output) */
3475-
const char* rowname, /**< row name */
3476-
const char* rownameextension, /**< row name extension */
3477-
const char* type, /**< row type ("=", "<=", or ">=") */
3478-
SCIP_VAR** linvars, /**< array of linear variables */
3479-
SCIP_RATIONAL** linvals, /**< array of linear coefficient values */
3480-
int nlinvars, /**< number of linear variables */
3481-
SCIP_RATIONAL* rhs /**< right hand side */
3482-
)
3483-
{
3484-
int v;
3485-
char linebuffer[LP_MAX_PRINTLEN+1] = { '\0' };
3486-
char ratbuffer[LP_MAX_PRINTLEN+1] = { '\0' };
3487-
int linecnt;
3488-
3489-
char varname[LP_MAX_NAMELEN];
3490-
char consname[LP_MAX_NAMELEN + 1]; /* an extra character for ':' */
3491-
char buffer[LP_MAX_PRINTLEN];
3492-
3493-
assert( scip != NULL );
3494-
assert( strcmp(type, "=") == 0 || strcmp(type, "<=") == 0 || strcmp(type, ">=") == 0 );
3495-
assert( nlinvars == 0 || (linvars != NULL && linvals != NULL) );
3496-
3497-
clearLine(linebuffer, &linecnt);
3498-
3499-
/* start each line with a space */
3500-
appendLine(scip, file, linebuffer, &linecnt, " ");
3501-
3502-
/* print row name */
3503-
if( strlen(rowname) > 0 || strlen(rownameextension) > 0 )
3504-
{
3505-
(void) SCIPsnprintf(consname, LP_MAX_NAMELEN + 1, "%s%s:", rowname, rownameextension);
3506-
appendLine(scip, file, linebuffer, &linecnt, consname);
3507-
}
3508-
3509-
/* print coefficients */
3510-
for( v = 0; v < nlinvars; ++v )
3511-
{
3512-
SCIP_VAR* var;
3513-
3514-
assert(linvars != NULL); /* for lint */
3515-
assert(linvals != NULL);
3516-
3517-
var = linvars[v];
3518-
assert( var != NULL );
3519-
3520-
/* we start a new line; therefore we tab this line */
3521-
if( linecnt == 0 )
3522-
appendLine(scip, file, linebuffer, &linecnt, " ");
3523-
3524-
(void) SCIPsnprintf(varname, LP_MAX_NAMELEN, "%s", SCIPvarGetName(var));
3525-
(void) SCIPrationalToString(linvals[v], ratbuffer, LP_MAX_PRINTLEN);
3526-
if( !SCIPrationalIsNegative(linvals[v]) )
3527-
(void) SCIPsnprintf(buffer, LP_MAX_PRINTLEN, "+%s %s", ratbuffer, varname);
3528-
else
3529-
(void) SCIPsnprintf(buffer, LP_MAX_PRINTLEN, "%s %s", ratbuffer, varname);
3530-
3531-
appendLine(scip, file, linebuffer, &linecnt, buffer);
3532-
}
3533-
3534-
(void) SCIPrationalToString(rhs, ratbuffer, LP_MAX_PRINTLEN);
3535-
(void) SCIPsnprintf(buffer, LP_MAX_PRINTLEN, " %s %s", type, ratbuffer);
3536-
3537-
/* we start a new line; therefore we tab this line */
3538-
if( linecnt == 0 )
3539-
appendLine(scip, file, linebuffer, &linecnt, " ");
3540-
appendLine(scip, file, linebuffer, &linecnt, buffer);
3541-
3542-
endLine(scip, file, linebuffer, &linecnt);
3543-
3544-
return SCIP_OKAY;
3545-
}
3546-
3547-
35483470
/* print row in LP format to file stream */
35493471
static
35503472
SCIP_RETCODE printRow(
@@ -3561,18 +3483,18 @@ SCIP_RETCODE printRow(
35613483
SCIP_Bool transformed /**< transformed constraint? */
35623484
)
35633485
{
3564-
int v;
3565-
char linebuffer[LP_MAX_PRINTLEN+1] = { '\0' };
3566-
int linecnt;
3567-
3486+
char linebuffer[LP_MAX_PRINTLEN + 1] = { '\0' };
35683487
char varname[LP_MAX_NAMELEN];
35693488
char varname2[LP_MAX_NAMELEN];
35703489
char consname[LP_MAX_NAMELEN + 1]; /* an extra character for ':' */
35713490
char buffer[LP_MAX_PRINTLEN];
3491+
int linecnt;
3492+
int written;
3493+
int v;
35723494

3573-
assert( scip != NULL );
3574-
assert( strcmp(type, "=") == 0 || strcmp(type, "<=") == 0 || strcmp(type, ">=") == 0 );
3575-
assert( nlinvars == 0 || (linvars != NULL && linvals != NULL) );
3495+
assert(scip != NULL);
3496+
assert(strcmp(type, "=") == 0 || strcmp(type, "<=") == 0 || strcmp(type, ">=") == 0);
3497+
assert(nlinvars == 0 || (linvars != NULL && linvals != NULL));
35763498

35773499
clearLine(linebuffer, &linecnt);
35783500

@@ -3582,7 +3504,12 @@ SCIP_RETCODE printRow(
35823504
/* print row name */
35833505
if( strlen(rowname) > 0 || strlen(rownameextension) > 0 )
35843506
{
3585-
(void) SCIPsnprintf(consname, LP_MAX_NAMELEN + 1, "%s%s:", rowname, rownameextension);
3507+
written = SCIPsnprintf(consname, LP_MAX_NAMELEN + 1, "%s%s:", rowname, rownameextension);
3508+
if( written > LP_MAX_NAMELEN )
3509+
{
3510+
SCIPerrorMessage("buffer length exceeded\n");
3511+
return SCIP_INVALIDDATA;
3512+
}
35863513
appendLine(scip, file, linebuffer, &linecnt, consname);
35873514
}
35883515

@@ -3595,15 +3522,24 @@ SCIP_RETCODE printRow(
35953522
assert(linvals != NULL);
35963523

35973524
var = linvars[v];
3598-
assert( var != NULL );
3525+
assert(var != NULL);
35993526

36003527
/* we start a new line; therefore we tab this line */
36013528
if( linecnt == 0 )
36023529
appendLine(scip, file, linebuffer, &linecnt, " ");
36033530

3604-
(void) SCIPsnprintf(varname, LP_MAX_NAMELEN, "%s", SCIPvarGetName(var));
3605-
(void) SCIPsnprintf(buffer, LP_MAX_PRINTLEN, " %+.15g %s", linvals[v], varname);
3606-
3531+
written = SCIPsnprintf(varname, LP_MAX_NAMELEN, "%s", SCIPvarGetName(var));
3532+
if( written >= LP_MAX_NAMELEN )
3533+
{
3534+
SCIPerrorMessage("buffer length exceeded\n");
3535+
return SCIP_INVALIDDATA;
3536+
}
3537+
written = SCIPsnprintf(buffer, LP_MAX_PRINTLEN, " %+.15g %s", linvals[v], varname);
3538+
if( written >= LP_MAX_PRINTLEN )
3539+
{
3540+
SCIPerrorMessage("buffer length exceeded\n");
3541+
return SCIP_INVALIDDATA;
3542+
}
36073543
appendLine(scip, file, linebuffer, &linecnt, buffer);
36083544
}
36093545

@@ -3652,15 +3588,24 @@ SCIP_RETCODE printRow(
36523588
assert(activevals != NULL);
36533589

36543590
var = activevars[v];
3655-
assert( var != NULL );
3591+
assert(var != NULL);
36563592

36573593
/* we start a new line; therefore we tab this line */
36583594
if( linecnt == 0 )
36593595
appendLine(scip, file, linebuffer, &linecnt, " ");
36603596

3661-
(void) SCIPsnprintf(varname, LP_MAX_NAMELEN, "%s", SCIPvarGetName(var));
3662-
(void) SCIPsnprintf(buffer, LP_MAX_PRINTLEN, " %+.15g %s", activevals[v], varname);
3663-
3597+
written = SCIPsnprintf(varname, LP_MAX_NAMELEN, "%s", SCIPvarGetName(var));
3598+
if( written >= LP_MAX_NAMELEN )
3599+
{
3600+
SCIPerrorMessage("buffer length exceeded\n");
3601+
return SCIP_INVALIDDATA;
3602+
}
3603+
written = SCIPsnprintf(buffer, LP_MAX_PRINTLEN, " %+.15g %s", activevals[v], varname);
3604+
if( written >= LP_MAX_PRINTLEN )
3605+
{
3606+
SCIPerrorMessage("buffer length exceeded\n");
3607+
return SCIP_INVALIDDATA;
3608+
}
36643609
appendLine(scip, file, linebuffer, &linecnt, buffer);
36653610
}
36663611

@@ -3694,9 +3639,18 @@ SCIP_RETCODE printRow(
36943639
if( linecnt == 0 )
36953640
appendLine(scip, file, linebuffer, &linecnt, " ");
36963641

3697-
(void) SCIPsnprintf(varname, LP_MAX_NAMELEN, "%s", SCIPvarGetName(var));
3698-
(void) SCIPsnprintf(buffer, LP_MAX_PRINTLEN, " %+.15g %s", lincoef, varname);
3699-
3642+
written = SCIPsnprintf(varname, LP_MAX_NAMELEN, "%s", SCIPvarGetName(var));
3643+
if( written >= LP_MAX_NAMELEN )
3644+
{
3645+
SCIPerrorMessage("buffer length exceeded\n");
3646+
return SCIP_INVALIDDATA;
3647+
}
3648+
written = SCIPsnprintf(buffer, LP_MAX_PRINTLEN, " %+.15g %s", lincoef, varname);
3649+
if( written >= LP_MAX_PRINTLEN )
3650+
{
3651+
SCIPerrorMessage("buffer length exceeded\n");
3652+
return SCIP_INVALIDDATA;
3653+
}
37003654
appendLine(scip, file, linebuffer, &linecnt, buffer);
37013655
}
37023656

@@ -3725,9 +3679,18 @@ SCIP_RETCODE printRow(
37253679
if( linecnt == 0 )
37263680
appendLine(scip, file, linebuffer, &linecnt, " ");
37273681

3728-
(void) SCIPsnprintf(varname, LP_MAX_NAMELEN, "%s", SCIPvarGetName(var));
3729-
(void) SCIPsnprintf(buffer, LP_MAX_PRINTLEN, " %+.15g %s^2", sqrcoef, varname);
3730-
3682+
written = SCIPsnprintf(varname, LP_MAX_NAMELEN, "%s", SCIPvarGetName(var));
3683+
if( written >= LP_MAX_NAMELEN )
3684+
{
3685+
SCIPerrorMessage("buffer length exceeded\n");
3686+
return SCIP_INVALIDDATA;
3687+
}
3688+
written = SCIPsnprintf(buffer, LP_MAX_PRINTLEN, " %+.15g %s^2", sqrcoef, varname);
3689+
if( written >= LP_MAX_PRINTLEN )
3690+
{
3691+
SCIPerrorMessage("buffer length exceeded\n");
3692+
return SCIP_INVALIDDATA;
3693+
}
37313694
appendLine(scip, file, linebuffer, &linecnt, buffer);
37323695
}
37333696

@@ -3756,22 +3719,148 @@ SCIP_RETCODE printRow(
37563719
if( linecnt == 0 )
37573720
appendLine(scip, file, linebuffer, &linecnt, " ");
37583721

3759-
(void) SCIPsnprintf(varname, LP_MAX_NAMELEN, "%s", SCIPvarGetName(var1));
3760-
(void) SCIPsnprintf(varname2, LP_MAX_NAMELEN, "%s", SCIPvarGetName(var2));
3761-
(void) SCIPsnprintf(buffer, LP_MAX_PRINTLEN, " %+.15g %s * %s", bilincoef, varname, varname2);
3762-
3722+
written = SCIPsnprintf(varname, LP_MAX_NAMELEN, "%s", SCIPvarGetName(var1));
3723+
if( written >= LP_MAX_NAMELEN )
3724+
{
3725+
SCIPerrorMessage("buffer length exceeded\n");
3726+
return SCIP_INVALIDDATA;
3727+
}
3728+
written = SCIPsnprintf(varname2, LP_MAX_NAMELEN, "%s", SCIPvarGetName(var2));
3729+
if( written >= LP_MAX_NAMELEN )
3730+
{
3731+
SCIPerrorMessage("buffer length exceeded\n");
3732+
return SCIP_INVALIDDATA;
3733+
}
3734+
written = SCIPsnprintf(buffer, LP_MAX_PRINTLEN, " %+.15g %s * %s", bilincoef, varname, varname2);
3735+
if( written >= LP_MAX_PRINTLEN )
3736+
{
3737+
SCIPerrorMessage("buffer length exceeded\n");
3738+
return SCIP_INVALIDDATA;
3739+
}
37633740
appendLine(scip, file, linebuffer, &linecnt, buffer);
37643741
}
37653742

37663743
/* end quadratic part */
37673744
appendLine(scip, file, linebuffer, &linecnt, " ]");
37683745
}
37693746

3770-
/* print left hand side */
3771-
if( SCIPisZero(scip, rhs) )
3772-
rhs = 0.0;
3747+
/* print right hand side */
3748+
written = SCIPsnprintf(buffer, LP_MAX_PRINTLEN, " %s %+.15g", type, rhs);
3749+
if( written >= LP_MAX_PRINTLEN )
3750+
{
3751+
SCIPerrorMessage("buffer length exceeded\n");
3752+
return SCIP_INVALIDDATA;
3753+
}
3754+
3755+
/* we start a new line; therefore we tab this line */
3756+
if( linecnt == 0 )
3757+
appendLine(scip, file, linebuffer, &linecnt, " ");
3758+
appendLine(scip, file, linebuffer, &linecnt, buffer);
3759+
3760+
endLine(scip, file, linebuffer, &linecnt);
3761+
3762+
return SCIP_OKAY;
3763+
}
3764+
3765+
/* print exact row in LP format to file stream */
3766+
static
3767+
SCIP_RETCODE printRowExact(
3768+
SCIP* scip, /**< SCIP data structure */
3769+
FILE* file, /**< output file (or NULL for standard output) */
3770+
const char* rowname, /**< row name */
3771+
const char* rownameextension, /**< row name extension */
3772+
const char* type, /**< row type ("=", "<=", or ">=") */
3773+
SCIP_VAR** linvars, /**< array of linear variables */
3774+
SCIP_RATIONAL** linvals, /**< array of linear coefficient values */
3775+
int nlinvars, /**< number of linear variables */
3776+
SCIP_RATIONAL* rhs /**< right hand side */
3777+
)
3778+
{
3779+
char linebuffer[LP_MAX_PRINTLEN + 1] = { '\0' };
3780+
char ratbuffer[LP_MAX_PRINTLEN] = { '\0' };
3781+
char varname[LP_MAX_NAMELEN];
3782+
char consname[LP_MAX_NAMELEN + 1]; /* an extra character for ':' */
3783+
char buffer[LP_MAX_PRINTLEN];
3784+
int linecnt;
3785+
int written;
3786+
int v;
3787+
3788+
assert(scip != NULL);
3789+
assert(strcmp(type, "=") == 0 || strcmp(type, "<=") == 0 || strcmp(type, ">=") == 0);
3790+
assert(nlinvars == 0 || (linvars != NULL && linvals != NULL));
3791+
3792+
clearLine(linebuffer, &linecnt);
3793+
3794+
/* start each line with a space */
3795+
appendLine(scip, file, linebuffer, &linecnt, " ");
3796+
3797+
/* print row name */
3798+
if( strlen(rowname) > 0 || strlen(rownameextension) > 0 )
3799+
{
3800+
written = SCIPsnprintf(consname, LP_MAX_NAMELEN + 1, "%s%s:", rowname, rownameextension);
3801+
if( written > LP_MAX_NAMELEN )
3802+
{
3803+
SCIPerrorMessage("buffer length exceeded\n");
3804+
return SCIP_INVALIDDATA;
3805+
}
3806+
appendLine(scip, file, linebuffer, &linecnt, consname);
3807+
}
3808+
3809+
/* print coefficients */
3810+
for( v = 0; v < nlinvars; ++v )
3811+
{
3812+
SCIP_VAR* var;
3813+
3814+
assert(linvars != NULL); /* for lint */
3815+
assert(linvals != NULL);
3816+
3817+
var = linvars[v];
3818+
assert(var != NULL);
37733819

3774-
(void) SCIPsnprintf(buffer, LP_MAX_PRINTLEN, " %s %+.15g", type, rhs);
3820+
/* we start a new line; therefore we tab this line */
3821+
if( linecnt == 0 )
3822+
appendLine(scip, file, linebuffer, &linecnt, " ");
3823+
3824+
written = SCIPsnprintf(varname, LP_MAX_NAMELEN, "%s", SCIPvarGetName(var));
3825+
if( written >= LP_MAX_NAMELEN )
3826+
{
3827+
SCIPerrorMessage("buffer length exceeded\n");
3828+
return SCIP_INVALIDDATA;
3829+
}
3830+
written = SCIPrationalToString(linvals[v], ratbuffer, LP_MAX_PRINTLEN);
3831+
if( written >= LP_MAX_PRINTLEN )
3832+
{
3833+
SCIPerrorMessage("buffer length exceeded\n");
3834+
return SCIP_INVALIDDATA;
3835+
}
3836+
if( !SCIPrationalIsNegative(linvals[v]) )
3837+
written = SCIPsnprintf(buffer, LP_MAX_PRINTLEN, " +%s %s", ratbuffer, varname);
3838+
else
3839+
written = SCIPsnprintf(buffer, LP_MAX_PRINTLEN, " %s %s", ratbuffer, varname);
3840+
if( written >= LP_MAX_PRINTLEN )
3841+
{
3842+
SCIPerrorMessage("buffer length exceeded\n");
3843+
return SCIP_INVALIDDATA;
3844+
}
3845+
appendLine(scip, file, linebuffer, &linecnt, buffer);
3846+
}
3847+
3848+
/* print right hand side */
3849+
written = SCIPrationalToString(rhs, ratbuffer, LP_MAX_PRINTLEN);
3850+
if( written >= LP_MAX_PRINTLEN )
3851+
{
3852+
SCIPerrorMessage("buffer length exceeded\n");
3853+
return SCIP_INVALIDDATA;
3854+
}
3855+
if( !SCIPrationalIsNegative(rhs) )
3856+
written = SCIPsnprintf(buffer, LP_MAX_PRINTLEN, " %s +%s", type, ratbuffer);
3857+
else
3858+
written = SCIPsnprintf(buffer, LP_MAX_PRINTLEN, " %s %s", type, ratbuffer);
3859+
if( written >= LP_MAX_PRINTLEN )
3860+
{
3861+
SCIPerrorMessage("buffer length exceeded\n");
3862+
return SCIP_INVALIDDATA;
3863+
}
37753864

37763865
/* we start a new line; therefore we tab this line */
37773866
if( linecnt == 0 )

0 commit comments

Comments
 (0)