Skip to content

Commit

Permalink
Added rho adaptation based on time
Browse files Browse the repository at this point in the history
  • Loading branch information
bstellato committed Oct 21, 2017
1 parent 4e46f41 commit 278d2e7
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
8 changes: 8 additions & 0 deletions configure/glob_opts.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,23 @@ typedef float c_float; /* for numerical values */
#define c_min(a, b) (((a) < (b)) ? (a) : (b))
#endif

// Round x to the nearest multiple of N
#ifndef c_roundmultiple
#define c_roundmultiple(x, N) ((x) + .5 * (N) - c_fmod((x) + .5 * (N), (N)))
#endif


/* Use customized functions ----------------------------------------------- */

#if EMBEDDED != 1

#include <math.h>
#ifndef DFLOAT // Doubles
#define c_sqrt sqrt
#define c_fmod fmod
#else // Floats
#define c_sqrt sqrtf
#define c_fmod fmodf
#endif

#endif // end EMBEDDED
Expand Down
1 change: 1 addition & 0 deletions include/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ static const char *LINSYS_SOLVER_NAME[] = {

#define ADAPTIVE_RHO (1)
#define ADAPTIVE_RHO_INTERVAL (0)
#define ADAPTIVE_RHO_AUTO_INTERVAL_PERCENTAGE (0.7) ///< Percentage of setup time after which we update rho
#endif

/* Printing */
Expand Down
2 changes: 1 addition & 1 deletion interfaces/python/modulepurepy/_osqp.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ def print_polish(self):
"""
Print polish information
"""
print("plsh %11.4e %8.2e %8.2e --- %8.2es" %
print("plsh %11.4e %8.2e %8.2e -------- %8.2es" %
(self.work.info.obj_val,
self.work.info.pri_res,
self.work.info.dua_res,
Expand Down
10 changes: 5 additions & 5 deletions interfaces/python/tests/general/simple_prob.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import mathprogbasepy as mpbpy
sp.random.seed(2)

n = 100
m = 500
n = 500
m = 1000
A = sparse.random(m, n, density=0.5,
data_rvs=np.random.randn,
format='csc')
Expand Down Expand Up @@ -46,11 +46,11 @@

osqp_opts = {'rho': rho,
'adaptive_rho': True,
'adaptive_rho_interval': 100,
'adaptive_rho_interval': 0,
'sigma': 1e-06,
'scaled_termination': False,
'check_termination': 1,
'polish': True,
'check_termination': 25,
'polish': False,
'verbose': True,
'linsys_solver': 'suitesparse ldl'
}
Expand Down
26 changes: 26 additions & 0 deletions src/osqp.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,32 @@ c_int osqp_solve(OSQPWorkspace * work){


#if EMBEDDED != 1
// If adaptive rho with automatic interval, check if the solve time is a certain percentage
// of the setup time.
if (work->settings->adaptive_rho && !work->settings->adaptive_rho_interval){
// Check time
if (toc(work->timer) > ADAPTIVE_RHO_AUTO_INTERVAL_PERCENTAGE * work->info->setup_time){
// Enough time has passed. We round the number of iterations to the
// closest multiple of check_termination
//
work->settings->adaptive_rho_interval = (c_int)c_roundmultiple(iter, work->settings->check_termination);
// Make sure the interval is not 0 and at least check_termination times
work->settings->adaptive_rho_interval = c_max(work->settings->adaptive_rho_interval, work->settings->check_termination);

// #ifdef PRINTING
// // DEBUG: print stuff
// c_print("time %.2e, iter %i, check_termination %i, new_interval %i\n",
// toc(work->timer), iter,
// work->settings->check_termination,
// work->settings->adaptive_rho_interval);
// #endif

}

}



// Adapt rho
if (work->settings->adaptive_rho &&
work->settings->adaptive_rho_interval &&
Expand Down
2 changes: 1 addition & 1 deletion src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void print_polish(OSQPWorkspace * work) {
c_print(" %11.4e", info->obj_val);
c_print(" %8.2e", info->pri_res);
c_print(" %8.2e", info->dua_res);
c_print(" --- ");
c_print(" --------");
#ifdef PROFILING
c_print(" %8.2es", info->setup_time + info->solve_time +
info->polish_time);
Expand Down

0 comments on commit 278d2e7

Please sign in to comment.