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

r""" 

Knapsack Problems 

 

This module implements a number of solutions to various knapsack problems, 

otherwise known as linear integer programming problems. Solutions to the 

following knapsack problems are implemented: 

 

- Solving the subset sum problem for super-increasing sequences. 

- General case using Linear Programming 

 

AUTHORS: 

 

- Minh Van Nguyen (2009-04): initial version 

- Nathann Cohen (2009-08): Linear Programming version 

 

Definition of Knapsack problems 

------------------------------- 

 

You have already had a knapsack problem, so you should know, but in case you do 

not, a knapsack problem is what happens when you have hundred of items to put 

into a bag which is too small, and you want to pack the most useful of them. 

 

When you formally write it, here is your problem: 

 

* Your bag can contain a weight of at most `W`. 

* Each item `i` has a weight `w_i`. 

* Each item `i` has a usefulness `u_i`. 

 

You then want to maximize the total usefulness of the items you will store into 

your bag, while keeping sure the weight of the bag will not go over `W`. 

 

As a linear program, this problem can be represented this way (if you define 

`b_i` as the binary variable indicating whether the item `i` is to be included 

in your bag): 

 

.. MATH:: 

 

\mbox{Maximize: }\sum_i b_i u_i \\ 

\mbox{Such that: } 

\sum_i b_i w_i \leq W \\ 

\forall i, b_i \mbox{ binary variable} \\ 

 

(For more information, see the :wikipedia:`Knapsack_problem`) 

 

Examples 

-------- 

 

If your knapsack problem is composed of three items (weight, value) 

defined by (1,2), (1.5,1), (0.5,3), and a bag of maximum weight 2, 

you can easily solve it this way:: 

 

sage: from sage.numerical.knapsack import knapsack 

sage: knapsack( [(1,2), (1.5,1), (0.5,3)], max=2) 

[5.0, [(1, 2), (0.500000000000000, 3)]] 

 

Super-increasing sequences 

-------------------------- 

 

We can test for whether or not a sequence is super-increasing:: 

 

sage: from sage.numerical.knapsack import Superincreasing 

sage: L = [1, 2, 5, 21, 69, 189, 376, 919] 

sage: seq = Superincreasing(L) 

sage: seq 

Super-increasing sequence of length 8 

sage: seq.is_superincreasing() 

True 

sage: Superincreasing().is_superincreasing([1,3,5,7]) 

False 

 

Solving the subset sum problem for a super-increasing sequence 

and target sum:: 

 

sage: L = [1, 2, 5, 21, 69, 189, 376, 919] 

sage: Superincreasing(L).subset_sum(98) 

[69, 21, 5, 2, 1] 

""" 

 

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

# Copyright (C) 2009 Minh Van Nguyen <nguyenminh2@gmail.com> 

# 

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

# 

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

# 

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

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

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

 

from sage.misc.latex import latex 

from sage.rings.integer import Integer 

from sage.structure.sage_object import SageObject 

 

class Superincreasing(SageObject): 

r""" 

A class for super-increasing sequences. 

 

Let `L = (a_1, a_2, a_3, \dots, a_n)` be a non-empty sequence of 

non-negative integers. Then `L` is said to be super-increasing if 

each `a_i` is strictly greater than the sum of all previous values. 

That is, for each `a_i \in L` the sequence `L` must satisfy the property 

 

.. MATH:: 

 

a_i > \sum_{k=1}^{i-1} a_k 

 

in order to be called a super-increasing sequence, where `|L| \geq 2`. 

If `L` has only one element, it is also defined to be a 

super-increasing sequence. 

 

If ``seq`` is ``None``, then construct an empty sequence. By 

definition, this empty sequence is not super-increasing. 

 

INPUT: 

 

- ``seq`` -- (default: ``None``) a non-empty sequence. 

 

EXAMPLES:: 

 

sage: from sage.numerical.knapsack import Superincreasing 

sage: L = [1, 2, 5, 21, 69, 189, 376, 919] 

sage: Superincreasing(L).is_superincreasing() 

True 

sage: Superincreasing().is_superincreasing([1,3,5,7]) 

False 

sage: seq = Superincreasing(); seq 

An empty sequence. 

sage: seq = Superincreasing([1, 3, 6]); seq 

Super-increasing sequence of length 3 

sage: seq = Superincreasing(list([1, 2, 5, 21, 69, 189, 376, 919])); seq 

Super-increasing sequence of length 8 

""" 

 

