or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

advanced-extensions.mdcore-modeling.mddae.mddata-management.mddomain-sets.mdgdp.mdindex.mdmathematical-functions.mdmpec.mdoptimization-interface.md

core-modeling.mddocs/

0

# Core Modeling Components

1

2

Essential modeling components for creating optimization problems in Pyomo. These components form the foundation of all Pyomo optimization models, providing the building blocks for variables, constraints, objectives, parameters, and model structure.

3

4

## Capabilities

5

6

### Model Classes

7

8

Base model classes for creating optimization problems with support for both abstract template models and concrete models with specific data.

9

10

```python { .api }

11

class Model:

12

"""Base model class for optimization problems."""

13

def __init__(self, name=None): ...

14

def nvariables(self): ...

15

def nconstraints(self): ...

16

def nobjectives(self): ...

17

def compute_statistics(self): ...

18

statistics: object

19

solutions: object

20

config: object

21

22

class ConcreteModel(Model):

23

"""Model with concrete data values."""

24

def __init__(self, name=None): ...

25

26

class AbstractModel(Model):

27

"""Abstract model template requiring data instantiation."""

28

def __init__(self, name=None): ...

29

def create_instance(self, data=None, name=None, namespace=None, namespaces=None, profile_memory=None, report_timing=False): ...

30

def load(self, data, namespace=None): ...

31

32

class Block:

33

"""Block component for hierarchical modeling."""

34

def __init__(self, *args, **kwargs): ...

35

36

class ScalarBlock(Block):

37

"""Scalar block component."""

38

def __init__(self, *args, **kwargs): ...

39

```

40

41

### Variable Components

42

43

Decision variable components with domain support, bounds, and initialization options for linear and nonlinear optimization problems.

44

45

```python { .api }

46

class Var:

47

"""Decision variable component."""

48

def __init__(self, *args, domain=None, bounds=None, initialize=None, within=None, units=None, **kwargs): ...

49

def set_value(self, value): ...

50

def fix(self, value=None): ...

51

def unfix(self): ...

52

def is_fixed(self): ...

53

@property

54

def bounds(self): ...

55

@property

56

def domain(self): ...

57

@domain.setter

58

def domain(self, value): ...

59

def has_lb(self): ...

60

def has_ub(self): ...

61

def setlb(self, val): ...

62

def setub(self, val): ...

63

@property

64

def lb(self): ...

65

@property

66

def ub(self): ...

67

@property

68

def lower(self): ...

69

@property

70

def upper(self): ...

71

@property

72

def stale(self): ...

73

def get_units(self): ...

74

75

class ScalarVar(Var):

76

"""Scalar variable component."""

77

def __init__(self, *args, **kwargs): ...

78

79

class VarList:

80

"""Variable list component for dynamic variable creation."""

81

def __init__(self, **kwargs): ...

82

def add(self): ...

83

84

class BooleanVar(Var):

85

"""Boolean variable component."""

86

def __init__(self, *args, **kwargs): ...

87

def get_associated_binary(self): ...

88

def associate_binary_var(self, binary_var): ...

89

@property

90

def stale(self): ...

91

@property

92

def domain(self): ...

93

94

class BooleanVarList:

95

"""Boolean variable list component."""

96

def __init__(self, **kwargs): ...

97

def add(self): ...

98

99

class ScalarBooleanVar(BooleanVar):

100

"""Scalar boolean variable component."""

101

def __init__(self, *args, **kwargs): ...

102

```

103

104

### Constraint Components

105

106

Constraint components for defining equality and inequality constraints with support for indexed constraints and logical constraints.

107

108

```python { .api }

109

class Constraint:

110

"""Constraint component for optimization problems."""

111

def __init__(self, *args, **kwargs): ...

112

def activate(self): ...

113

def deactivate(self): ...

114

def is_active(self): ...

115

def to_bounded_expression(self): ...

116

@property

117

def body(self): ...

118

@property

119

def lower(self): ...

120

@property

121

def upper(self): ...

122

@property

123

def equality(self): ...

124

def has_lb(self): ...

125

def has_ub(self): ...

126

def set_value(self, expr): ...

127

128

class ConstraintList:

129

"""Constraint list component for dynamic constraint creation."""

130

def __init__(self, **kwargs): ...

131

def add(self, expr): ...

132

133

class LogicalConstraint:

134

"""Logical constraint component for Boolean relationships."""

135

def __init__(self, *args, **kwargs): ...

136

137

class LogicalConstraintList:

138

"""Logical constraint list component."""

139

def __init__(self, **kwargs): ...

140

def add(self, expr): ...

141

```

