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

""" 

Constraints on Linear Functions Tensored with a Free Module 

 

Here is an example of a vector-valued linear function:: 

 

sage: mip.<x> = MixedIntegerLinearProgram('ppl') # base ring is QQ 

sage: x[0] * vector([3,4]) + 1 # vector linear function 

(1, 1) + (3, 4)*x_0 

 

Just like :mod:`~sage.numerical.linear_functions`, (in)equalities 

become symbolic inequalities:: 

 

sage: 3 + x[0] + 2*x[1] <= 10 

3 + x_0 + 2*x_1 <= 10 

sage: x[0] * vector([3,4]) + 1 <= 10 

(1, 1) + (3, 4)*x_0 <= (10, 10) 

sage: x[0] * matrix([[0,0,1],[0,1,0],[1,0,0]]) + x[1] * identity_matrix(3) >= 0 

[0 0 0] [x_1 0 x_0] 

[0 0 0] <= [0 x_0 + x_1 0 ] 

[0 0 0] [x_0 0 x_1] 

""" 

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

# Copyright (C) 2014 Volker Braun <vbraun.name@gmail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 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/ 

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

# python3 

from __future__ import division 

 

from sage.structure.parent import Parent 

from sage.structure.element import Element 

from sage.misc.cachefunc import cached_function 

 

 

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

# 

# Utility functions to test that something is a linear function / constraint 

# 

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

 

def is_LinearTensorConstraint(x): 

""" 

Test whether ``x`` is a constraint on module-valued linear functions. 

 

INPUT: 

 

- ``x`` -- anything. 

 

OUTPUT: 

 

Boolean. 

 

EXAMPLES:: 

 

sage: mip.<x> = MixedIntegerLinearProgram() 

sage: vector_ieq = (x[0] * vector([1,2]) <= x[1] * vector([2,3])) 

sage: from sage.numerical.linear_tensor_constraints import is_LinearTensorConstraint 

sage: is_LinearTensorConstraint(vector_ieq) 

True 

sage: is_LinearTensorConstraint('a string') 

False 

""" 

return isinstance(x, LinearTensorConstraint) 

 

 

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

# 

# Factory functions for the parents to ensure uniqueness 

# 

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

 

@cached_function 

def LinearTensorConstraintsParent(linear_functions_parent): 

""" 

Return the parent for linear functions over ``base_ring``. 

 

The output is cached, so only a single parent is ever constructed 

for a given base ring. 

 

INPUT: 

 

- ``linear_functions_parent`` -- a 

:class:`~sage.numerical.linear_functions.LinearFunctionsParent_class`. The 

type of linear functions that the constraints are made out of. 

 

OUTPUT: 

 

The parent of the linear constraints with the given linear functions. 

 

EXAMPLES:: 

 

sage: from sage.numerical.linear_functions import LinearFunctionsParent 

sage: from sage.numerical.linear_tensor import LinearTensorParent 

sage: from sage.numerical.linear_tensor_constraints import \ 

....: LinearTensorConstraintsParent, LinearTensorConstraintsParent 

sage: LF = LinearFunctionsParent(QQ) 

sage: LT = LinearTensorParent(QQ^2, LF) 

sage: LinearTensorConstraintsParent(LT) 

Linear constraints in the tensor product of Vector space of dimension 2  

over Rational Field and Linear functions over Rational Field 

""" 

return LinearTensorConstraintsParent_class(linear_functions_parent) 

 

 

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

# 

# Elements of linear tensor constraints 

# 

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

 

class LinearTensorConstraint(Element): 

""" 

Formal constraint involving two module-valued linear functions. 

 

.. NOTE:: 

 

In the code, we use "linear tensor" as abbreviation for the 

tensor product (over the common base ring) of a :mod:`linear 

function <sage.numerical.linear_functions>` and a free module 

like a vector/matrix space. 

 

.. warning:: 

 

This class has no reason to be instantiated by the user, and 

is meant to be used by instances of 

:class:`MixedIntegerLinearProgram`. 

 

INPUT: 

 

- ``parent`` -- the parent, a 

:class:`LinearTensorConstraintsParent_class` 

 

- ``lhs``, ``rhs`` -- two 

:class:`sage.numerical.linear_tensor_element.LinearTensor`. The 

left and right hand side of the constraint (in)equality. 

 

- ``equality`` -- boolean (default: ``False``). Whether the 

constraint is an equality. If ``False``, it is a ``<=`` 

inequality. 

 

EXAMPLES:: 

 

sage: mip.<b> = MixedIntegerLinearProgram() 

sage: (b[2]+2*b[3]) * vector([1,2]) <= b[8] * vector([2,3]) - 5 

(1.0, 2.0)*x_0 + (2.0, 4.0)*x_1 <= (-5.0, -5.0) + (2.0, 3.0)*x_2 

""" 

 

def __init__(self, parent, lhs, rhs, equality): 