def __init__(self, seq=None): 

r""" 

Constructing a super-increasing sequence object from ``seq``. 

 

If ``seq`` is ``None``, then construct an empty sequence. By 

definition, this empty sequence is not super-increasing. 

 

 

INPUT: 

 

- ``seq`` -- (default: ``None``) a non-empty sequence. 

 

 

EXAMPLES:: 

 

sage: from sage.numerical.knapsack import Superincreasing 

sage: L = [1, 2, 5, 21, 69, 189, 376, 919] 

sage: Superincreasing(L).is_superincreasing() 

True 

sage: Superincreasing().is_superincreasing([1,3,5,7]) 

False 

""" 

# argument seq is None, so construct an empty sequence 

if seq is None: 

self._seq = None 

# now seq is known to be not None, so try to construct a 

# super-increasing sequence from seq 

else: 

if self.is_superincreasing(seq): 

self._seq = seq 

else: 

raise ValueError("seq must be a super-increasing sequence") 

 

def __eq__(self, other): 

r""" 

Comparing ``self`` to ``other``. 

 

TESTS:: 

 

sage: from sage.numerical.knapsack import Superincreasing 

sage: L = [1, 2, 5, 21, 69, 189, 376, 919] 

sage: seq = Superincreasing(L) 

sage: seq == loads(dumps(seq)) 

True 

""" 

return self._seq == other._seq 

 

def __ne__(self, other): 

r""" 

Comparing ``self`` to ``other``. 

 

TESTS:: 

 

sage: from sage.numerical.knapsack import Superincreasing 

sage: L = [1, 2, 5, 21, 69, 189, 376, 919] 

sage: seq = Superincreasing(L) 

sage: seq != seq 

False 

""" 

return not self.__eq__(other) 

 

def __repr__(self): 

r""" 

Return a string representation of this super-increasing 

sequence object. 

 

 

EXAMPLES:: 

 

sage: from sage.numerical.knapsack import Superincreasing 

sage: seq = Superincreasing(); seq 

An empty sequence. 

sage: seq = Superincreasing([1, 3, 6]); seq 

Super-increasing sequence of length 3 

sage: seq = Superincreasing(list([1, 2, 5, 21, 69, 189, 376, 919])); seq 

Super-increasing sequence of length 8 

""" 

if self._seq is None: 

return "An empty sequence." 

else: 

return "Super-increasing sequence of length %s" % len(self._seq) 

 

def largest_less_than(self, N): 

r""" 

Return the largest integer in the sequence ``self`` that is less than 

or equal to ``N``. 

 

This function narrows down the candidate solution using a binary trim, 

similar to the way binary search halves the sequence at each iteration. 

 

 

INPUT: 

 

- ``N`` -- integer; the target value to search for. 

 

 

OUTPUT: 

 

The largest integer in ``self`` that is less than or equal to ``N``. If 

no solution exists, then return ``None``. 

 

 

EXAMPLES: 

 

When a solution is found, return it:: 

 

sage: from sage.numerical.knapsack import Superincreasing 

sage: L = [2, 3, 7, 25, 67, 179, 356, 819] 

sage: Superincreasing(L).largest_less_than(207) 

179 

sage: L = (2, 3, 7, 25, 67, 179, 356, 819) 

sage: Superincreasing(L).largest_less_than(2) 

2 

 

But if no solution exists, return ``None``:: 

 

sage: L = [2, 3, 7, 25, 67, 179, 356, 819] 

sage: Superincreasing(L).largest_less_than(-1) is None 

True 

 

TESTS: 

 

The target ``N`` must be an integer:: 

 

sage: from sage.numerical.knapsack import Superincreasing 

sage: L = [2, 3, 7, 25, 67, 179, 356, 819] 

sage: Superincreasing(L).largest_less_than(2.30) 

Traceback (most recent call last): 

... 

TypeError: N (= 2.30000000000000) must be an integer. 

 

The sequence that ``self`` represents must also be non-empty:: 

 

sage: Superincreasing([]).largest_less_than(2) 

Traceback (most recent call last): 

... 

ValueError: seq must be a super-increasing sequence 

sage: Superincreasing(list()).largest_less_than(2) 

Traceback (most recent call last): 

... 

ValueError: seq must be a super-increasing sequence 

""" 

from sage.functions.other import Function_floor 

floor = Function_floor() 

# input error handling 

if len(self._seq) == 0: 

raise TypeError("self must be a non-empty list of integers.") 

