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

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

r""" 

InteractiveLP Backend 

  

AUTHORS: 

  

- Nathann Cohen (2010-10) : generic_backend template 

- Matthias Koeppe (2016-03) : this backend 

  

""" 

  

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

# Copyright (C) 2010 Nathann Cohen <nathann.cohen@gmail.com> 

# Copyright (C) 2016 Matthias Koeppe <mkoeppe@math.ucdavis.edu> 

# 

# 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.mip import MIPSolverException 

from sage.numerical.interactive_simplex_method import InteractiveLPProblem, default_variable_name 

from sage.modules.all import vector 

from copy import copy 

  

cdef class InteractiveLPBackend: 

""" 

MIP Backend that works with :class:`InteractiveLPProblem`. 

  

This backend should be used only for linear programs over general fields, 

or for educational purposes. For fast computations with floating point 

arithmetic, use one of the numerical backends. For exact computations 

with rational numbers, use backend 'PPL'. 

  

There is no support for integer variables. 

  

EXAMPLES:: 

  

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

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

  

TESTS: 

  

General backend testsuite:: 

  

sage: p = MixedIntegerLinearProgram(solver="InteractiveLP") 

sage: TestSuite(p.get_backend()).run(skip="_test_pickling") 

  

""" 

  

def __cinit__(self, maximization = True, base_ring = None): 

""" 

Cython constructor 

  

EXAMPLES:: 

  

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

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

  

This backend can work with irrational algebraic numbers:: 

  

sage: poly = polytopes.dodecahedron(base_ring=AA) 

sage: lp, x = poly.to_linear_program(solver='InteractiveLP', return_variable=True) 

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

sage: lp.solve() 

2.291796067500631? 

sage: lp.get_values(x[0], x[1], x[2]) 

[0.763932022500211?, 0.763932022500211?, 0.763932022500211?] 

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

sage: lp.solve() 

2.291796067500631? 

sage: lp.get_values(x[0], x[1], x[2]) 

[0.763932022500211?, -0.763932022500211?, -0.763932022500211?] 

""" 

  

if base_ring is None: 

from sage.rings.all import QQ 

base_ring = QQ 

  

self.lp = InteractiveLPProblem([], [], [], base_ring=base_ring) 

self.set_verbosity(0) 

  

if maximization: 

self.set_sense(+1) 

else: 

self.set_sense(-1) 

  

self.row_names = [] 

  

cpdef __copy__(self): 

""" 

Returns a copy of self. 

  

EXAMPLES:: 

  

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

sage: p = MixedIntegerLinearProgram(solver = "InteractiveLP") 

sage: b = p.new_variable() 

sage: p.add_constraint(b[1] + b[2] <= 6) 

sage: p.set_objective(b[1] + b[2]) 

sage: cp = copy(p.get_backend()) 

sage: cp.solve() 

0 

sage: cp.get_objective_value() 

6 

""" 

cdef InteractiveLPBackend cp = type(self)(base_ring=self.base_ring()) 

cp.lp = self.lp # it's considered immutable; so no need to copy. 

cp.row_names = copy(self.row_names) 

cp.prob_name = self.prob_name 

return cp 

  

cpdef base_ring(self): 

""" 

Return the base ring. 

  

OUTPUT: 

  

A ring. The coefficients that the chosen solver supports. 

  

EXAMPLES:: 

  

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

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

sage: p.base_ring() 

Rational Field 

""" 

return self.lp.base_ring() 

  

def _variable_type_from_bounds(self, lower_bound, upper_bound): 

""" 

Return a string designating a variable type in `InteractiveLPProblem`. 

  

INPUT: 

  

- ``lower_bound`` - the lower bound of the variable 

  

- ``upper_bound`` - the upper bound of the variable 

  

OUTPUT: 

  

- a string, one of "", "<=", ">=" 

  

The function raises an error if this pair of bounds cannot be 

represented by an `InteractiveLPProblem` variable type. 

  

EXAMPLES:: 

  

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

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

sage: p._variable_type_from_bounds(0, None) 

'>=' 

sage: p._variable_type_from_bounds(None, 0) 

'<=' 

sage: p._variable_type_from_bounds(None, None) 

'' 

sage: p._variable_type_from_bounds(None, 5) 

Traceback (most recent call last): 

... 

NotImplementedError: General variable bounds not supported 

""" 