r""" 

Constructor for ``LinearTensorConstraint`` 

 

INPUT: 

 

See :class:`LinearTensorConstraint`. 

 

EXAMPLES:: 

 

sage: mip.<b> = MixedIntegerLinearProgram() 

sage: b[2] * vector([1,2]) + 2*b[3] <= 0 

(1.0, 2.0)*x_0 + (2.0, 2.0)*x_1 <= (0.0, 0.0) 

""" 

super(LinearTensorConstraint, self).__init__(parent) 

self._lhs = lhs 

self._rhs = rhs 

self._equality = equality 

 

def is_equation(self): 

""" 

Whether the constraint is a chained equation 

 

OUTPUT: 

 

Boolean. 

 

EXAMPLES:: 

 

sage: mip.<b> = MixedIntegerLinearProgram() 

sage: (b[0] * vector([1,2]) == 0).is_equation() 

True 

sage: (b[0] * vector([1,2]) >= 0).is_equation() 

False 

""" 

return self._equality 

 

def is_less_or_equal(self): 

""" 

Whether the constraint is a chained less-or_equal inequality 

 

OUTPUT: 

 

Boolean. 

 

EXAMPLES:: 

 

sage: mip.<b> = MixedIntegerLinearProgram() 

sage: (b[0] * vector([1,2]) == 0).is_less_or_equal() 

False 

sage: (b[0] * vector([1,2]) >= 0).is_less_or_equal() 

True 

""" 

return not self._equality 

 

def lhs(self): 

""" 

Return the left side of the (in)equality. 

 

OUTPUT: 

 

Instance of 

:class:`sage.numerical.linear_tensor_element.LinearTensor`. A 

linear function valued in a free module. 

 

EXAMPLES:: 

 

sage: mip.<x> = MixedIntegerLinearProgram() 

sage: (x[0] * vector([1,2]) == 0).lhs() 

(1.0, 2.0)*x_0  

""" 

return self._lhs 

 

def rhs(self): 

""" 

Return the right side of the (in)equality. 

 

OUTPUT: 

 

Instance of 

:class:`sage.numerical.linear_tensor_element.LinearTensor`. A 

linear function valued in a free module. 

 

EXAMPLES:: 

 

sage: mip.<x> = MixedIntegerLinearProgram() 

sage: (x[0] * vector([1,2]) == 0).rhs() 

(0.0, 0.0) 

""" 

return self._rhs 

 

def _ascii_art_(self): 

""" 

Return Ascii Art 

 

OUTPUT: 

 

Ascii art of the constraint (in)equality. 

 

EXAMPLES:: 

 

sage: mip.<x> = MixedIntegerLinearProgram() 

sage: ascii_art(x[0] * vector([1,2]) >= 0) 

(0.0, 0.0) <= (1.0, 2.0)*x_0 

sage: ascii_art(x[0] * matrix([[1,2],[3,4]]) >= 0)  

[0 0] <= [x_0 2*x_0] 

[0 0] [3*x_0 4*x_0] 

""" 

from sage.typeset.ascii_art import AsciiArt 

 

def matrix_art(m): 

lines = str(m).splitlines() 

