Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

r""" 

Generic Backend for SDP solvers 

  

This class only lists the methods that should be defined by any 

interface with a SDP Solver. All these methods immediately raise 

``NotImplementedError`` exceptions when called, and are obviously 

meant to be replaced by the solver-specific method. This file can also 

be used as a template to create a new interface : one would only need 

to replace the occurrences of ``"Nonexistent_SDP_solver"`` by the 

solver's name, and replace ``GenericSDPBackend`` by 

``SolverName(GenericSDPBackend)`` so that the new solver extends this 

class. 

  

AUTHORS: 

  

- Ingolfur Edvardsson (2014-07): initial implementation 

  

""" 

  

#***************************************************************************** 

# Copyright (C) 2014 Ingolfur Edvardsson <ingolfured@gmail.com> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

from __future__ import print_function 

  

  

cdef class GenericSDPBackend: 

  

cpdef base_ring(self): 

""" 

The base ring 

  

TESTS:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import GenericSDPBackend 

sage: GenericSDPBackend().base_ring() 

Real Double Field 

""" 

from sage.rings.all import RDF 

return RDF 

  

cpdef zero(self): 

""" 

Zero of the base ring 

  

TESTS:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import GenericSDPBackend 

sage: GenericSDPBackend().zero() 

0.0 

""" 

return self.base_ring().zero() 

  

cpdef int add_variable(self, obj=0.0, name=None) except -1: 

""" 

Add a variable. 

  

This amounts to adding a new column to the matrix. By default, 

the variable is both positive and real. 

  

INPUT: 

  

- ``obj`` - (optional) coefficient of this variable in the objective function (default: 0.0) 

  

- ``name`` - an optional name for the newly added variable (default: ``None``). 

  

OUTPUT: The index of the newly created variable 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: p.ncols() # optional - Nonexistent_LP_solver 

0 

sage: p.add_variable() # optional - Nonexistent_LP_solver 

0 

sage: p.ncols() # optional - Nonexistent_LP_solver 

1 

sage: p.add_variable(name='x',obj=1.0) # optional - Nonexistent_LP_solver 

3 

sage: p.col_name(3) # optional - Nonexistent_LP_solver 

'x' 

sage: p.objective_coefficient(3) # optional - Nonexistent_LP_solver 

1.0 

""" 

raise NotImplementedError() 

  

cpdef int add_variables(self, int n, names=None) except -1: 

""" 

Add ``n`` variables. 

  

This amounts to adding new columns to the matrix. By default, 

the variables are both positive and real. 

  

INPUT: 

  

- ``n`` - the number of new variables (must be > 0) 

  

- ``obj`` - (optional) coefficient of all variables in the objective function (default: 0.0) 

  

- ``names`` - optional list of names (default: ``None``) 

  

OUTPUT: The index of the variable created last. 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: p.ncols() # optional - Nonexistent_LP_solver 

0 

sage: p.add_variables(5) # optional - Nonexistent_LP_solver 

4 

sage: p.ncols() # optional - Nonexistent_LP_solver 

5 

sage: p.add_variables(2, lower_bound=-2.0, integer=True, names=['a','b']) # optional - Nonexistent_LP_solver 

6 

""" 

raise NotImplementedError() 

  

cpdef set_sense(self, int sense): 

""" 

Set the direction (maximization/minimization). 

  

INPUT: 

  

- ``sense`` (integer) : 

  

* +1 => Maximization 

* -1 => Minimization 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: p.is_maximization() # optional - Nonexistent_LP_solver 

True 

sage: p.set_sense(-1) # optional - Nonexistent_LP_solver 

sage: p.is_maximization() # optional - Nonexistent_LP_solver 

False 

""" 

raise NotImplementedError() 

  

cpdef objective_coefficient(self, int variable, coeff=None): 

""" 

Set or get the coefficient of a variable in the objective 

function 

  

INPUT: 

  

- ``variable`` (integer) -- the variable's id 

  

- ``coeff`` (double) -- its coefficient 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: p.add_variable() # optional - Nonexistent_LP_solver 

1 

sage: p.objective_coefficient(0) # optional - Nonexistent_LP_solver 

0.0 

sage: p.objective_coefficient(0,2) # optional - Nonexistent_LP_solver 

sage: p.objective_coefficient(0) # optional - Nonexistent_LP_solver 

2.0 

""" 

raise NotImplementedError() 

  