if lower_bound is None: 

if upper_bound is None: 

return "" 

elif upper_bound == 0: 

return "<=" 

else: 

raise NotImplementedError("General variable bounds not supported") 

elif lower_bound == 0: 

if upper_bound is None: 

return ">=" 

else: 

raise NotImplementedError("General variable bounds not supported") 

else: 

raise NotImplementedError("General variable bounds not supported") 

  

cpdef int add_variable(self, lower_bound=0, upper_bound=None, 

binary=False, continuous=True, integer=False, 

obj=None, name=None, coefficients=None) except -1: 

## coefficients is an extension in this backend, 

## and a proposed addition to the interface, to unify this with add_col. 

""" 

Add a variable. 

  

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

the variable is both nonnegative and real. 

  

In this backend, variables are always continuous (real). 

If integer variables are requested via the parameters 

``binary`` and ``integer``, an error will be raised. 

  

INPUT: 

  

- ``lower_bound`` - the lower bound of the variable (default: 0) 

  

- ``upper_bound`` - the upper bound of the variable (default: ``None``) 

  

- ``binary`` - ``True`` if the variable is binary (default: ``False``). 

  

- ``continuous`` - ``True`` if the variable is binary (default: ``True``). 

  

- ``integer`` - ``True`` if the variable is binary (default: ``False``). 

  

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

  

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

  

- ``coefficients`` -- (optional) an iterable of pairs ``(i, v)``. In each 

pair, ``i`` is a variable index (integer) and ``v`` is a 

value (element of :meth:`base_ring`). 

  

OUTPUT: The index of the newly created variable 

  

EXAMPLES:: 

  

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

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

sage: p.ncols() 

0 

sage: p.add_variable() 

0 

sage: p.ncols() 

1 

sage: p.add_variable(continuous=True, integer=True) 

Traceback (most recent call last): 

... 

ValueError: ... 

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

1 

sage: p.col_name(1) 

'x' 

sage: p.objective_coefficient(1) 

1 

""" 

A, b, c, x, constraint_types, variable_types, problem_type, ring, d = self._AbcxCVPRd() 

cdef int vtype = int(binary) + int(continuous) + int(integer) 

if vtype == 0: 

continuous = True 

elif vtype != 1: 

raise ValueError("Exactly one parameter of 'binary', 'integer' and 'continuous' must be 'True'.") 

if not continuous: 

raise NotImplementedError("Integer variables are not supported") 

variable_types = variable_types + (self._variable_type_from_bounds(lower_bound, upper_bound),) 

col = vector(ring, self.nrows()) 

if coefficients is not None: 

for (i, v) in coefficients: 

col[i] = v 

A = A.augment(col) 

if obj is None: 

obj = 0 

c = tuple(c) + (obj,) 

if name is None: 

var_names = default_variable_name("primal decision") 

name = "{}{:d}".format(var_names, self.ncols() + 1) 

x = tuple(x) + (name,) 

self.lp = InteractiveLPProblem(A, b, c, x, 

constraint_types, variable_types, 

problem_type, ring, objective_constant_term=d) 

return self.ncols() - 1 

  

cpdef set_variable_type(self, int variable, int vtype): 

""" 

Set the type of a variable. 

  

In this backend, variables are always continuous (real). 

If integer or binary variables are requested via the parameter 

``vtype``, an error will be raised. 

  

INPUT: 

  

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

  

- ``vtype`` (integer) : 

  

* 1 Integer 

* 0 Binary 

* -1 Continuous 

  

EXAMPLES:: 

  

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

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

sage: p.ncols() 

0 

sage: p.add_variable() 

0 

sage: p.set_variable_type(0,-1) 

sage: p.is_variable_continuous(0) 

True 

""" 

if vtype == -1: 

pass 

else: 

raise NotImplementedError() 

  

def _AbcxCVPRd(self): 

