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

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

r""" 

CVXOPT SDP Backend 

  

  

AUTHORS: 

  

- Ingolfur Edvardsson (2014-05) : initial implementation 

  

- Dima Pasechnik (2015-12) : minor fixes 

  

""" 

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

# 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 

  

from sage.numerical.sdp import SDPSolverException 

from sage.matrix.all import Matrix 

from .generic_sdp_backend cimport GenericSDPBackend 

  

  

cdef class CVXOPTSDPBackend(GenericSDPBackend): 

cdef list objective_function #c_matrix 

cdef list coeffs_matrix 

cdef bint is_maximize 

  

cdef list row_name_var 

cdef list col_name_var 

cdef dict answer 

cdef dict param 

cdef str name 

  

def __cinit__(self, maximization = True): 

""" 

Cython constructor 

  

EXAMPLES:: 

  

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

sage: p = get_solver(solver = "CVXOPT") 

  

""" 

  

self.objective_function = [] #c_matrix in the example for cvxopt 

self.name = "" 

self.coeffs_matrix = [] 

self.obj_constant_term = 0 

self.matrices_dim = {} 

self.is_maximize = 1 

  

self.row_name_var = [] 

self.col_name_var = [] 

  

self.param = {"show_progress":False, 

"maxiters":100, 

"abstol":1e-7, 

"reltol":1e-6, 

"feastol":1e-7, 

"refinement":1 } 

self.answer = {} 

if maximization: 

self.set_sense(+1) 

else: 

self.set_sense(-1) 

  

def get_matrix(self): 

""" 

Get a block of a matrix coefficient 

  

EXAMPLES:: 

  

sage: p = SemidefiniteProgram(solver="cvxopt") 

sage: x = p.new_variable() 

sage: a1 = matrix([[1, 2.], [2., 3.]]) 

sage: a2 = matrix([[3, 4.], [4., 5.]]) 

sage: p.add_constraint(a1*x[0] + a2*x[1] <= a1) 

sage: b = p.get_backend() 

sage: b.get_matrix()[0][0] 

( 

[-1.0 -2.0] 

-1, [-2.0 -3.0] 

) 

  

""" 

return self.coeffs_matrix 

  

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

""" 

Add a variable. 

  

This amounts to adding a new column of matrices 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 = "CVXOPT") 

sage: p.ncols() 

0 

sage: p.add_variable() 

0 

sage: p.ncols() 

1 

sage: p.add_variable() 

1 

sage: p.add_variable(name='x',obj=1.0) 

2 

sage: p.col_name(2) 

'x' 

sage: p.objective_coefficient(2) 

1.00000000000000 

""" 

i = 0 

for row in self.coeffs_matrix: 

if self.matrices_dim.get(i) is not None: 

row.append( Matrix.zero(self.matrices_dim[i], self.matrices_dim[i]) ) 

else: 

row.append(0) 

i+=1 

self.col_name_var.append(name) 

self.objective_function.append(obj) 

return len(self.objective_function) - 1 

  

  

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) 

  

- ``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 = "CVXOPT") 

sage: p.ncols() 

0 

sage: p.add_variables(5) 

4 

sage: p.ncols() 

5 

sage: p.add_variables(2, names=['a','b']) 

6 

""" 

for i in range(n): 

self.add_variable() 

return len(self.objective_function) - 1; 

  

  

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 = "CVXOPT") 

sage: p.is_maximization() 

True 

sage: p.set_sense(-1) 

sage: p.is_maximization() 

False 

""" 

if sense == 1: 

self.is_maximize = 1 

else: 

self.is_maximize = 0 

  

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 = "CVXOPT") 

sage: p.add_variable() 

0 

sage: p.objective_coefficient(0) 

0.0 

sage: p.objective_coefficient(0,2) 

sage: p.objective_coefficient(0) 

2.0 

""" 

if coeff is not None: 

self.objective_function[variable] = float(coeff); 

else: 

return self.objective_function[variable] 

  

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 = "CVXOPT") 

sage: p.add_variables(5) 

4 

sage: p.set_objective([1, 1, 2, 1, 3]) 

sage: [p.objective_coefficient(x) for x in range(5)] 

[1, 1, 2, 1, 3] 

""" 