cpdef set_objective(self, list coeff, d=0.0): 

""" 

Set the objective function. 

  

INPUT: 

  

- ``coeff`` -- a list of real values, whose ith element is the 

coefficient of the ith variable in the objective function. 

  

- ``d`` (double) -- the constant term in the linear function (set to `0` by default) 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: p.add_variables(5) # optional - Nonexistent_LP_solver 

5 

sage: p.set_objective([1, 1, 2, 1, 3]) # optional - Nonexistent_LP_solver 

sage: [p.objective_coefficient(x) for x in range(5)] # optional - Nonexistent_LP_solver 

[1.0, 1.0, 2.0, 1.0, 3.0] 

  

Constants in the objective function are respected. 

""" 

raise NotImplementedError() 

  

  

cpdef add_linear_constraint(self, coefficients, name=None): 

""" 

Add a linear constraint. 

  

INPUT: 

  

- ``coefficients`` an iterable with ``(c,v)`` pairs where ``c`` 

is a variable index (integer) and ``v`` is a value (real 

value). 

  

- ``lower_bound`` - a lower bound, either a real value or ``None`` 

  

- ``upper_bound`` - an upper bound, either a real value or ``None`` 

  

- ``name`` - an optional name for this row (default: ``None``) 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: p.add_variables(5) # optional - Nonexistent_LP_solver 

4 

sage: p.add_linear_constraint(zip(range(5), range(5)), 2.0, 2.0) # optional - Nonexistent_LP_solver 

sage: p.row(0) # optional - Nonexistent_LP_solver 

([4, 3, 2, 1], [4.0, 3.0, 2.0, 1.0]) # optional - Nonexistent_LP_solver 

sage: p.row_bounds(0) # optional - Nonexistent_LP_solver 

(2.0, 2.0) 

sage: p.add_linear_constraint( zip(range(5), range(5)), 1.0, 1.0, name='foo') # optional - Nonexistent_LP_solver 

sage: p.row_name(-1) # optional - Nonexistent_LP_solver 

"foo" 

""" 

raise NotImplementedError() 

  

  

cpdef add_linear_constraints(self, int number, names=None): 

""" 

Add constraints. 

  

INPUT: 

  

- ``number`` (integer) -- the number of constraints to add. 

  

- ``lower_bound`` - a lower bound, either a real value or ``None`` 

  

- ``upper_bound`` - an upper bound, either a real value or ``None`` 

  

- ``names`` - an optional list of names (default: ``None``) 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: p.add_variables(5) # optional - Nonexistent_LP_solver 

5 

sage: p.add_linear_constraints(5, None, 2) # optional - Nonexistent_LP_solver 

sage: p.row(4) # optional - Nonexistent_LP_solver 

([], []) 

sage: p.row_bounds(4) # optional - Nonexistent_LP_solver 

(None, 2.0) 

""" 

raise NotImplementedError() 

  

cpdef int solve(self) except -1: 

""" 

Solve the problem. 

  

.. NOTE:: 

  

This method raises ``SDPSolverException`` exceptions when 

the solution can not be computed for any reason (none 

exists, or the LP solver was not able to find it, etc...) 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: p.add_linear_constraints(5, 0, None) # optional - Nonexistent_LP_solver 

sage: p.add_col(range(5), range(5)) # optional - Nonexistent_LP_solver 

sage: p.solve() # optional - Nonexistent_LP_solver 

0 

sage: p.objective_coefficient(0,1) # optional - Nonexistent_LP_solver 

sage: p.solve() # optional - Nonexistent_LP_solver 

Traceback (most recent call last): 

... 

SDPSolverException: ... 

""" 

raise NotImplementedError() 

  

cpdef get_objective_value(self): 

""" 

Return the value of the objective function. 

  

.. NOTE:: 

  

Behaviour is undefined unless ``solve`` has been called before. 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: p.add_variables(2) # optional - Nonexistent_LP_solver 

2 

sage: p.add_linear_constraint([(0,1), (1,2)], None, 3) # optional - Nonexistent_LP_solver 

sage: p.set_objective([2, 5]) # optional - Nonexistent_LP_solver 

sage: p.solve() # optional - Nonexistent_LP_solver 

0 

sage: p.get_objective_value() # optional - Nonexistent_LP_solver 

7.5 

sage: p.get_variable_value(0) # optional - Nonexistent_LP_solver 

0.0 

sage: p.get_variable_value(1) # optional - Nonexistent_LP_solver 

1.5 

""" 

  