""" 

Retrieve all problem data from the LP. 

  

EXAMPLES:: 

  

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

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

sage: p._AbcxCVPRd() 

([], (), (), (), (), (), 'max', Rational Field, 0) 

""" 

A, b, c, x = self.lp.Abcx() 

constraint_types = self.lp.constraint_types() 

variable_types = self.lp.variable_types() 

problem_type = self.lp.problem_type() 

base_ring = self.lp.base_ring() 

d = self.lp.objective_constant_term() 

return A, b, c, x, constraint_types, variable_types, problem_type, base_ring, d 

  

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_backend import get_solver 

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

sage: p.is_maximization() 

True 

sage: p.set_sense(-1) 

sage: p.is_maximization() 

False 

""" 

A, b, c, x, constraint_types, variable_types, problem_type, ring, d = self._AbcxCVPRd() 

if sense == +1: 

problem_type = "max" 

else: 

problem_type = "min" 

self.lp = InteractiveLPProblem(A, b, c, x, 

constraint_types, variable_types, 

problem_type, ring, objective_constant_term=d) 

  

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_backend import get_solver 

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

sage: p.add_variable() 

0 

sage: p.objective_coefficient(0) 

0 

sage: p.objective_coefficient(0,2) 

sage: p.objective_coefficient(0) 

2 

""" 

if coeff is None: 

return self.lp.objective_coefficients()[variable] 

else: 

A, b, c, x, constraint_types, variable_types, problem_type, ring, d = self._AbcxCVPRd() 

c = list(c) 

c[variable] = coeff 

self.lp = InteractiveLPProblem(A, b, c, x, 

constraint_types, variable_types, 

problem_type, ring, objective_constant_term=d) 

  

cpdef objective_constant_term(self, d=None): 

""" 

Set or get the constant term in the objective function 

  

INPUT: 

  

- ``d`` (double) -- its coefficient. If `None` (default), return the current value. 

  

EXAMPLES:: 

  

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

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

sage: p.objective_constant_term() 

0 

sage: p.objective_constant_term(42) 

sage: p.objective_constant_term() 

42 

""" 

if d is None: 

return self.lp.objective_constant_term() 

else: 

A, b, c, x, constraint_types, variable_types, problem_type, ring, _ = self._AbcxCVPRd() 

self.lp = InteractiveLPProblem(A, b, c, x, 

constraint_types, variable_types, 

problem_type, ring, objective_constant_term=d) 

  

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

""" 

Set the objective function. 

  

INPUT: 

  

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

coefficient of the i-th variable in the objective function. 

  

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

  

EXAMPLES:: 

  

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

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

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] 

  

Constants in the objective function are respected:: 

  

sage: p = MixedIntegerLinearProgram(solver='InteractiveLP') 

sage: x,y = p[0], p[1] 

sage: p.add_constraint(2*x + 3*y, max = 6) 

sage: p.add_constraint(3*x + 2*y, max = 6) 

sage: p.set_objective(x + y + 7) 

sage: p.solve() 

47/5 

  

TESTS: 

  

Constants also work the right way for min:: 

  

sage: p = MixedIntegerLinearProgram(solver='InteractiveLP', maximization=False) 

sage: x,y = p[0], p[1] 

sage: p.add_constraint(2*x + 3*y, max = 6) 

sage: p.add_constraint(3*x + 2*y, max = 6) 

sage: p.set_objective(-x - y - 7) 

sage: p.solve() 

-47/5 

  

""" 

A, b, _, x, constraint_types, variable_types, problem_type, ring, _ = self._AbcxCVPRd() 

c = coeff 

self.lp = InteractiveLPProblem(A, b, c, x, 

constraint_types, variable_types, 

problem_type, ring, objective_constant_term=d) 

  

cpdef set_verbosity(self, int level): 

""" 

Set the log (verbosity) level 

  

INPUT: 

  

- ``level`` (integer) -- From 0 (no verbosity) to 3. 

  

EXAMPLES:: 

  

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

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

sage: p.set_verbosity(2) 

""" 

self.verbosity = level 

  

cpdef remove_constraint(self, int i): 