for i in range(len(coeff)): 

self.objective_function[i] = coeff[i]; 

obj_constant_term = d; 

  

  

  

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 (matrix). 

The pairs come sorted by indices. If c is -1 it 

represents the constant coefficient. 

  

- ``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 = "CVXOPT") 

sage: p.add_variables(2) 

1 

sage: p.add_linear_constraint( [(0, matrix([[33., -9.], [-9., 26.]])) , (1, matrix([[-7., -11.] ,[ -11., 3.]]) )]) 

sage: p.row(0) 

([0, 1], 

[ 

[ 33.0000000000000 -9.00000000000000] 

[-9.00000000000000 26.0000000000000], 

<BLANKLINE> 

[-7.00000000000000 -11.0000000000000] 

[-11.0000000000000 3.00000000000000] 

]) 

sage: p.add_linear_constraint( [(0, matrix([[33., -9.], [-9., 26.]])) , (1, matrix([[-7., -11.] ,[ -11., 3.]]) )],name="fun") 

sage: p.row_name(-1) 

'fun' 

""" 

from sage.structure.element import is_Matrix 

for t in coefficients: 

m = t[1] 

if not is_Matrix(m): 

raise ValueError("The coefficients must be matrices") 

if not m.is_square(): 

raise ValueError("The matrix has to be a square") 

if self.matrices_dim.get(self.nrows()) is not None and m.dimensions()[0] != self.matrices_dim.get(self.nrows()): 

raise ValueError("the matrices have to be of the same dimension") 

self.coeffs_matrix.append(coefficients) 

self.matrices_dim[self.nrows()] = m.dimensions()[0] # 

self.row_name_var.append(name) 

  

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

""" 

Add constraints. 

  

INPUT: 

  

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

  

- ``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 = "CVXOPT") 

sage: p.add_variables(5) 

4 

sage: p.add_linear_constraints(5) 

sage: p.row(4) 

([], []) 

""" 

for i in range(number): 

self.add_linear_constraint(zip(range(self.ncols()+1),[Matrix.zero(1,1) for i in range(self.ncols()+1)]), 

name=None if names is None else names[i]) 

  

  

  

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: p = SemidefiniteProgram(solver = "cvxopt", maximization=False) 

sage: x = p.new_variable() 

sage: p.set_objective(x[0] - x[1] + x[2]) 

sage: a1 = matrix([[-7., -11.], [-11., 3.]]) 

sage: a2 = matrix([[7., -18.], [-18., 8.]]) 

sage: a3 = matrix([[-2., -8.], [-8., 1.]]) 

sage: a4 = matrix([[33., -9.], [-9., 26.]]) 

sage: b1 = matrix([[-21., -11., 0.], [-11., 10., 8.], [0., 8., 5.]]) 

sage: b2 = matrix([[0., 10., 16.], [10., -10., -10.], [16., -10., 3.]]) 

sage: b3 = matrix([[-5., 2., -17.], [2., -6., 8.], [-17., 8., 6.]]) 

sage: b4 = matrix([[14., 9., 40.], [9., 91., 10.], [40., 10., 15.]]) 

sage: p.add_constraint(a1*x[0] + a3*x[2] <= a4) 

sage: p.add_constraint(b1*x[0] + b2*x[1] + b3*x[2] <= b4) 

sage: round(p.solve(), 3) 

-3.225 

sage: p = SemidefiniteProgram(solver = "cvxopt", maximization=False) 

sage: x = p.new_variable() 

sage: p.set_objective(x[0] - x[1] + x[2]) 

sage: a1 = matrix([[-7., -11.], [-11., 3.]]) 

sage: a2 = matrix([[7., -18.], [-18., 8.]]) 

sage: a3 = matrix([[-2., -8.], [-8., 1.]]) 

sage: a4 = matrix([[33., -9.], [-9., 26.]]) 

sage: b1 = matrix([[-21., -11., 0.], [-11., 10., 8.], [0., 8., 5.]]) 

sage: b2 = matrix([[0., 10., 16.], [10., -10., -10.], [16., -10., 3.]]) 

sage: b3 = matrix([[-5., 2., -17.], [2., -6., 8.], [-17., 8., 6.]]) 

sage: b4 = matrix([[14., 9., 40.], [9., 91., 10.], [40., 10., 15.]]) 

sage: p.add_constraint(a1*x[0] + a2*x[1] + a3*x[2] <= a4) 

sage: p.add_constraint(b1*x[0] + b2*x[1] + b3*x[2] <= b4) 

sage: round(p.solve(), 3) 

-3.154 

  

""" 

