mirror of
https://github.com/robertmartin8/PyPortfolioOpt.git
synced 2022-11-27 18:02:41 +03:00
Promote Solver & Verbose to Constructor Options; internals are now private
This commit is contained in:
@@ -47,7 +47,6 @@ class BaseOptimizer:
|
||||
self.tickers = tickers
|
||||
# Outputs
|
||||
self.weights = None
|
||||
self.verbose = False
|
||||
|
||||
def _make_output_weights(self, weights=None):
|
||||
"""
|
||||
@@ -142,12 +141,16 @@ class BaseConvexOptimizer(BaseOptimizer):
|
||||
- ``save_weights_to_file()`` saves the weights to csv, json, or txt.
|
||||
"""
|
||||
|
||||
def __init__(self, n_assets, tickers=None, weight_bounds=(0, 1)):
|
||||
def __init__(self, n_assets, tickers=None, weight_bounds=(0, 1), solver=None, verbose=False):
|
||||
"""
|
||||
:param weight_bounds: minimum and maximum weight of each asset OR single min/max pair
|
||||
if all identical, defaults to (0, 1). Must be changed to (-1, 1)
|
||||
for portfolios with shorting.
|
||||
:type weight_bounds: tuple OR tuple list, optional
|
||||
:param solver: name of solver. list available solvers with: `cvxpy.installed_solvers()`
|
||||
:type solver: str, optional (see cvxpy.Problem#_solve for default. spoiler: it's ECOS)
|
||||
:param verbose: whether performance and debugging info should be printed, defaults to False
|
||||
:type verbose: bool, optional
|
||||
"""
|
||||
super().__init__(n_assets, tickers)
|
||||
|
||||
@@ -160,7 +163,8 @@ class BaseConvexOptimizer(BaseOptimizer):
|
||||
self._upper_bounds = None
|
||||
self._map_bounds_to_constraints(weight_bounds)
|
||||
|
||||
self.solver = None
|
||||
self._solver = solver
|
||||
self._verbose = verbose
|
||||
|
||||
def _map_bounds_to_constraints(self, test_bounds):
|
||||
"""
|
||||
@@ -212,10 +216,10 @@ class BaseConvexOptimizer(BaseOptimizer):
|
||||
try:
|
||||
opt = cp.Problem(cp.Minimize(self._objective), self._constraints)
|
||||
|
||||
if self.solver is not None:
|
||||
opt.solve(solver=self.solver, verbose=self.verbose)
|
||||
if self._solver is not None:
|
||||
opt.solve(solver=self._solver, verbose=self._verbose)
|
||||
else:
|
||||
opt.solve(verbose=self.verbose)
|
||||
opt.solve(verbose=self._verbose)
|
||||
except (TypeError, cp.DCPError):
|
||||
raise exceptions.OptimizationError
|
||||
|
||||
|
||||
Reference in New Issue
Block a user