r""" 

Remove a constraint. 

  

INPUT: 

  

- ``i`` -- index of the constraint to remove. 

  

EXAMPLES:: 

  

sage: p = MixedIntegerLinearProgram(solver="InteractiveLP") 

sage: v = p.new_variable(nonnegative=True) 

sage: x,y = v[0], v[1] 

sage: p.add_constraint(2*x + 3*y, max = 6) 

sage: p.add_constraint(3*x + 2*y, max = 6) 

sage: p.set_objective(x + y + 7) 

sage: p.solve() 

47/5 

sage: p.remove_constraint(0) 

sage: p.solve() 

10 

sage: p.get_values([x,y]) 

[0, 3] 

""" 

A, b, c, x, constraint_types, variable_types, problem_type, ring, d = self._AbcxCVPRd() 

A = A.delete_rows((i,)) 

b = list(b); del b[i] 

constraint_types=list(constraint_types); del constraint_types[i] 

self.lp = InteractiveLPProblem(A, b, c, x, 

constraint_types, variable_types, 

problem_type, ring, objective_constant_term=d) 

  

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

""" 

Add a linear constraint. 

  

INPUT: 

  

- ``coefficients`` -- an iterable of pairs ``(i, v)``. In each 

pair, ``i`` is a variable index (integer) and ``v`` is a 

value (element of :meth:`base_ring`). 

  

- ``lower_bound`` -- element of :meth:`base_ring` or 

``None``. The lower bound. 

  

- ``upper_bound`` -- element of :meth:`base_ring` or 

``None``. The upper bound. 

  

- ``name`` -- string or ``None``. Optional name for this row. 

  

EXAMPLES:: 

  

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

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

sage: p.add_variables(5) 

4 

sage: p.add_linear_constraint( zip(range(5), range(5)), 2, 2) 

sage: p.row(0) 

([1, 2, 3, 4], [1, 2, 3, 4]) 

sage: p.row_bounds(0) 

(2, 2) 

sage: p.add_linear_constraint( zip(range(5), range(5)), 1, 1, name='foo') 

sage: p.row_name(1) 

'foo' 

""" 

A, b, c, x, constraint_types, variable_types, problem_type, ring, d = self._AbcxCVPRd() 

if lower_bound is None: 

if upper_bound is None: 

raise ValueError("At least one of lower_bound and upper_bound must be provided") 

else: 

constraint_types = constraint_types + ("<=",) 

b = tuple(b) + (upper_bound,) 

else: 

if upper_bound is None: 

constraint_types = constraint_types + (">=",) 

b = tuple(b) + (lower_bound,) 

elif lower_bound == upper_bound: 

constraint_types = constraint_types + ("==",) 

b = tuple(b) + (lower_bound,) 

else: 

raise NotImplementedError("Ranged constraints are not supported") 

  

row = vector(ring, self.ncols()) 

for (i, v) in coefficients: 

row[i] = v 

A = A.stack(row) 

  

self.row_names.append(name) 

  

self.lp = InteractiveLPProblem(A, b, c, x, 

constraint_types, variable_types, 

problem_type, ring, objective_constant_term=d) 

  

  

cpdef add_col(self, list indices, list coeffs): 

""" 

Add a column. 

  

INPUT: 

  

- ``indices`` (list of integers) -- this list contains the 

indices of the constraints in which the variable's 

coefficient is nonzero 

  

- ``coeffs`` (list of real values) -- associates a coefficient 

to the variable in each of the constraints in which it 

appears. Namely, the i-th entry of ``coeffs`` corresponds to 

the coefficient of the variable in the constraint 

represented by the i-th entry in ``indices``. 

  

.. NOTE:: 

  

``indices`` and ``coeffs`` are expected to be of the same 

length. 

  

EXAMPLES:: 

  

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

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

sage: p.ncols() 

0 

sage: p.nrows() 

0 

sage: p.add_linear_constraints(5, 0, None) 

sage: p.add_col(range(5), range(5)) 

sage: p.nrows() 

5 

""" 

self.add_variable(coefficients = zip(indices, coeffs)) 

  

cpdef int solve(self) except -1: 