from cvxopt import matrix as c_matrix, solvers 

from sage.rings.all import RDF 

G_matrix = [] 

h_matrix = [] 

debug_g = [] 

debug_h = [] 

debug_c = [] 

  

#cvxopt minimizes on default 

if self.is_maximize: 

c = [-1 * float(e) for e in self.objective_function] 

else: 

c = [float(e) for e in self.objective_function] 

debug_c = (c) 

c = c_matrix(c) 

  

row_index = -1 

for row in self.coeffs_matrix: 

row_index += 1 

row.sort() 

G_temp = [] 

add_null = [True for i in range(self.ncols())] 

for i,m in row: 

if i == -1: 

h_temp = [] 

for row in m.rows(): 

row_temp = [] 

for e in row: 

row_temp.append(-1*float(e)) 

h_temp.append(row_temp) 

h_matrix += [c_matrix(h_temp)] 

debug_h += [h_temp] 

else: 

add_null[i] = False 

m = [float(e) for e in m.list()] 

G_temp.append(m) 

for j in range(self.ncols()): 

if add_null[j]: 

G_temp.insert(j,[float(0) for t in range(self.matrices_dim[row_index]**2)]) 

G_matrix += [c_matrix(G_temp)] 

debug_g += [(G_temp)] 

#raise Exception("G_matrix " + str(debug_g) + "\nh_matrix: " + str(debug_h) + "\nc_matrix: " + str(debug_c)) 

  

#solvers comes from the cvxopt library 

for k,v in self.param.iteritems(): 

solvers.options[k] = v 

  

self.answer = solvers.sdp(c,Gs=G_matrix,hs=h_matrix) 

  

#possible outcomes 

if self.answer['status'] == 'optimized': 

pass 

elif self.answer['status'] == 'primal infeasible': 

raise SDPSolverException("CVXOPT: primal infeasible") 

elif self.answer['status'] == 'dual infeasible': 

raise SDPSolverException("CVXOPT: dual infeasible") 

elif self.answer['status'] == 'unknown': 

raise SDPSolverException("CVXOPT: Terminated early due to numerical difficulties or because the maximum number of iterations was reached.") 

return 0 

  

  

cpdef get_objective_value(self): 

""" 

Return the value of the objective function. 

  

.. NOTE:: 

  

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

  

EXAMPLES:: 

  

sage: p = SemidefiniteProgram(solver = "cvxopt", maximization=False) 

sage: x = p.new_variable() 

sage: p.set_objective(x[0] - x[1] + x[2]) 

sage: a1 = matrix([[-7., -11.], [-11., 3.]]) 

sage: a2 = matrix([[7., -18.], [-18., 8.]]) 

sage: a3 = matrix([[-2., -8.], [-8., 1.]]) 

sage: a4 = matrix([[33., -9.], [-9., 26.]]) 

sage: b1 = matrix([[-21., -11., 0.], [-11., 10., 8.], [0., 8., 5.]]) 

sage: b2 = matrix([[0., 10., 16.], [10., -10., -10.], [16., -10., 3.]]) 

sage: b3 = matrix([[-5., 2., -17.], [2., -6., 8.], [-17., 8., 6.]]) 

sage: b4 = matrix([[14., 9., 40.], [9., 91., 10.], [40., 10., 15.]]) 

sage: p.add_constraint(a1*x[0] + a2*x[1] + a3*x[2] <= a4) 

sage: p.add_constraint(b1*x[0] + b2*x[1] + b3*x[2] <= b4) 

sage: round(p.solve(),3) 

-3.154 

sage: round(p.get_backend().get_objective_value(),3) 

-3.154 

""" 