if (not isinstance(N, Integer)) and (not isinstance(N, int)): 

raise TypeError("N (= %s) must be an integer." % N) 

 

# halving the list at each iteration, just like binary search 

# TODO: some error handling to ensure that self only contains integers? 

low = 0 

high = len(self._seq) - 1 

while low <= high: 

mid = floor((low + high) / 2) 

if N == self._seq[mid]: 

return self._seq[mid] 

if N < self._seq[mid]: 

high = mid - 1 

else: 

low = mid + 1 

if N >= self._seq[high]: 

return self._seq[high] 

else: 

return None 

 

def _latex_(self): 

r""" 

Return LaTeX representation of ``self``. 

 

EXAMPLES:: 

 

sage: from sage.numerical.knapsack import Superincreasing 

sage: latex(Superincreasing()) 

\left[\right] 

sage: seq = Superincreasing([1, 2, 5, 21, 69, 189, 376, 919]) 

sage: latex(seq) 

<BLANKLINE> 

\left[1, 

2, 

5, 

21, 

69, 

189, 

376, 

919\right] 

""" 

if self._seq is None: 

return latex([]) 

else: 

return latex(self._seq) 

 

def is_superincreasing(self, seq=None): 

r""" 

Determine whether or not ``seq`` is super-increasing. 

 

If ``seq=None`` then determine whether or not ``self`` is 

super-increasing. 

 

Let `L = (a_1, a_2, a_3, \dots, a_n)` be a non-empty sequence of 

non-negative integers. Then `L` is said to be super-increasing if 

each `a_i` is strictly greater than the sum of all previous values. 

That is, for each `a_i \in L` the sequence `L` must satisfy the 

property 

 

.. MATH:: 

 

a_i > \sum_{k=1}^{i-1} a_k 

 

in order to be called a super-increasing sequence, where `|L| \geq 2`. 

If `L` has exactly one element, then it is also defined to be a 

super-increasing sequence. 

 

 

INPUT: 

 

- ``seq`` -- (default: ``None``) a sequence to test 

 

 

OUTPUT: 

 

- If ``seq`` is ``None``, then test ``self`` to determine whether or 

not it is super-increasing. In that case, return ``True`` if 

``self`` is super-increasing; ``False`` otherwise. 

 

- If ``seq`` is not ``None``, then test ``seq`` to determine whether 

or not it is super-increasing. Return ``True`` if ``seq`` is 

super-increasing; ``False`` otherwise. 

 

 

EXAMPLES: 

 

By definition, an empty sequence is not super-increasing:: 

 

sage: from sage.numerical.knapsack import Superincreasing 

sage: Superincreasing().is_superincreasing([]) 

False 

sage: Superincreasing().is_superincreasing() 

False 

sage: Superincreasing().is_superincreasing(tuple()) 

False 

sage: Superincreasing().is_superincreasing(()) 

False 

 

But here is an example of a super-increasing sequence:: 

 

sage: L = [1, 2, 5, 21, 69, 189, 376, 919] 

sage: Superincreasing(L).is_superincreasing() 

True 

sage: L = (1, 2, 5, 21, 69, 189, 376, 919) 

sage: Superincreasing(L).is_superincreasing() 

True 

 

A super-increasing sequence can have zero as one of its elements:: 

 

sage: L = [0, 1, 2, 4] 

sage: Superincreasing(L).is_superincreasing() 

True 

 

A super-increasing sequence can be of length 1:: 

 

sage: Superincreasing([randint(0, 100)]).is_superincreasing() 

True 

 

 

TESTS: 

 

The sequence must contain only integers:: 

 

sage: from sage.numerical.knapsack import Superincreasing 

sage: L = [1.0, 2.1, pi, 21, 69, 189, 376, 919] 

sage: Superincreasing(L).is_superincreasing() 

Traceback (most recent call last): 

... 

TypeError: Element e (= 1.00000000000000) of seq must be a non-negative integer. 

sage: L = [1, 2.1, pi, 21, 69, 189, 376, 919] 

sage: Superincreasing(L).is_superincreasing() 

Traceback (most recent call last): 

... 

TypeError: Element e (= 2.10000000000000) of seq must be a non-negative integer. 

""" 

# argument seq is None, so test self for super-increasing 

if seq is None: 

# self must be a non-empty sequence 

if (self._seq is None) or len(self._seq) == 0: 

return False 

# so now self is known to represent a non-empty sequence 