return AsciiArt(lines, baseline=len(lines) // 2) 

comparator = AsciiArt([' == ' if self.is_equation() else ' <= ']) 

return matrix_art(self.lhs()) + comparator + matrix_art(self.rhs()) 

 

def _repr_(self): 

r""" 

Returns a string representation of the constraint. 

 

OUTPUT: 

 

String. 

 

EXAMPLES:: 

 

sage: mip.<b> = MixedIntegerLinearProgram() 

sage: b[3] * vector([1,2]) <= (b[8] + 9) * vector([2,3]) 

(1.0, 2.0)*x_0 <= (18.0, 27.0) + (2.0, 3.0)*x_1 

sage: b[3] * vector([1,2]) == (b[8] + 9) * vector([2,3]) 

(1.0, 2.0)*x_0 == (18.0, 27.0) + (2.0, 3.0)*x_1 

sage: b[0] * identity_matrix(3) == 0 

[x_2 0 0 ] [0 0 0] 

[0 x_2 0 ] == [0 0 0] 

[0 0 x_2] [0 0 0] 

""" 

if self.parent().linear_tensors().is_matrix_space(): 

return str(self._ascii_art_()) 

comparator = (' == ' if self.is_equation() else ' <= ') 

return str(self.lhs()) + comparator + str(self.rhs()) 

 

 

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

# 

# Parent of linear constraints 

# 

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

 

class LinearTensorConstraintsParent_class(Parent): 

""" 

Parent for :class:`LinearTensorConstraint` 

 

.. warning:: 

 

This class has no reason to be instantiated by the user, and 

is meant to be used by instances of 

:class:`MixedIntegerLinearProgram`. Also, use the 

:func:`LinearTensorConstraintsParent` factory function. 

 

INPUT/OUTPUT: 

 

See :func:`LinearTensorConstraintsParent` 

 

EXAMPLES:: 

 

sage: p = MixedIntegerLinearProgram() 

sage: LT = p.linear_functions_parent().tensor(RDF^2); LT 

Tensor product of Vector space of dimension 2 over Real Double  

Field and Linear functions over Real Double Field 

sage: from sage.numerical.linear_tensor_constraints import LinearTensorConstraintsParent 

sage: LTC = LinearTensorConstraintsParent(LT); LTC 

Linear constraints in the tensor product of Vector space of  

dimension 2 over Real Double Field and Linear functions over  

Real Double Field 

sage: type(LTC) 

<class 'sage.numerical.linear_tensor_constraints.LinearTensorConstraintsParent_class'> 

""" 

Element = LinearTensorConstraint 

 

def __init__(self, linear_tensor_parent): 

""" 

The Python constructor 

 

INPUT: 

 

- ``linear_tensor_parent`` -- instance of 

:class:`LinearTensorParent_class`. 

 

TESTS:: 

 

sage: from sage.numerical.linear_functions import LinearFunctionsParent 

sage: LF = LinearFunctionsParent(RDF) 

sage: from sage.numerical.linear_tensor import LinearTensorParent 

sage: LT = LinearTensorParent(RDF^2, LF) 

sage: from sage.numerical.linear_tensor_constraints import LinearTensorConstraintsParent 

sage: LinearTensorConstraintsParent(LT) 

Linear constraints in the tensor product of Vector space of  

dimension 2 over Real Double Field and Linear functions over 

Real Double Field 

""" 

Parent.__init__(self) 

self._LT = linear_tensor_parent 

self._LF = linear_tensor_parent.linear_functions() 

 

def linear_tensors(self): 

""" 

Return the parent for the linear functions 

 

OUTPUT: 

 

Instance of :class:`sage.numerical.linear_tensor.LinearTensorParent_class`. 

 

EXAMPLES:: 

 

sage: mip.<x> = MixedIntegerLinearProgram() 

sage: ieq = (x[0] * vector([1,2]) >= 0) 

sage: ieq.parent().linear_tensors() 

Tensor product of Vector space of dimension 2 over Real Double 

Field and Linear functions over Real Double Field 

""" 

return self._LT 

 

def linear_functions(self): 

""" 

Return the parent for the linear functions 

 

OUTPUT: 

 

Instance of :class:`sage.numerical.linear_functions.LinearFunctionsParent_class`. 

 

EXAMPLES:: 

 

sage: mip.<x> = MixedIntegerLinearProgram() 

sage: ieq = (x[0] * vector([1,2]) >= 0) 

sage: ieq.parent().linear_functions() 

Linear functions over Real Double Field 

""" 

return self._LF 

 

def _repr_(self): 

""" 

Return a string representation 

 

OUTPUT: 

 

String. 

 

EXAMPLES:: 

 

sage: mip.<x> = MixedIntegerLinearProgram() 

sage: ieq = (x[0] * vector([1,2]) >= 0) 

sage: ieq.parent() # indirect doctests 

Linear constraints in the tensor product of Vector space of 

dimension 2 over Real Double Field and Linear functions over  

Real Double Field 

""" 

return 'Linear constraints in the tensor product of {0} and {1}'.format( 

self.linear_tensors().free_module(), self.linear_functions()) 

 

def _element_constructor_(self, left, right, equality): 

""" 

Construct a :class:`LinearConstraint`. 

 

INPUT: 

 

- ``left`` -- a :class:`LinearTensor`, or something that can 

be converted into one, a list/tuple of 

:class:`LinearTensor`, or an existing 

:class:`LinearTensorConstraint`. 

 

- ``right`` -- a :class:`LinearTensor` or ``None`` 

(default). 

 

- ``equality`` -- boolean. Whether to 

construct an equation or a less-or-equal inequality. 

 

OUTPUT: 

 

The :class:`LinearTensorConstraint` constructed from the input data. 

 

EXAMPLES:: 

 

sage: mip.<x> = MixedIntegerLinearProgram() 

sage: ieq = (x[0] * vector([1,2]) >= 0) 

sage: LTC = ieq.parent() 

sage: LTC._element_constructor_(1, 2, True) 

(1.0, 1.0) == (2.0, 2.0) 

sage: LTC(x[0], x[1], False) 

(1.0, 1.0)*x_0 <= (1.0, 1.0)*x_1 

sage: type(_) 

<class 'sage.numerical.linear_tensor_constraints.LinearTensorConstraintsParent_class.element_class'> 

""" 

LT = self.linear_tensors() 

left = LT(left) 

right = LT(right) 

equality = bool(equality) 

return self.element_class(self, left, right, equality) 

 

def _an_element_(self): 

""" 

Returns an element 

 

EXAMPLES:: 

 

sage: mip.<x> = MixedIntegerLinearProgram() 

sage: ieq = (x[0] * vector([1,2]) >= 0) 

sage: ieq.parent().an_element() # indirect doctest 

(0.0, 0.0) <= (1.0, 0.0) + (5.0, 0.0)*x_2 + (7.0, 0.0)*x_5 

""" 

LT = self.linear_tensors() 

return LT.an_element() >= 0