sum = self.obj_constant_term 

i = 0 

for v in self.objective_function: 

sum += v * float(self.answer['x'][i]) 

i+=1 

return sum 

  

cpdef _get_answer(self): 

""" 

return the complete output dict of the solver 

  

Mainly for testing purposes 

  

TESTS:: 

  

sage: p = SemidefiniteProgram(maximization = False, solver='cvxopt') 

sage: x = p.new_variable() 

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

sage: a1 = matrix([[1, 2.], [2., 3.]]) 

sage: a2 = matrix([[3, 4.], [4., 5.]]) 

sage: a3 = matrix([[5, 6.], [6., 7.]]) 

sage: b1 = matrix([[1, 1.], [1., 1.]]) 

sage: b2 = matrix([[2, 2.], [2., 2.]]) 

sage: b3 = matrix([[3, 3.], [3., 3.]]) 

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

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

sage: p.solve(); # tol 1e-08 

-3.0 

sage: p.get_backend()._get_answer() 

{...} 

""" 

return self.answer 

  

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 = "cvxopt") 

sage: p = SemidefiniteProgram(solver = "cvxopt", maximization=False) 

sage: x = p.new_variable() 

sage: p.set_objective(x[0] - x[1] + x[2]) 

sage: a1 = matrix([[-7., -11.], [-11., 3.]]) 

sage: a2 = matrix([[7., -18.], [-18., 8.]]) 

sage: a3 = matrix([[-2., -8.], [-8., 1.]]) 

sage: a4 = matrix([[33., -9.], [-9., 26.]]) 

sage: b1 = matrix([[-21., -11., 0.], [-11., 10., 8.], [0., 8., 5.]]) 

sage: b2 = matrix([[0., 10., 16.], [10., -10., -10.], [16., -10., 3.]]) 

sage: b3 = matrix([[-5., 2., -17.], [2., -6., 8.], [-17., 8., 6.]]) 

sage: b4 = matrix([[14., 9., 40.], [9., 91., 10.], [40., 10., 15.]]) 

sage: p.add_constraint(a1*x[0] + a2*x[1] + a3*x[2] <= a4) 

sage: p.add_constraint(b1*x[0] + b2*x[1] + b3*x[2] <= b4) 

sage: round(p.solve(),3) 

-3.154 

sage: round(p.get_backend().get_variable_value(0),3) 

-0.368 

sage: round(p.get_backend().get_variable_value(1),3) 

1.898 

sage: round(p.get_backend().get_variable_value(2),3) 

-0.888 

  

""" 

return self.answer['x'][variable] 

  

  

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 = "CVXOPT") 

sage: p.ncols() 

0 

sage: p.add_variables(2) 

1 

sage: p.ncols() 

2 

""" 

  

return len(self.objective_function) 

  

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 = "CVXOPT") 

sage: p.nrows() 

0 

sage: p.add_variables(5) 

4 

sage: p.add_linear_constraints(2) 

sage: p.nrows() 

2 

""" 

return len(self.matrices_dim) 

  

  

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 = "CVXOPT") 

sage: p.is_maximization() 

True 

sage: p.set_sense(-1) 

sage: p.is_maximization() 

False 

""" 

if self.is_maximize == 1: 

return 1 

else: 

return 0 

  

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 = "CVXOPT") 

sage: p.problem_name("There once was a french fry") 

sage: print(p.problem_name()) 

There once was a french fry 

""" 

if name == NULL: 

return self.name 

self.name = str(<bytes>name) 

  

  

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 = "CVXOPT") 

sage: p.add_variables(5) 

4 

sage: p.add_linear_constraint( [(0, matrix([[33., -9.], [-9., 26.]])) , (1, matrix([[-7., -11.] ,[ -11., 3.]]) )]) 

sage: p.row(0) 

([0, 1], 

[ 

[ 33.0000000000000 -9.00000000000000] 

[-9.00000000000000 26.0000000000000], 

<BLANKLINE> 

[-7.00000000000000 -11.0000000000000] 

[-11.0000000000000 3.00000000000000] 

]) 

""" 