raise NotImplementedError() 

  

cpdef get_variable_value(self, int variable): 

""" 

Return the value of a variable given by the solver. 

  

.. NOTE:: 

  

Behaviour is undefined unless ``solve`` has been called before. 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: p.add_variables(2) # optional - Nonexistent_LP_solver 

2 

sage: p.add_linear_constraint([(0,1), (1, 2)], None, 3) # optional - Nonexistent_LP_solver 

sage: p.set_objective([2, 5]) # optional - Nonexistent_LP_solver 

sage: p.solve() # optional - Nonexistent_LP_solver 

0 

sage: p.get_objective_value() # optional - Nonexistent_LP_solver 

7.5 

sage: p.get_variable_value(0) # optional - Nonexistent_LP_solver 

0.0 

sage: p.get_variable_value(1) # optional - Nonexistent_LP_solver 

1.5 

""" 

  

raise NotImplementedError() 

  

cpdef int ncols(self): 

""" 

Return the number of columns/variables. 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: p.ncols() # optional - Nonexistent_LP_solver 

0 

sage: p.add_variables(2) # optional - Nonexistent_LP_solver 

2 

sage: p.ncols() # optional - Nonexistent_LP_solver 

2 

""" 

  

raise NotImplementedError() 

  

cpdef int nrows(self): 

""" 

Return the number of rows/constraints. 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: p.nrows() # optional - Nonexistent_LP_solver 

0 

sage: p.add_linear_constraints(2, 2.0, None) # optional - Nonexistent_LP_solver 

sage: p.nrows() # optional - Nonexistent_LP_solver 

2 

""" 

  

raise NotImplementedError() 

  

cpdef bint is_maximization(self): 

""" 

Test whether the problem is a maximization 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: p.is_maximization() # optional - Nonexistent_LP_solver 

True 

sage: p.set_sense(-1) # optional - Nonexistent_LP_solver 

sage: p.is_maximization() # optional - Nonexistent_LP_solver 

False 

""" 

raise NotImplementedError() 

  

cpdef problem_name(self, char * name = NULL): 

""" 

Return or define the problem's name 

  

INPUT: 

  

- ``name`` (``char *``) -- the problem's name. When set to 

``NULL`` (default), the method returns the problem's name. 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: p.problem_name("There once was a french fry") # optional - Nonexistent_LP_solver 

sage: print(p.get_problem_name()) # optional - Nonexistent_LP_solver 

There once was a french fry 

""" 

  

raise NotImplementedError() 

  

cpdef row(self, int i): 

""" 

Return a row 

  

INPUT: 

  

- ``index`` (integer) -- the constraint's id. 

  

OUTPUT: 

  

A pair ``(indices, coeffs)`` where ``indices`` lists the 

entries whose coefficient is nonzero, and to which ``coeffs`` 

associates their coefficient on the model of the 

``add_linear_constraint`` method. 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: p.add_variables(5) # optional - Nonexistent_LP_solver 

5 

sage: p.add_linear_constraint(zip(range(5), range(5)), 2, 2) # optional - Nonexistent_LP_solver 

sage: p.row(0) # optional - Nonexistent_LP_solver 

([4, 3, 2, 1], [4.0, 3.0, 2.0, 1.0]) 

sage: p.row_bounds(0) # optional - Nonexistent_LP_solver 

(2.0, 2.0) 

""" 

raise NotImplementedError() 

  

  

  

cpdef row_name(self, int index): 

""" 

Return the ``index`` th row name 

  

INPUT: 

  

- ``index`` (integer) -- the row's id 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: p.add_linear_constraints(1, 2, None, name="Empty constraint 1") # optional - Nonexistent_LP_solver 

sage: p.row_name(0) # optional - Nonexistent_LP_solver 

'Empty constraint 1' 

  

""" 

raise NotImplementedError() 

  

cpdef col_name(self, int index): 

""" 

Return the ``index`` th col name 

  

INPUT: 

  

- ``index`` (integer) -- the col's id 

  

- ``name`` (``char *``) -- its name. When set to ``NULL`` 

(default), the method returns the current name. 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: p.add_variable(name="I am a variable") # optional - Nonexistent_LP_solver 

1 

sage: p.col_name(0) # optional - Nonexistent_LP_solver 

'I am a variable' 

""" 

