本文整理汇总了Python中cvxpy.SolverError方法的典型用法代码示例。如果您正苦于以下问题:Python cvxpy.SolverError方法的具体用法?Python cvxpy.SolverError怎么用?Python cvxpy.SolverError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cvxpy
的用法示例。
在下文中一共展示了cvxpy.SolverError方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: solve
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import SolverError [as 别名]
def solve(self, **kwargs):
error = False
try:
self.prob.solve(**kwargs)
except cvx.SolverError:
error = True
stats = self.prob.solver_stats
print()
info = {
'setup_time': stats.setup_time,
'solver_time': stats.solve_time,
'iterations': stats.num_iters,
'solver_error': error
}
return info
示例2: test_diamond_norm
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import SolverError [as 别名]
def test_diamond_norm(self, num_qubits):
"""Test the diamond_norm for {num_qubits}-qubit pauli channel."""
# pylint: disable=import-outside-toplevel
try:
import cvxpy
except ImportError:
# Skip test if CVXPY not installed
self.skipTest("CVXPY not installed.")
# Pauli channels have an analytic expression for the
# diamond norm so we can easily test it
op = Choi(np.zeros((4 ** num_qubits, 4 ** num_qubits)))
labels = [num_qubits * i for i in ['I', 'X', 'Y', 'Z']]
coeffs = [-1.0, 0.5, 2.5, -0.1]
for coeff, label in zip(coeffs, labels):
op = op + coeff * Choi(Operator.from_label(label))
target = np.sum(np.abs(coeffs))
try:
value = diamond_norm(op)
self.assertAlmostEqual(value, target, places=4)
except cvxpy.SolverError:
self.skipTest("CVXPY solver failed.")
示例3: solve
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import SolverError [as 别名]
def solve(self, **kwargs):
error = False
try:
self.prob.solve(**kwargs)
except cvx.SolverError:
error = True
return error
示例4: _test_same
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import SolverError [as 别名]
def _test_same(dataset):
X, y = dataset
if X.shape[1] == 1:
# If we only have one column (which is also the sensitive one) we can't fit
return True
sensitive_cols = [0]
X_without_sens = np.delete(X, sensitive_cols, axis=1)
lr = LogisticRegression(
penalty="none",
solver="lbfgs",
multi_class="ovr",
dual=False,
tol=1e-4,
C=1.0,
fit_intercept=True,
intercept_scaling=1,
class_weight=None,
random_state=None,
max_iter=100,
verbose=0,
warm_start=False,
n_jobs=None,
l1_ratio=None,
)
fair = DemographicParityClassifier(
covariance_threshold=None, sensitive_cols=sensitive_cols, penalty="none"
)
try:
fair.fit(X, y)
except SolverError:
pass
else:
lr.fit(X_without_sens, y)
normal_pred = lr.predict_proba(X_without_sens)
fair_pred = fair.predict_proba(X)
np.testing.assert_almost_equal(normal_pred, fair_pred, decimal=3)
assert np.sum(lr.predict(X_without_sens) != fair.predict(X)) / len(X) < 0.01
示例5: solve
# 需要导入模块: import cvxpy [as 别名]
# 或者: from cvxpy import SolverError [as 别名]
def solve(self, example):
'''
Solve problem
Args:
example: example object
Returns:
Results structure
'''
problem = example.cvxpy_problem
if 'verbose' in self._settings:
verbose = self._settings["verbose"]
else:
verbose = False
try:
obj_val = problem.solve(solver=cvxpy.ECOS, verbose=verbose)
except cvxpy.SolverError:
if 'verbose' in self._settings: # if verbose is null, suppress it
if self._settings['verbose']:
print("Error in ECOS solution\n")
return Results(s.SOLVER_ERROR, None, None, None,
None, None)
status = self.STATUS_MAP.get(problem.status, s.SOLVER_ERROR)
# Obtain time and number of iterations
run_time = problem.solver_stats.solve_time \
+ problem.solver_stats.setup_time
niter = problem.solver_stats.num_iters
# Get primal, dual solution
x, y = example.revert_cvxpy_solution()
# Validate status
if not is_qp_solution_optimal(example.qp_problem, x, y,
high_accuracy=self._settings.get('high_accuracy')):
status = s.SOLVER_ERROR
# Validate execution time
if 'time_limit' in self._settings:
if run_time > self._settings['time_limit']:
status = s.TIME_LIMIT
return Results(status,
obj_val,
x,
y,
run_time,
niter)