""" 

Solve the problem. 

  

.. NOTE:: 

  

This method raises ``MIPSolverException`` 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_backend import get_solver 

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

sage: p.add_linear_constraints(5, 0, None) 

sage: p.add_col(range(5), range(5)) 

sage: p.solve() 

0 

sage: p.objective_coefficient(0,1) 

sage: p.solve() 

Traceback (most recent call last): 

... 

MIPSolverException: ... 

""" 

## FIXME: standard_form should allow to pass slack names (which we would take from row_names). 

## FIXME: Perhaps also pass the problem name as objective name 

lp_std_form, transformation = self.lp.standard_form(transformation=True) 

self.lp_std_form, self.std_form_transformation = lp_std_form, transformation 

output = lp_std_form.run_revised_simplex_method() 

## FIXME: Display output as a side effect if verbosity is high enough 

d = self.final_dictionary = lp_std_form.final_revised_dictionary() 

if d.is_optimal(): 

if lp_std_form.auxiliary_variable() in d.basic_variables(): 

raise MIPSolverException("InteractiveLP: Problem has no feasible solution") 

else: 

return 0 

else: 

raise MIPSolverException("InteractiveLP: Problem is unbounded") 

  

cpdef get_objective_value(self): 

""" 

Return the value of the objective function. 

  

.. NOTE:: 

  

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

  

EXAMPLES:: 

  

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

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

sage: p.add_variables(2) 

1 

sage: p.add_linear_constraint([(0,1), (1,2)], None, 3) 

sage: p.set_objective([2, 5]) 

sage: p.solve() 

0 

sage: p.get_objective_value() 

15/2 

sage: p.get_variable_value(0) 

0 

sage: p.get_variable_value(1) 

3/2 

""" 

d = self.final_dictionary 

v = d.objective_value() 

if self.lp_std_form.is_negative(): 

v = - v 

return v 

  

cpdef get_variable_value(self, int variable): 

""" 

Return the value of a variable given by the solver. 

  

.. NOTE:: 

  

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

  

EXAMPLES:: 

  

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

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

sage: p.add_variables(2) 

1 

sage: p.add_linear_constraint([(0,1), (1, 2)], None, 3) 

sage: p.set_objective([2, 5]) 

sage: p.solve() 

0 

sage: p.get_objective_value() 

15/2 

sage: p.get_variable_value(0) 

0 

sage: p.get_variable_value(1) 

3/2 

""" 

solution = self.std_form_transformation(self.final_dictionary.basic_solution()) 

return solution[variable] 

  

cpdef int ncols(self): 

""" 

Return the number of columns/variables. 

  

EXAMPLES:: 

  

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

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

sage: p.ncols() 

0 

sage: p.add_variables(2) 

1 

sage: p.ncols() 

2 

""" 

return self.lp.n_variables() 

  

cpdef int nrows(self): 

""" 

Return the number of rows/constraints. 

  

EXAMPLES:: 

  

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

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

sage: p.nrows() 

0 

sage: p.add_linear_constraints(2, 0, None) 

sage: p.nrows() 

2 

""" 

return self.lp.n_constraints() 

  

cpdef bint is_maximization(self): 

""" 

Test whether the problem is a maximization 

  

EXAMPLES:: 

  

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

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

sage: p.is_maximization() 

True 

sage: p.set_sense(-1) 

sage: p.is_maximization() 

False 

""" 

return self.lp.problem_type() == "max" 

  

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_backend import get_solver 

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

sage: p.problem_name("There_once_was_a_french_fry") 

sage: print(p.problem_name()) 

There_once_was_a_french_fry 

""" 

if name == NULL: 

if self.prob_name is not None: 

return self.prob_name 

else: 

return "" 

else: 

self.prob_name = str(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_backend import get_solver 

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

sage: p.add_variables(5) 

4 

sage: p.add_linear_constraint(zip(range(5), range(5)), 0, None) 

sage: p.row(0) 

([1, 2, 3, 4], [1, 2, 3, 4]) 

""" 

A, b, c, x = self.lp.Abcx() 

indices = [] 

coeffs = [] 

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

if A[i][j] != 0: 

indices.append(j) 

coeffs.append(A[i][j]) 

return (indices, coeffs) 

  