indices = [] 

matrices = [] 

for index,m in self.coeffs_matrix[i]: 

if m != Matrix.zero(self.matrices_dim[i],self.matrices_dim[i]): 

indices.append(index) 

matrices.append(m) 

return (indices, matrices) 

  

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='cvxopt') 

sage: x = p.new_variable() 

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

sage: a1 = matrix([[1, 2.], [2., 3.]]) 

sage: a2 = matrix([[3, 4.], [4., 5.]]) 

sage: a3 = matrix([[5, 6.], [6., 7.]]) 

sage: b1 = matrix([[1, 1.], [1., 1.]]) 

sage: b2 = matrix([[2, 2.], [2., 2.]]) 

sage: b3 = matrix([[3, 3.], [3., 3.]]) 

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

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

sage: p.solve() # tol 1e-08 

-3.0 

sage: B=p.get_backend() 

sage: x=p.get_values(x).values() 

sage: -(a3*B.dual_variable(0)).trace()-(b3*B.dual_variable(1)).trace() # tol 1e-07 

-3.0 

sage: g = sum((B.slack(j)*B.dual_variable(j)).trace() for j in range(2)); g # tol 1.5e-08 

0.0 

  

  

TESTS:: 

  

sage: B.dual_variable(7) 

... 

Traceback (most recent call last): 

... 

IndexError: list index out of range 

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

0.0 

  

""" 

cdef int n 

n = self.answer['zs'][i].size[0] 

assert(n == self.answer['zs'][i].size[1]) # must be square matrix 

return Matrix(n, n, list(self.answer['zs'][i]), sparse=sparse) 

  

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='cvxopt') 

sage: x = p.new_variable() 

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

sage: a1 = matrix([[1, 2.], [2., 3.]]) 

sage: a2 = matrix([[3, 4.], [4., 5.]]) 

sage: a3 = matrix([[5, 6.], [6., 7.]]) 

sage: b1 = matrix([[1, 1.], [1., 1.]]) 

sage: b2 = matrix([[2, 2.], [2., 2.]]) 

sage: b3 = matrix([[3, 3.], [3., 3.]]) 

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

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

sage: p.solve() # tol 1e-08 

-3.0 

sage: B = p.get_backend() 

sage: B1 = B.slack(1); B1 # tol 1e-08 

[0.0 0.0] 

[0.0 0.0] 

sage: B1.is_positive_definite() 

True 

sage: x = p.get_values(x).values() 

sage: x[0]*b1 + x[1]*b2 - b3 + B1 # tol 1e-09 

[0.0 0.0] 

[0.0 0.0] 

  

TESTS:: 

  

sage: B.slack(7) 

... 

Traceback (most recent call last): 

... 

IndexError: list index out of range 

  

""" 

cdef int n 

n = self.answer['ss'][i].size[0] 

assert(n == self.answer['ss'][i].size[1]) # must be square matrix 

return Matrix(n, n, list(self.answer['ss'][i]), sparse=sparse) 

  

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 = "CVXOPT") 

sage: p.add_linear_constraints(1, names="A") 

sage: p.row_name(0) 

'A' 

  

""" 

if self.row_name_var[index] is not None: 

return self.row_name_var[index] 

return "constraint_" + repr(index) 

  

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 = "CVXOPT") 

sage: p.add_variable(name="I am a variable") 

0 

sage: p.col_name(0) 

'I am a variable' 

""" 

if self.col_name_var[index] is not None: 

return self.col_name_var[index] 

return "x_" + repr(index) 

  

  

  

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 = "CVXOPT") 

sage: p.solver_parameter("show_progress") 

False 

sage: p.solver_parameter("show_progress", True) 

sage: p.solver_parameter("show_progress") 

True 

""" 

if value is None: 

return self.param[name] 

else: 

self.param[name] = value