Skip to content

Commit

Permalink
Changed parametric problems. Fixed csv export
Browse files Browse the repository at this point in the history
  • Loading branch information
bstellato committed Sep 9, 2019
1 parent ea9ce22 commit f0310d2
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 19 deletions.
3 changes: 2 additions & 1 deletion parametric_problems/lasso.py
Expand Up @@ -4,6 +4,7 @@
import numpy as np
import pandas as pd
import os
from solvers.solvers import SOLVER_MAP # AVOID CIRCULAR DEPENDENCY
from problem_classes.lasso import LassoExample
from utils.general import make_sure_path_exists
# import osqppurepy as osqp
Expand Down Expand Up @@ -131,7 +132,7 @@ def solve(self):

# Solve problem
r = m.solve()

# DEBUG
# print("Lambda = %.4e,\t niter = %d" % (lambda_val, r.info.iter))

Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 17 additions & 14 deletions run_parametric_problems.py
Expand Up @@ -5,12 +5,11 @@
'''

from utils.parametric import print_results_parametric

from parametric_problems.lasso import LassoParametric
from parametric_problems.mpc import MPCParametric
from parametric_problems.portfolio import PortfolioParametric

from utils.parametric import print_results_parametric, compute_results_parametric

PROBLEMS_MAP = {'Lasso': LassoParametric,
'MPC': MPCParametric,
Expand All @@ -23,9 +22,9 @@
]

# Problem dimensions
dimensions = {'Lasso': 50,
'MPC': 20,
'Portfolio': 100
dimensions = {'Lasso': [50, 100, 150, 200],
'MPC': [20, 40, 60, 80],
'Portfolio': [100, 200, 300, 400]
}

# OSQP solver settings
Expand All @@ -35,12 +34,16 @@

# Solve all problems
for problem in problems:
problem_instance = PROBLEMS_MAP[problem](osqp_settings,
dimensions[problem])
problem_instance.solve()

# Extract results info
print("Results")
print("-------")
for problem in problems:
print_results_parametric(problem, dimensions[problem])
for dim in dimensions[problem]:
problem_instance = PROBLEMS_MAP[problem](osqp_settings,
dim)
problem_instance.solve()

# Compute results
compute_results_parametric(problems, dimensions)

# # Extract results info
# print("Results")
# print("-------")
# for problem in problems:
# print_results_parametric(problem, dimensions[problem])
8 changes: 4 additions & 4 deletions utils/benchmark.py
Expand Up @@ -163,7 +163,7 @@ def compute_shifted_geometric_means(solvers, problems_type):
g_mean_file = os.path.join('.', 'results',
problems_type,
'geom_mean.csv')
df_g_mean.to_csv(g_mean_file, header=False, index=True)
df_g_mean.to_frame().transpose().to_csv(g_mean_file, index=False)


# r = {} # Dictionary of relative times for each solver/problem
Expand Down Expand Up @@ -224,7 +224,7 @@ def compute_failure_rates(solvers, problems_type):

# Write csv file
df_failure_rates = pd.Series(failure_rates)
df_failure_rates.to_csv(failure_rates_file, header=False, index=True)
df_failure_rates.to_frame().transpose().to_csv(failure_rates_file, index=False)


def compute_polish_statistics(problems_type, high_accuracy=False):
Expand Down Expand Up @@ -262,7 +262,7 @@ def compute_polish_statistics(problems_type, high_accuracy=False):
'percentage_of_success': (polish_success * 100)}

df_polish = pd.Series(polish_statistics)
df_polish.to_csv(polish_file, header=False, index=True)
df_polish.to_frame().transpose().to_csv(polish_file, index=False)


def compute_ratio_setup_solve(problems_type, high_accuracy=False):
Expand Down Expand Up @@ -292,7 +292,7 @@ def compute_ratio_setup_solve(problems_type, high_accuracy=False):
'median_ratio': np.median(ratios)}

df_ratio = pd.Series(ratio_stats)
df_ratio.to_csv(ratio_file, header=False, index=True)
df_ratio.to_frame().transpose().to_csv(ratio_file, index=False)


def compute_stats_info(solvers, benchmark_type,
Expand Down
51 changes: 51 additions & 0 deletions utils/parametric.py
Expand Up @@ -46,3 +46,54 @@ def print_results_parametric(problem, dimension):
f.close()

print("")

def compute_results_parametric(problems, dimensions):

row_list = []
statistics_file = os.path.join('.', 'results', 'parametric_problems',
"statistics.csv")
for p in problems:

statistics_prob_file = os.path.join('.', 'results', 'parametric_problems',
"statistics_%s.csv" % p.lower())
row_list_prob = []
for d in dimensions[p]:
ws_file = os.path.join('.', 'results', 'parametric_problems',
'OSQP warmstart',
p,
'n%i.csv' % d
)
no_ws_file = os.path.join('.', 'results', 'parametric_problems',
'OSQP no warmstart',
p,
'n%i.csv' % d
)
no_ws_df = pd.read_csv(no_ws_file)
ws_df = pd.read_csv(ws_file)
dict_stats = {
'problem': p,
'dimension': d,
'OSQP_ws_mean_time': ws_df['run_time'].mean(),
'OSQP_ws_median_time': ws_df['run_time'].median(),
'OSQP_ws_mean_iter': ws_df['iter'].mean(),
'OSQP_ws_median_iter': ws_df['iter'].median(),
'OSQP_nows_mean_time': no_ws_df['run_time'].mean(),
'OSQP_nows_median_time': no_ws_df['run_time'].median(),
'OSQP_nows_mean_iter': no_ws_df['iter'].mean(),
'OSQP_nows_median_iter': no_ws_df['iter'].median(),
'time_speedup_mean': no_ws_df['run_time'].mean()/ws_df['run_time'].mean(),
'time_speedup_median': no_ws_df['run_time'].median()/ws_df['run_time'].median(),
'iter_reduction_mean': no_ws_df['iter'].mean()/ws_df['iter'].mean(),
'iter_reduction_median': no_ws_df['iter'].median()/ws_df['iter'].median()
}
row_list_prob.append(dict_stats)

# Store prob statistics
prob_statistics = pd.DataFrame(row_list_prob)
prob_statistics.to_csv(statistics_prob_file)
row_list += row_list_prob


# Create statistics dict
statistics = pd.DataFrame(row_list)
statistics.to_csv(statistics_file)

0 comments on commit f0310d2

Please sign in to comment.