cpdef row_bounds(self, int index): 

""" 

Return the bounds of a specific constraint. 

  

INPUT: 

  

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

  

OUTPUT: 

  

A pair ``(lower_bound, upper_bound)``. Each of them can be set 

to ``None`` if the constraint is not bounded in the 

corresponding direction, and is a real value otherwise. 

  

EXAMPLES:: 

  

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

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

sage: p.add_variables(5) 

4 

sage: p.add_linear_constraint(zip(range(5), range(5)), 2, 2) 

sage: p.row_bounds(0) 

(2, 2) 

""" 

A, b, c, x = self.lp.Abcx() 

constraint_types = self.lp.constraint_types() 

if constraint_types[index] == '>=': 

return (b[index], None) 

elif constraint_types[index] == '<=': 

return (None, b[index]) 

elif constraint_types[index] == '==': 

return (b[index], b[index]) 

else: 

raise ValueError("Bad constraint_type") 

  

cpdef col_bounds(self, int index): 

""" 

Return the bounds of a specific variable. 

  

INPUT: 

  

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

  

OUTPUT: 

  

A pair ``(lower_bound, upper_bound)``. Each of them can be set 

to ``None`` if the variable is not bounded in the 

corresponding direction, and is a real value otherwise. 

  

EXAMPLES:: 

  

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

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

sage: p.add_variable(lower_bound=None) 

0 

sage: p.col_bounds(0) 

(None, None) 

sage: p.variable_lower_bound(0, 0) 

sage: p.col_bounds(0) 

(0, None) 

""" 

t = self.lp.variable_types()[index] 

if t == ">=": 

return (0, None) 

elif t == "<=": 

return (None, 0) 

elif t == "": 

return (None, None) 

else: 

raise ValueError("Bad _variable_types") 

  

cpdef bint is_variable_binary(self, int index): 

""" 

Test whether the given variable is of binary type. 

  

INPUT: 

  

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

  

EXAMPLES:: 

  

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

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

sage: p.ncols() 

0 

sage: p.add_variable() 

0 

sage: p.is_variable_binary(0) 

False 

  

""" 

return False 

  

cpdef bint is_variable_integer(self, int index): 

""" 

Test whether the given variable is of integer type. 

  

INPUT: 

  

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

  

EXAMPLES:: 

  

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

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

sage: p.ncols() 

0 

sage: p.add_variable() 

0 

sage: p.is_variable_integer(0) 

False 

""" 

return False 

  

cpdef bint is_variable_continuous(self, int index): 

""" 

Test whether the given variable is of continuous/real type. 

  

INPUT: 

  

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

  

EXAMPLES:: 

  

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

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

sage: p.ncols() 

0 

sage: p.add_variable() 

0 

sage: p.is_variable_continuous(0) 

True 

  

""" 

return True 

  

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_backend import get_solver 

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

sage: p.add_linear_constraints(1, 2, None, names=['Empty constraint 1']) 

sage: p.row_name(0) 

'Empty constraint 1' 

  

""" 

return self.row_names[index] 

  

cpdef col_name(self, int index): 

""" 

Return the ``index``-th column name 

  

INPUT: 

  

- ``index`` (integer) -- the column id 

  

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

(default), the method returns the current name. 

  

EXAMPLES:: 

  

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

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

sage: p.add_variable(name="I_am_a_variable") 

0 

sage: p.col_name(0) 

'I_am_a_variable' 

""" 

return str(self.lp.decision_variables()[index]) 

  

cpdef variable_upper_bound(self, int index, value = False): 

""" 

Return or define the upper bound on a variable 

  

INPUT: 

  

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

  

- ``value`` -- real value, or ``None`` to mean that the 

variable has not upper bound. When set to ``None`` 

(default), the method returns the current value. 

  

EXAMPLES:: 

  

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

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

sage: p.add_variable(lower_bound=None) 

0 

sage: p.col_bounds(0) 

(None, None) 

sage: p.variable_upper_bound(0) is None 

True 

sage: p.variable_upper_bound(0, 0) 

sage: p.col_bounds(0) 

(None, 0) 