142

143

### Objective Components

144

145

Objective function components supporting minimization and maximization with single and multiple objective capabilities.

146

147

```python { .api }

148

class Objective:

149

"""Objective function component."""

150

def __init__(self, *args, **kwargs): ...

151

def activate(self): ...

152

def deactivate(self): ...

153

def is_active(self): ...

154

def is_minimizing(self): ...

155

def set_sense(self, sense): ...

156

@property

157

def sense(self): ...

158

@sense.setter

159

def sense(self, value): ...

160

161

class ObjectiveList:

162

"""Objective list component for multiple objectives."""

163

def __init__(self, **kwargs): ...

164

def add(self, expr): ...

165

```

166

167

### Set Components

168

169

Set components for defining index sets, domains, and structured data organization with support for finite and infinite sets.

170

171

```python { .api }

172

class Set:

173

"""Set component for model indexing."""

174

def __init__(self, *args, **kwargs): ...

175

def add(self, value): ...

176

def remove(self, value): ...

177

def __contains__(self, item): ...

178

179

class SetOf:

180

"""Set-of component for hierarchical sets."""

181

def __init__(self, *args, **kwargs): ...

182

183

class RangeSet(Set):

184

"""Range set component for continuous ranges."""

185

def __init__(self, *args, **kwargs): ...

186

```

187

188

### Parameter Components

189

190

Parameter components for model data including mutable and immutable parameters with validation and initialization support.

191

192

```python { .api }

193

class Param:

194

"""Parameter component for model data."""

195

def __init__(self, *args, mutable=False, initialize=None, domain=None, within=None, default=None, validate=None, units=None, **kwargs): ...

196

def set_value(self, value): ...

197

def is_mutable(self): ...

198

def clear(self): ...

199

```

200

201

### Expression Components

202

203

Expression components for algebraic expressions and intermediate calculations with automatic differentiation support.

204

205

```python { .api }

206

class Expression:

207

"""Expression component for algebraic expressions."""

208

def __init__(self, *args, **kwargs): ...

209

```

210

211

### Specialized Components

212

213

Specialized components for advanced modeling including special ordered sets, piecewise functions, and external function interfaces.

214

215

```python { .api }

216

class SOSConstraint:

217

"""Special Ordered Set constraint component."""

218

def __init__(self, *args, **kwargs): ...

219

220

class Piecewise:

221

"""Piecewise linear function component."""

222

def __init__(self, *args, **kwargs): ...

223

224

class Connector:

225

"""Component connector for modular modeling."""

226

def __init__(self, *args, **kwargs): ...

227

228

class ExternalFunction:

229

"""External function interface component."""

230

def __init__(self, *args, **kwargs): ...

231

232

def Reference(reference, ctype=None):

233

"""Reference function for component aliasing."""

234

235

class Suffix:

236

"""Suffix component for solver annotations."""

237

def __init__(self, *args, **kwargs): ...

238

```

239

240

### Component Navigation and Management

241

242

Utilities for traversing, managing, and accessing model components with support for filtering and iteration patterns.

243

244

```python { .api }

245

def active_components(block, ctype, sort_by_names=False, sort_by_keys=False):

246

"""

247

Iterate over active components of specified type.

248

249

Args:

250

block: Pyomo model or block

251

ctype: Component type filter

252

sort_by_names: Sort by component names (default False)

253

sort_by_keys: Sort by component keys (default False)

254

255

Returns:

256

Iterator over active components

257

"""

258

259

def components(block, ctype=None, active=None, sort_by_names=False, sort_by_keys=False):

260

"""

261

Iterate over all components of specified type.

262

263

Args:

264

block: Pyomo model or block

265

ctype: Component type filter (optional)

266

active: Filter by active status (optional)

267

sort_by_names: Sort by component names (default False)

268

sort_by_keys: Sort by component keys (default False)

269

270

Returns:

271

Iterator over components

272

"""

273

274

def active_components_data(block, ctype, sort_by_names=False, sort_by_keys=False):

275

"""

276

Iterate over active component data objects.

277

278

Args:

279

block: Pyomo model or block

280

ctype: Component type filter

281

sort_by_names: Sort by names (default False)

282

sort_by_keys: Sort by keys (default False)

283

284

Returns:

285

Iterator over component data objects

286

"""

287

288

def components_data(block, ctype=None, active=None, sort_by_names=False, sort_by_keys=False):

289

"""

290

Iterate over all component data objects.

291

292

Args:

293

block: Pyomo model or block

294

ctype: Component type filter (optional)

295

active: Filter by active status (optional)

296

sort_by_names: Sort by names (default False)

297

sort_by_keys: Sort by keys (default False)

298

299

Returns:

300

Iterator over component data objects

301

"""

302

```