if (not isinstance(self._seq[0], Integer)) and (not isinstance(self._seq[0], int)): 

raise TypeError("Element e (= %s) of self must be a non-negative integer." % self._seq[0]) 

if self._seq[0] < 0: 

raise TypeError("Element e (= %s) of self must be a non-negative integer." % self._seq[0]) 

cumSum = self._seq[0] # the cumulative sum of the sequence represented by self 

for e in self._seq[1:]: 

if (not isinstance(e, Integer)) and (not isinstance(e, int)): 

raise TypeError("Element e (= %s) of self must be a non-negative integer." % e) 

if e < 0: 

raise TypeError("Element e (= %s) of self must be a non-negative integer." % e) 

if e <= cumSum: 

return False 

cumSum += e 

return True 

# now we know that seq is not None, so test seq for super-increasing 

else: 

# seq must be a non-empty sequence 

if len(seq) == 0: 

return False 

# so now seq is known to represent a non-empty sequence 

if (not isinstance(seq[0], Integer)) and (not isinstance(seq[0], int)): 

raise TypeError("Element e (= %s) of seq must be a non-negative integer." % seq[0]) 

if seq[0] < 0: 

raise TypeError("Element e (= %s) of seq must be a non-negative integer." % seq[0]) 

cumSum = seq[0] # the cumulative sum of the sequence seq 

for e in seq[1:]: 

if (not isinstance(e, Integer)) and (not isinstance(e, int)): 

raise TypeError("Element e (= %s) of seq must be a non-negative integer." % e) 

if e < 0: 

raise TypeError("Element e (= %s) of seq must be a non-negative integer." % e) 

if e <= cumSum: 

return False 

cumSum += e 

return True 

 

def subset_sum(self, N): 

r""" 

Solving the subset sum problem for a super-increasing sequence. 

 

Let `S = (s_1, s_2, s_3, \dots, s_n)` be a non-empty sequence of 

non-negative integers, and let `N \in \ZZ` be non-negative. The 

subset sum problem asks for a subset `A \subseteq S` all of whose 

elements sum to `N`. This method specializes the subset sum problem 

to the case of super-increasing sequences. If a solution exists, then 

it is also a super-increasing sequence. 

 

.. NOTE:: 

 

This method only solves the subset sum problem for 

super-increasing sequences. In general, solving the subset sum 

problem for an arbitrary sequence is known to be computationally 

hard. 

 

 

INPUT: 

 

- ``N`` -- a non-negative integer. 

 

 

OUTPUT: 

 

- A non-empty subset of ``self`` whose elements sum to ``N``. This 

subset is also a super-increasing sequence. If no such subset 

exists, then return the empty list. 

 

 

ALGORITHMS: 

 

The algorithm used is adapted from page 355 of [HPS2008]_. 

 

 

EXAMPLES: 

 

Solving the subset sum problem for a super-increasing sequence 

and target sum:: 

 

sage: from sage.numerical.knapsack import Superincreasing 

sage: L = [1, 2, 5, 21, 69, 189, 376, 919] 

sage: Superincreasing(L).subset_sum(98) 

[69, 21, 5, 2, 1] 

 

 

TESTS: 

 

The target ``N`` must be a non-negative integer:: 

 

sage: from sage.numerical.knapsack import Superincreasing 

sage: L = [0, 1, 2, 4] 

sage: Superincreasing(L).subset_sum(-6) 

Traceback (most recent call last): 

... 

TypeError: N (= -6) must be a non-negative integer. 

sage: Superincreasing(L).subset_sum(-6.2) 

Traceback (most recent call last): 

... 

TypeError: N (= -6.20000000000000) must be a non-negative integer. 

 

The sequence that ``self`` represents must only contain non-negative 

integers:: 

 

sage: L = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1] 

sage: Superincreasing(L).subset_sum(1) 

Traceback (most recent call last): 

... 

TypeError: Element e (= -10) of seq must be a non-negative integer. 

""" 

# input error handling 

if not self.is_superincreasing(): 

raise TypeError("self is not super-increasing. Only super-increasing sequences are currently supported.") 

if (not isinstance(N, Integer)) and (not isinstance(N, int)): 

raise TypeError("N (= %s) must be a non-negative integer." % N) 

if N < 0: 

raise TypeError("N (= %s) must be a non-negative integer." % N) 

 

# solve subset sum problem for super-increasing sequence 

candidates = [] 

a = self.largest_less_than(N) 