sage: p.variable_upper_bound(0) 

0 

sage: p.variable_upper_bound(0, None) 

sage: p.variable_upper_bound(0) is None 

True 

""" 

bounds = self.col_bounds(index) 

if value is False: 

return bounds[1] 

else: 

if value != bounds[1]: 

bounds = (bounds[0], value) 

A, b, c, x, constraint_types, variable_types, problem_type, ring, d = self._AbcxCVPRd() 

variable_types = list(variable_types) 

variable_types[index] = self._variable_type_from_bounds(*bounds) 

self.lp = InteractiveLPProblem(A, b, c, x, 

constraint_types, variable_types, 

problem_type, ring, objective_constant_term=d) 

  

cpdef variable_lower_bound(self, int index, value = False): 

""" 

Return or define the lower bound on a variable 

  

INPUT: 

  

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

  

- ``value`` -- real value, or ``None`` to mean that the 

variable has no lower bound. When set to ``None`` 

(default), the method returns the current value. 

  

EXAMPLES:: 

  

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

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

sage: p.add_variable(lower_bound=None) 

0 

sage: p.col_bounds(0) 

(None, None) 

sage: p.variable_lower_bound(0) is None 

True 

sage: p.variable_lower_bound(0, 0) 

sage: p.col_bounds(0) 

(0, None) 

sage: p.variable_lower_bound(0) 

0 

sage: p.variable_lower_bound(0, None) 

sage: p.variable_lower_bound(0) is None 

True 

""" 

bounds = self.col_bounds(index) 

if value is False: 

return bounds[0] 

else: 

if value != bounds[0]: 

bounds = (value, bounds[1]) 

A, b, c, x, constraint_types, variable_types, problem_type, ring, d = self._AbcxCVPRd() 

variable_types = list(variable_types) 

variable_types[index] = self._variable_type_from_bounds(*bounds) 

self.lp = InteractiveLPProblem(A, b, c, x, 

constraint_types, variable_types, 

problem_type, ring, objective_constant_term=d) 

  

cpdef bint is_variable_basic(self, int index): 

""" 

Test whether the given variable is basic. 

  

This assumes that the problem has been solved with the simplex method 

and a basis is available. Otherwise an exception will be raised. 

  

INPUT: 

  

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

  

EXAMPLES:: 

  

sage: p = MixedIntegerLinearProgram(maximization=True,\ 

solver="InteractiveLP") 

sage: x = p.new_variable(nonnegative=True) 

sage: p.add_constraint(-x[0] + x[1] <= 2) 

sage: p.add_constraint(8 * x[0] + 2 * x[1] <= 17) 

sage: p.set_objective(11/2 * x[0] - 3 * x[1]) 

sage: b = p.get_backend() 

sage: # Backend-specific commands to instruct solver to use simplex method here 

sage: b.solve() 

0 

sage: b.is_variable_basic(0) 

True 

sage: b.is_variable_basic(1) 

False 

""" 

return self.lp_std_form.decision_variables()[index] in self.final_dictionary.basic_variables() 

  

cpdef bint is_variable_nonbasic_at_lower_bound(self, int index): 

""" 

Test whether the given variable is nonbasic at lower bound. 

  

This assumes that the problem has been solved with the simplex method 

and a basis is available. Otherwise an exception will be raised. 

  

INPUT: 

  

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

  

EXAMPLES:: 

  

sage: p = MixedIntegerLinearProgram(maximization=True,\ 

solver="InteractiveLP") 

sage: x = p.new_variable(nonnegative=True) 

sage: p.add_constraint(-x[0] + x[1] <= 2) 

sage: p.add_constraint(8 * x[0] + 2 * x[1] <= 17) 

sage: p.set_objective(11/2 * x[0] - 3 * x[1]) 

sage: b = p.get_backend() 

sage: # Backend-specific commands to instruct solver to use simplex method here 

sage: b.solve() 

0 

sage: b.is_variable_nonbasic_at_lower_bound(0) 

False 

sage: b.is_variable_nonbasic_at_lower_bound(1) 

True 

""" 

return self.lp_std_form.decision_variables()[index] in self.final_dictionary.nonbasic_variables() 

  

cpdef bint is_slack_variable_basic(self, int index): 

""" 