raise NotImplementedError() 

  

cpdef dual_variable(self, int i, sparse=False): 

""" 

The `i`-th dual variable 

  

Available after self.solve() is called, otherwise the result is undefined 

  

- ``index`` (integer) -- the constraint's id. 

  

OUTPUT: 

  

The matrix of the `i`-th dual variable 

  

EXAMPLES:: 

  

sage: p = SemidefiniteProgram(maximization = False,solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: x = p.new_variable() # optional - Nonexistent_LP_solver 

sage: p.set_objective(x[0] - x[1]) # optional - Nonexistent_LP_solver 

sage: a1 = matrix([[1, 2.], [2., 3.]]) # optional - Nonexistent_LP_solver 

sage: a2 = matrix([[3, 4.], [4., 5.]]) # optional - Nonexistent_LP_solver 

sage: a3 = matrix([[5, 6.], [6., 7.]]) # optional - Nonexistent_LP_solver 

sage: b1 = matrix([[1, 1.], [1., 1.]]) # optional - Nonexistent_LP_solver 

sage: b2 = matrix([[2, 2.], [2., 2.]]) # optional - Nonexistent_LP_solver 

sage: b3 = matrix([[3, 3.], [3., 3.]]) # optional - Nonexistent_LP_solver 

sage: p.add_constraint(a1*x[0] + a2*x[1] <= a3) # optional - Nonexistent_LP_solver 

sage: p.add_constraint(b1*x[0] + b2*x[1] <= b3) # optional - Nonexistent_LP_solver 

sage: p.solve() # optional - Nonexistent_LP_solver # tol ??? 

-3.0 

sage: B=p.get_backend() # optional - Nonexistent_LP_solver 

sage: x=p.get_values(x).values() # optional - Nonexistent_LP_solver 

sage: -(a3*B.dual_variable(0)).trace()-(b3*B.dual_variable(1)).trace() # optional - Nonexistent_LP_solver # tol ??? 

-3.0 

sage: g = sum((B.slack(j)*B.dual_variable(j)).trace() for j in range(2)); g # optional - Nonexistent_LP_solver # tol ??? 

0.0 

  

TESTS:: 

  

sage: B.dual_variable(7) # optional - Nonexistent_LP_solver 

... 

Traceback (most recent call last): 

... 

IndexError: list index out of range 

sage: abs(g - B._get_answer()['gap']) # optional - Nonexistent_LP_solver # tol 1e-22 

0.0 

""" 

raise NotImplementedError() 

  

cpdef slack(self, int i, sparse=False): 

""" 

Slack of the `i`-th constraint 

  

Available after self.solve() is called, otherwise the result is undefined 

  

- ``index`` (integer) -- the constraint's id. 

  

OUTPUT: 

  

The matrix of the slack of the `i`-th constraint 

  

EXAMPLES:: 

  

sage: p = SemidefiniteProgram(maximization = False,solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: x = p.new_variable() # optional - Nonexistent_LP_solver 

sage: p.set_objective(x[0] - x[1]) # optional - Nonexistent_LP_solver 

sage: a1 = matrix([[1, 2.], [2., 3.]]) # optional - Nonexistent_LP_solver 

sage: a2 = matrix([[3, 4.], [4., 5.]]) # optional - Nonexistent_LP_solver 

sage: a3 = matrix([[5, 6.], [6., 7.]]) # optional - Nonexistent_LP_solver 

sage: b1 = matrix([[1, 1.], [1., 1.]]) # optional - Nonexistent_LP_solver 

sage: b2 = matrix([[2, 2.], [2., 2.]]) # optional - Nonexistent_LP_solver 

sage: b3 = matrix([[3, 3.], [3., 3.]]) # optional - Nonexistent_LP_solver 

sage: p.add_constraint(a1*x[0] + a2*x[1] <= a3) # optional - Nonexistent_LP_solver 

sage: p.add_constraint(b1*x[0] + b2*x[1] <= b3) # optional - Nonexistent_LP_solver 

sage: p.solve() # optional - Nonexistent_LP_solver # tol ??? 

-3.0 

sage: B=p.get_backend() # optional - Nonexistent_LP_solver 

sage: B1 = B.slack(1); B1 # optional - Nonexistent_LP_solver # tol ??? 

[0.0 0.0] 

[0.0 0.0] 

sage: B1.is_positive_definite() # optional - Nonexistent_LP_solver 

True 

sage: x = p.get_values(x).values() # optional - Nonexistent_LP_solver 

sage: x[0]*b1 + x[1]*b2 - b3 + B1 # optional - Nonexistent_LP_solver # tol ??? 

[0.0 0.0] 

[0.0 0.0] 

  

TESTS:: 

  

sage: B.slack(7) # optional - Nonexistent_LP_solver 

... 

Traceback (most recent call last): 

... 

IndexError: list index out of range 

""" 