while a is not None: 

candidates.append(a) 

a = self.largest_less_than(N - sum(candidates)) 

 

lst = list(set(candidates)) # removing any duplicate elements 

if len(lst) != len(candidates): 

return [] 

if sum(candidates) == N: 

return candidates 

else: 

return [] 

 

def knapsack(seq, binary=True, max=1, value_only=False, solver=None, verbose=0): 

r""" 

Solves the knapsack problem 

 

For more information on the knapsack problem, see the documentation of the 

:mod:`knapsack module <sage.numerical.knapsack>` or the 

:wikipedia:`Knapsack_problem`. 

 

INPUT: 

 

- ``seq`` -- Two different possible types: 

 

- A sequence of tuples ``(weight, value, something1, something2, 

...)``. Note that only the first two coordinates (``weight`` and 

``values``) will be taken into account. The rest (if any) will be 

ignored. This can be useful if you need to attach some information to 

the items. 

 

- A sequence of reals (a value of 1 is assumed). 

 

- ``binary`` -- When set to ``True``, an item can be taken 0 or 1 time. 

When set to ``False``, an item can be taken any amount of times (while 

staying integer and positive). 

 

- ``max`` -- Maximum admissible weight. 

 

- ``value_only`` -- When set to ``True``, only the maximum useful value is 

returned. When set to ``False``, both the maximum useful value and an 

assignment are returned. 

 

- ``solver`` -- (default: ``None``) Specify a Linear Program (LP) solver to 

be used. If set to ``None``, the default one is used. For more information 

on LP solvers and which default solver is used, see the documentation of 

class :class:`MixedIntegerLinearProgram 

<sage.numerical.mip.MixedIntegerLinearProgram>`. 

 

- ``verbose`` -- integer (default: ``0``). Sets the level of verbosity. Set 

to 0 by default, which means quiet. 

 

OUTPUT: 

 

If ``value_only`` is set to ``True``, only the maximum useful value is 

returned. Else (the default), the function returns a pair ``[value,list]``, 

where ``list`` can be of two types according to the type of ``seq``: 

 

- The list of tuples `(w_i, u_i, ...)` occurring in the solution. 

 

- A list of reals where each real is repeated the number of times it is 

taken into the solution. 

 

EXAMPLES: 

 

If your knapsack problem is composed of three items ``(weight, value)`` 

defined by ``(1,2), (1.5,1), (0.5,3)``, and a bag of maximum weight `2`, you 

can easily solve it this way:: 

 

sage: from sage.numerical.knapsack import knapsack 

sage: knapsack( [(1,2), (1.5,1), (0.5,3)], max=2) 

[5.0, [(1, 2), (0.500000000000000, 3)]] 

 

sage: knapsack( [(1,2), (1.5,1), (0.5,3)], max=2, value_only=True) 

5.0 

 

Besides weight and value, you may attach any data to the items:: 

 

sage: from sage.numerical.knapsack import knapsack 

sage: knapsack( [(1, 2, 'spam'), (0.5, 3, 'a', 'lot')]) 

[3.0, [(0.500000000000000, 3, 'a', 'lot')]] 

 

In the case where all the values (usefulness) of the items are equal to one, 

you do not need embarrass yourself with the second values, and you can just 

type for items `(1,1), (1.5,1), (0.5,1)` the command:: 

 

sage: from sage.numerical.knapsack import knapsack 

sage: knapsack([1,1.5,0.5], max=2, value_only=True) 

2.0 

""" 

reals = not isinstance(seq[0], tuple) 

if reals: 

seq = [(x,1) for x in seq] 

 

from sage.numerical.mip import MixedIntegerLinearProgram 

p = MixedIntegerLinearProgram(solver=solver, maximization=True) 

 

if binary: 

present = p.new_variable(binary = True) 

else: 

present = p.new_variable(integer = True) 

 

p.set_objective(p.sum([present[i] * seq[i][1] for i in range(len(seq))])) 

p.add_constraint(p.sum([present[i] * seq[i][0] for i in range(len(seq))]), max=max) 

 

if value_only: 

return p.solve(objective_only=True, log=verbose) 

 

else: 

objective = p.solve(log=verbose) 

present = p.get_values(present) 

 

val = [] 

 

if reals: 

[val.extend([seq[i][0]] * int(present[i])) for i in range(len(seq))] 

else: 

[val.extend([seq[i]] * int(present[i])) for i in range(len(seq))] 

 

return [objective,val]