Test whether the slack variable of the given row is basic. 

  

This assumes that the problem has been solved with the simplex method 

and a basis is available. Otherwise an exception will be raised. 

  

INPUT: 

  

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

  

EXAMPLES:: 

  

sage: p = MixedIntegerLinearProgram(maximization=True,\ 

solver="InteractiveLP") 

sage: x = p.new_variable(nonnegative=True) 

sage: p.add_constraint(-x[0] + x[1] <= 2) 

sage: p.add_constraint(8 * x[0] + 2 * x[1] <= 17) 

sage: p.set_objective(11/2 * x[0] - 3 * x[1]) 

sage: b = p.get_backend() 

sage: # Backend-specific commands to instruct solver to use simplex method here 

sage: b.solve() 

0 

sage: b.is_slack_variable_basic(0) 

True 

sage: b.is_slack_variable_basic(1) 

False 

""" 

return self.lp_std_form.slack_variables()[index] in self.final_dictionary.basic_variables() 

  

cpdef bint is_slack_variable_nonbasic_at_lower_bound(self, int index): 

""" 

Test whether the given variable is nonbasic at lower bound. 

  

This assumes that the problem has been solved with the simplex method 

and a basis is available. Otherwise an exception will be raised. 

  

INPUT: 

  

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

  

EXAMPLES:: 

  

sage: p = MixedIntegerLinearProgram(maximization=True,\ 

solver="InteractiveLP") 

sage: x = p.new_variable(nonnegative=True) 

sage: p.add_constraint(-x[0] + x[1] <= 2) 

sage: p.add_constraint(8 * x[0] + 2 * x[1] <= 17) 

sage: p.set_objective(11/2 * x[0] - 3 * x[1]) 

sage: b = p.get_backend() 

sage: # Backend-specific commands to instruct solver to use simplex method here 

sage: b.solve() 

0 

sage: b.is_slack_variable_nonbasic_at_lower_bound(0) 

False 

sage: b.is_slack_variable_nonbasic_at_lower_bound(1) 

True 

""" 

return self.lp_std_form.slack_variables()[index] in self.final_dictionary.nonbasic_variables() 

  

cpdef dictionary(self): 

# Proposed addition to the general interface, 

# which would for other solvers return backend dictionaries (#18804) 

""" 

Return a dictionary representing the current basis. 

  

EXAMPLES:: 

  

sage: p = MixedIntegerLinearProgram(maximization=True,\ 

solver="InteractiveLP") 

sage: x = p.new_variable(nonnegative=True) 

sage: p.add_constraint(-x[0] + x[1] <= 2) 

sage: p.add_constraint(8 * x[0] + 2 * x[1] <= 17) 

sage: p.set_objective(11/2 * x[0] - 3 * x[1]) 

sage: b = p.get_backend() 

sage: # Backend-specific commands to instruct solver to use simplex method here 

sage: b.solve() 

0 

sage: d = b.dictionary(); d 

LP problem dictionary ... 

sage: set(d.basic_variables()) 

{x1, x3} 

sage: d.basic_solution() 

(17/8, 0) 

  

TESTS: 

  

Compare with a dictionary obtained in another way:: 

  

sage: lp, basis = p.interactive_lp_problem() 

sage: lp.dictionary(*basis).basic_solution() 

(17/8, 0) 

  

""" 

return self.final_dictionary 

  

cpdef interactive_lp_problem(self): 

  

""" 

Return the :class:`InteractiveLPProblem` object associated with this backend. 

  

EXAMPLES:: 

  

sage: p = MixedIntegerLinearProgram(maximization=True,\ 

solver="InteractiveLP") 

sage: x = p.new_variable(nonnegative=True) 

sage: p.add_constraint(-x[0] + x[1] <= 2) 

sage: p.add_constraint(8 * x[0] + 2 * x[1] <= 17) 

sage: p.set_objective(11/2 * x[0] - 3 * x[1]) 

sage: b = p.get_backend() 

sage: b.interactive_lp_problem() 

LP problem ... 

""" 

return self.lp