303

304

### Component Rules and Helpers

305

306

Helper functions and rule constructors for simplified component creation and validation.

307

308

```python { .api }

309

def simple_set_rule():

310

"""Helper for creating simple set rules."""

311

312

def simple_constraint_rule():

313

"""Helper for creating simple constraint rules."""

314

315

def simple_constraintlist_rule():

316

"""Helper for creating simple constraint list rules."""

317

318

def simple_objective_rule():

319

"""Helper for creating simple objective rules."""

320

321

def simple_objectivelist_rule():

322

"""Helper for creating simple objective list rules."""

323

324

class BuildAction:

325

"""Component construction action helper."""

326

def __init__(self, *args, **kwargs): ...

327

328

class BuildCheck:

329

"""Component construction validation helper."""

330

def __init__(self, *args, **kwargs): ...

331

```

332

333

### Global Domain Sets and Utility Functions

334

335

Predefined domain sets and utility functions commonly used in optimization models.

336

337

```python { .api }

338

# Global domain sets

339

Reals: Set

340

PositiveReals: Set

341

NonPositiveReals: Set

342

NegativeReals: Set

343

NonNegativeReals: Set

344

Integers: Set

345

PositiveIntegers: Set

346

NonPositiveIntegers: Set

347

NegativeIntegers: Set

348

NonNegativeIntegers: Set

349

Boolean: Set

350

Binary: Set

351

Any: Set

352

AnyWithNone: Set

353

EmptySet: Set

354

UnitInterval: Set

355

PercentFraction: Set

356

357

# Interval constructors

358

def RealInterval(lb, ub): ...

359

def IntegerInterval(lb, ub): ...

360

361

# Set types

362

class RealSet(Set): ...

363

class IntegerSet(Set): ...

364

class BooleanSet(Set): ...

365

366

# Utility functions

367

def prod(*args): ...

368

def quicksum(args): ...

369

def sum_product(*args): ...

370

def dot_product(*args): ...

371

def summation(component, *args, **kwargs): ...

372

def sequence(*args): ...

373

374

# Component utilities

375

def symbol_map_from_instance(instance): ...

376

def display(instance, filename=None, ostream=None): ...

377

```

378

379

## Types

380

381

```python { .api }

382

from enum import Flag, Enum

383

384

class SortComponents(Flag):

385

"""Component sorting options for iteration."""

386

UNSORTED = 0

387

ORDERED_INDICES = 2

388

SORTED_INDICES = 4

389

ALPHABETICAL = 8

390

391

class TraversalStrategy(Enum):

392

"""Model traversal strategy options."""

393

BreadthFirstSearch = 1

394

PrefixDepthFirstSearch = 2 # aliased as DepthFirstSearch

395

PostfixDepthFirstSearch = 3

396

DepthFirstSearch = 2 # alias for PrefixDepthFirstSearch

397

```

398

399

## Usage Examples

400

401

### Basic Model Creation

402

403

```python

404

from pyomo.environ import *

405

406

# Create concrete model

407

model = ConcreteModel()

408

409

# Add variables

410

model.x = Var(domain=NonNegativeReals)

411

model.y = Var(bounds=(0, 10))

412

413

# Add parameters

414

model.a = Param(initialize=2.5)

415

model.b = Param(initialize=1.0, mutable=True)

416

417

# Add constraints

418

model.con1 = Constraint(expr=model.x + model.y <= 10)

419

model.con2 = Constraint(expr=model.a * model.x >= model.b)

420

421

# Add objective

422

model.obj = Objective(expr=model.x + 2*model.y, sense=maximize)

423

```

424

425

### Indexed Components

426

427

```python

428

from pyomo.environ import *

429

430

model = ConcreteModel()

431

432

# Define index sets

433

model.I = Set(initialize=[1, 2, 3])

434

model.J = Set(initialize=['a', 'b', 'c'])

435

436

# Indexed variables

437

model.x = Var(model.I, model.J, domain=Binary)

438

439

# Indexed parameters

440

model.cost = Param(model.I, model.J, initialize=1.0)

441

442

# Indexed constraints

443

def constraint_rule(model, i, j):

444

return model.x[i, j] <= model.cost[i, j]

445

446

model.constraints = Constraint(model.I, model.J, rule=constraint_rule)

447

```