Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions include/PSLP/PSLP_API.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,12 @@ extern "C"
for a variable at its lower bound, z <= 0 at its upper bound, y >= 0 for
a row active at its lhs and y <= 0 at its rhs. The function populates
presolver->sol, so if you're looking for the solution to the original
problem, you want to look there. */
void postsolve(Presolver *presolver, const double *x, const double *y,
problem, you want to look there.

Returns true on success. Returns false if memory runs out; presolver->sol
then does not hold the solution. With 'verbose' set, the reason for a
failure is printed (this holds for all functions below as well). */
bool postsolve(Presolver *presolver, const double *x, const double *y,
const double *z);

/* Map a primal-dual point (x, y) of the original problem to the reduced
Expand All @@ -229,17 +233,21 @@ extern "C"
x has length n and y has length m (original dimensions). On return, x_red
(length reduced_prob->n) and y_red (length reduced_prob->m) hold the
mapped point and z_red (length reduced_prob->n) holds z_red = c_red -
A_red^T y_red. Any output pointer may be NULL to skip that part; x_red
requires x, y_red requires y, and z_red requires y_red. The function does
not modify presolver->sol.
A_red^T y_red. Any output pointer may be NULL to skip that part. The
caller must pass x when x_red is requested, y when y_red is requested,
and y_red when z_red is requested. The function does not modify
presolver->sol.

The semantics are as follows. If (x, y) is an optimal primal-dual point
of the original problem and 'relax_bounds' is false, (x_red, y_red,
z_red) is an optimal primal-dual point of the reduced problem.

This function is currently only implemented for LPs. For a QP it prints
an error and leaves x_red, y_red and z_red untouched. */
void map_original_sol_to_reduced(Presolver *presolver, const double *x,
Returns true on success and false otherwise. It fails
* for a QP (currently only implemented for LPs) or if memory runs out;
x_red, y_red and z_red are then left untouched;
* if z_red is requested after 'free_presolver_reduced_problem'; x_red
and y_red are then still computed and z_red is left untouched. */
bool map_original_sol_to_reduced(Presolver *presolver, const double *x,
const double *y, double *x_red, double *y_red,
double *z_red);

Expand All @@ -248,10 +256,13 @@ extern "C"
to y_orig. It does not check whether y is a valid ray.

y uses the Farkas sign convention, yi >= 0 when row i is active at its
rhs, the opposite of postsolve(). It matches Gurobi's FarkasDual. Not
available yet for a problem with cone blocks: an error is printed and y_orig
is left untouched. */
void postsolve_primal_infeas_ray(Presolver *presolver, const double *y,
rhs, the opposite of postsolve(). It matches Gurobi's FarkasDual.

Returns true on success. Returns false, and leaves y_orig untouched, for
a problem with cone blocks or with 'lp_fold' applied (not available
yet), if called after 'free_presolver_reduced_problem', or if memory
runs out. */
bool postsolve_primal_infeas_ray(Presolver *presolver, const double *y,
double *y_orig);

/* Postsolve a dual infeasibility ray x of the reduced problem.
Expand All @@ -260,9 +271,12 @@ extern "C"

x is an unbounded direction: a recession direction of the constraint
set with Px = 0 and c'x < 0 (for an LP simply c'x < 0); no sign
convention involved. Not available yet for a problem with cone
blocks: an error is printed and x_orig is left untouched. */
void postsolve_dual_infeas_ray(Presolver *presolver, const double *x,
convention involved.

Returns true on success. Returns false, and leaves x_orig untouched, for
a problem with cone blocks or with 'lp_fold' applied (not available
yet), or if memory runs out. */
bool postsolve_dual_infeas_ray(Presolver *presolver, const double *x,
double *x_orig);

#ifdef __cplusplus
Expand Down
8 changes: 8 additions & 0 deletions include/core/glbopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define GLB_H_GUARD

#include <math.h>
#include <stdio.h>
#define INF (INFINITY)
#define IS_POS_INF(x) (isinf(x) && (x) > 0)
#define IS_NEG_INF(x) (isinf(x) && (x) < 0)
Expand All @@ -32,6 +33,13 @@
#define RETURN_IF_NOT_UNCHANGED(x) \
if ((x) != UNCHANGED) return x

/* Reports why a public call failed. Silent unless 'verbose' is set. */
#define PSLP_FAIL_MSG(stgs, msg) \
do \
{ \
if ((stgs)->verbose) printf("PSLP error: " msg "\n"); \
} while (0)

#define SIZE_INACTIVE_ROW -1
#define SIZE_INACTIVE_COL -1

Expand Down
56 changes: 19 additions & 37 deletions src/core/MapToReduced.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "Workspace.h"
#include "dVec.h"
#include "fold_postsolve.h"
#include "glbopts.h"
#include "iVec.h"
#include "u16Vec.h"
#include <assert.h>
Expand Down Expand Up @@ -215,7 +216,7 @@ static void compute_reduced_dual_slack(double *z, const ProblemData *prob,
}
}

void map_original_sol_to_reduced(Presolver *presolver, const double *x,
bool map_original_sol_to_reduced(Presolver *presolver, const double *x,
const double *y, double *x_red, double *y_red,
double *z_red)
{
Expand All @@ -234,38 +235,19 @@ void map_original_sol_to_reduced(Presolver *presolver, const double *x,
/* 'stats' survives 'free_presolver_reduced_problem', unlike the reduced P */
if (presolver->stats->Pnnz_original > 0 || presolver->stats->Pnnz_reduced > 0)
{
fprintf(stderr, "PSLP error: map_original_sol_to_reduced is not supported "
"for QPs yet! Outputs are left untouched. \n");
return;
PSLP_FAIL_MSG(presolver->stgs, "map_original_sol_to_reduced is not "
"supported for QPs yet");
return false;
}

if (x_red && !x)
{
fprintf(stderr,
"PSLP warning: map_original_sol_to_reduced needs x to compute "
"x_red! x_red is left untouched. \n");
x_red = NULL;
}

if (y_red && !y)
{
fprintf(stderr,
"PSLP warning: map_original_sol_to_reduced needs y to compute "
"y_red! y_red and z_red are left untouched. \n");
y_red = NULL;
z_red = NULL;
}

if (z_red && !y_red)
{
fprintf(stderr, "PSLP warning: map_original_sol_to_reduced needs y_red to "
"compute z_red! z_red is left untouched. \n");
z_red = NULL;
}
/* an output needs its input */
assert(!x_red || x);
assert(!y_red || y);
assert(!z_red || y_red);

if (!x_red && !y_red)
{
return;
return true;
}

// scratch space for the replay and, with a fold, for the folded point (we
Expand All @@ -274,10 +256,9 @@ void map_original_sol_to_reduced(Presolver *presolver, const double *x,
work = (double *) ps_malloc(n_fold * (fold_maps ? 2 : 1) + 1, sizeof(double));
if (!work)
{
fprintf(stderr,
"PSLP warning: map_original_sol_to_reduced failed to allocate "
"memory! Outputs are left untouched. \n");
return;
PSLP_FAIL_MSG(presolver->stgs,
"out of memory in map_original_sol_to_reduced");
return false;
}

if (fold_maps)
Expand Down Expand Up @@ -310,18 +291,19 @@ void map_original_sol_to_reduced(Presolver *presolver, const double *x,

if (!z_red)
{
return;
return true;
}

if (!reduced_prob->Ax)
{
// The reduced problem data was released by
// 'free_presolver_reduced_problem', so z_red cannot be computed.
fprintf(stderr, "PSLP warning: map_original_sol_to_reduced called after "
"free_presolver_reduced_problem! z_red is left "
"untouched. \n");
return;
PSLP_FAIL_MSG(presolver->stgs,
"map_original_sol_to_reduced cannot compute z_red after "
"free_presolver_reduced_problem");
return false;
}

compute_reduced_dual_slack(z_red, reduced_prob, y_red);
return true;
}
73 changes: 42 additions & 31 deletions src/core/Presolver.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ Presolver *new_presolver_from_problem(const ProblemData *problem,
static inline bool update_termination(size_t nnz_after_cycle,
size_t nnz_before_cycle, Complexity complexity,
PresolveStatus status, double max_time,
Timer outer_timer)
Timer outer_timer, bool verbose)
{
if (HAS_STATUS(status, UNBNDORINFEAS))
{
Expand All @@ -535,7 +535,10 @@ static inline bool update_termination(size_t nnz_after_cycle,

if (GET_ELAPSED_SECONDS(outer_timer) >= max_time)
{
printf("Maximum time limit of %.2f seconds reached.\n", max_time);
if (verbose)
{
printf("Maximum time limit of %.2f seconds reached.\n", max_time);
}
return true;
}

Expand Down Expand Up @@ -941,7 +944,7 @@ PresolveStatus run_presolver(Presolver *presolver)

terminate =
update_termination(nnz_after_cycle, nnz_before_cycle, curr_complexity,
status, stgs->max_time, outer_timer);
status, stgs->max_time, outer_timer, stgs->verbose);

if (curr_complexity == MEDIUM)
{
Expand Down Expand Up @@ -1044,8 +1047,8 @@ void free_presolver_reduced_problem(Presolver *presolver)
free_reduced_problem_arrays(reduced_prob);
}

/* Allocates presolver->sol on the first postsolve call. Returns false (and
prints an error) if the allocation fails. */
/* Allocates presolver->sol on the first postsolve call. Returns false if the
allocation fails. */
static bool ensure_solution(Presolver *presolver)
{
if (presolver->sol)
Expand All @@ -1057,21 +1060,21 @@ static bool ensure_solution(Presolver *presolver)
presolver->stats->n_cols_original);
if (!presolver->sol)
{
fprintf(stderr, "PSLP error: out of memory when allocating the "
"postsolved solution. Outputs are left untouched. \n");
PSLP_FAIL_MSG(presolver->stgs, "out of memory when allocating the "
"postsolved solution");
return false;
}
return true;
}

void postsolve(Presolver *presolver, const double *x, const double *y,
bool postsolve(Presolver *presolver, const double *x, const double *y,
const double *z)
{
Timer timer;
PresolveStats *stats = presolver->stats;
if (!ensure_solution(presolver))
{
return;
return false;
}
Solution *sol = presolver->sol;
State *data = presolver->prob->constraints->state;
Expand All @@ -1096,6 +1099,7 @@ void postsolve(Presolver *presolver, const double *x, const double *y,
{
printf("PSLP postsolve time: %.4f seconds\n", stats->time_postsolve);
}
return true;
}

static void compute_primal_infeas_ray_z(double *z, const double *Ax, const int *Ai,
Expand All @@ -1116,7 +1120,7 @@ static void compute_primal_infeas_ray_z(double *z, const double *Ax, const int *
}
}

void postsolve_primal_infeas_ray(Presolver *presolver, const double *y,
bool postsolve_primal_infeas_ray(Presolver *presolver, const double *y,
double *y_orig)
{
Timer timer;
Expand All @@ -1129,32 +1133,37 @@ void postsolve_primal_infeas_ray(Presolver *presolver, const double *y,
assert(reduced_prob != NULL);
if (data->work->fold_maps)
{
fprintf(stderr, "PSLP error: postsolve_primal_infeas_ray is not supported "
"with lp_fold yet! y_orig is left untouched. \n");
return;
PSLP_FAIL_MSG(presolver->stgs, "postsolve_primal_infeas_ray is not "
"supported with lp_fold yet");
return false;
}
if (stats->n_cones_original > 0)
{
fprintf(stderr, "PSLP error: postsolve_primal_infeas_ray is not supported "
"for problems with cone blocks yet! y_orig is left "
"untouched. \n");
return;
PSLP_FAIL_MSG(presolver->stgs,
"postsolve_primal_infeas_ray is not "
"supported for problems with cone blocks yet");
return false;
}
if (!reduced_prob || !reduced_prob->Ax)
{
// The reduced constraint matrix was released by
// 'free_presolver_reduced_problem', so the ray cannot be recovered.
fprintf(stderr, "PSLP warning: postsolve_primal_infeas_ray called after "
"free_presolver_reduced_problem! Don't do this! \n");
return;
PSLP_FAIL_MSG(presolver->stgs, "postsolve_primal_infeas_ray called after "
"free_presolver_reduced_problem");
return false;
}
if (!ensure_solution(presolver))
{
return;
return false;
}
Solution *sol = presolver->sol;
z = (double *) ps_malloc(len_z, sizeof(double));
assert(z != NULL);
if (!z)
{
PSLP_FAIL_MSG(presolver->stgs,
"out of memory in postsolve_primal_infeas_ray");
return false;
}

clock_gettime(CLOCK_MONOTONIC, &timer.start);
compute_primal_infeas_ray_z(z, reduced_prob->Ax, reduced_prob->Ai,
Expand All @@ -1175,9 +1184,10 @@ void postsolve_primal_infeas_ray(Presolver *presolver, const double *y,
{
printf("PSLP postsolve time: %.4f seconds\n", stats->time_postsolve);
}
return true;
}

void postsolve_dual_infeas_ray(Presolver *presolver, const double *x, double *x_orig)
bool postsolve_dual_infeas_ray(Presolver *presolver, const double *x, double *x_orig)
{
Timer timer;
PresolveStats *stats = presolver->stats;
Expand All @@ -1186,20 +1196,20 @@ void postsolve_dual_infeas_ray(Presolver *presolver, const double *x, double *x_
assert(presolver->reduced_prob != NULL);
if (data->work->fold_maps)
{
fprintf(stderr, "PSLP error: postsolve_dual_infeas_ray is not supported "
"with lp_fold yet! x_orig is left untouched. \n");
return;
PSLP_FAIL_MSG(presolver->stgs, "postsolve_dual_infeas_ray is not "
"supported with lp_fold yet");
return false;
}
if (stats->n_cones_original > 0)
{
fprintf(stderr, "PSLP error: postsolve_dual_infeas_ray is not supported "
"for problems with cone blocks yet! x_orig is left "
"untouched. \n");
return;
PSLP_FAIL_MSG(presolver->stgs,
"postsolve_dual_infeas_ray is not "
"supported for problems with cone blocks yet");
return false;
}
if (!ensure_solution(presolver))
{
return;
return false;
}
Solution *sol = presolver->sol;

Expand All @@ -1218,6 +1228,7 @@ void postsolve_dual_infeas_ray(Presolver *presolver, const double *x, double *x_
{
printf("PSLP postsolve time: %.4f seconds\n", stats->time_postsolve);
}
return true;
}

void free_presolver(Presolver *presolver)
Expand Down
Loading
Loading