raise NotImplementedError() 

  

cpdef solver_parameter(self, name, value = None): 

""" 

Return or define a solver parameter 

  

INPUT: 

  

- ``name`` (string) -- the parameter 

  

- ``value`` -- the parameter's value if it is to be defined, 

or ``None`` (default) to obtain its current value. 

  

.. NOTE:: 

  

The list of available parameters is available at 

:meth:`~sage.numerical.sdp.SemidefiniteProgram.solver_parameter`. 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver 

sage: p.solver_parameter("timelimit") # optional - Nonexistent_LP_solver 

sage: p.solver_parameter("timelimit", 60) # optional - Nonexistent_LP_solver 

sage: p.solver_parameter("timelimit") # optional - Nonexistent_LP_solver 

""" 

raise NotImplementedError() 

  

  

  

default_solver = None 

  

def default_sdp_solver(solver = None): 

""" 

Returns/Sets the default SDP Solver used by Sage 

  

INPUT: 

  

- ``solver`` -- defines the solver to use: 

  

  

- CVXOPT (``solver="CVXOPT"``). See the `CVXOPT 

<http://cvxopt.org/>`_ web site. 

  

``solver`` should then be equal to one of ``"CVXOPT"``. 

  

- If ``solver=None`` (default), the current default solver's name is 

returned. 

  

OUTPUT: 

  

This function returns the current default solver's name if ``solver = None`` 

(default). Otherwise, it sets the default solver to the one given. If this 

solver does not exist, or is not available, a ``ValueError`` exception is 

raised. 

  

EXAMPLES:: 

  

sage: former_solver = default_sdp_solver() 

sage: default_sdp_solver("Cvxopt") 

sage: default_sdp_solver() 

'Cvxopt' 

sage: default_sdp_solver("Yeahhhhhhhhhhh") 

Traceback (most recent call last): 

... 

ValueError: 'solver' should be set to 'CVXOPT' or None. 

sage: default_sdp_solver(former_solver) 

""" 

global default_solver 

  

if solver is None: 

  

if default_solver is not None: 

return default_solver 

  

else: 

for s in ["Cvxopt"]: 

try: 

default_sdp_solver(s) 

return s 

except ValueError: 

pass 

  

solver = solver.capitalize() 

  

  

if solver == "Cvxopt": 

try: 

from sage.numerical.backends.cvxopt_sdp_backend import CVXOPTSDPBackend 

default_solver = solver 

except ImportError: 

raise ValueError("CVXOPT is not available. Please refer to the documentation to install it.") 

  

else: 

raise ValueError("'solver' should be set to 'CVXOPT' or None.") 

  

cpdef GenericSDPBackend get_solver(solver = None): 

""" 

Return a solver according to the given preferences. 

  

INPUT: 

  

- ``solver`` -- 1 solver should be available through this class: 

  

- CVXOPT (``solver="CVXOPT"``). See the `CVXOPT 

<http://cvxopt.org/>`_ web site. 

  

``solver`` should then be equal to one of ``"CVXOPT"`` or ``None``. 

If ``solver=None`` (default), the default solver is used (see ``default_sdp_solver`` method. 

  

.. SEEALSO:: 

  

- :func:`default_sdp_solver` -- Returns/Sets the default SDP solver. 

  

EXAMPLES:: 

  

sage: from sage.numerical.backends.generic_sdp_backend import get_solver 

sage: p = get_solver() 

""" 

if solver is None: 

solver = default_sdp_solver() 

  

else: 

solver = solver.capitalize() 

  

  

if solver == "Cvxopt": 

from sage.numerical.backends.cvxopt_sdp_backend import CVXOPTSDPBackend 

return CVXOPTSDPBackend() 

  

else: 

raise ValueError("'solver' should be set to 'CVXOPT' or None (in which case the default one is used).")