Nonlinear Models#
This page describes the dwave-optimization package’s nonlinear model: classes, attributes, and methods.
For examples, see Ocean’s Getting Started examples.
Nonlinear models are especially suited for use with decision variables that represent a common logic, such as subsets of choices or permutations of ordering. For example, in a traveling salesperson problem permutations of the variables representing cities can signify the order of the route being optimized and in a knapsack problem the variables representing items can be divided into subsets of packed and not packed.
Model Class#
- class Model#
Nonlinear model.
The nonlinear model represents a general optimization problem with an objective function and/or constraints over variables of various types.
The
Model
class can contain this model and its methods provide convenient utilities for working with representations of a problem.Examples
This example creates a model for a
flow-shop-scheduling
problem with two jobs on three machines.>>> from dwave.optimization.generators import flow_shop_scheduling ... >>> processing_times = [[10, 5, 7], [20, 10, 15]] >>> model = flow_shop_scheduling(processing_times=processing_times)
Model Attributes#
Objective to be minimized. |
|
States of the model. |
Model Primitives#
Instantiation of the model’s decision (and constant) symbols. For the full list of supported symbols, see the Symbols page.
|
Create a binary symbol as a decision variable. |
|
Create a constant symbol. |
|
Create a disjoint-sets symbol as a decision variable. |
|
Create a disjoint-lists symbol as a decision variable. |
|
Create an integer symbol as a decision variable. |
|
Create a list symbol as a decision variable. |
|
Create a set symbol as a decision variable. |
Model Methods#
|
Add a constraint to the model. |
An estimated size, in bytes, of the model's decision states. |
|
|
Construct a model from the given file. |
|
Serialize the model into an existing file. |
Lock status of the model. |
|
Iterate over all constraints in the model. |
|
Iterate over all decision variables in the model. |
|
Iterate over all symbols in the model. |
|
|
Lock the model. |
|
Set the objective value to minimize. |
Number of constraints in the model. |
|
Number of independent decision nodes in the model. |
|
Number of nodes in the directed acyclic graph for the model. |
|
Number of symbols tracked by the model. |
|
|
Create a quadratic model from an array and a quadratic model. |
An estimate of the size, in bytes, of all states in the model. |
|
|
Serialize the model to a new file-like object. |
Convert the model to a NetworkX graph. |
|
|
Release a lock, decrementing the lock count. |
States Class#
- class States#
States of a symbol in a model.
States represent assignments of values to a symbol’s elements. For example, an
integer()
symbol of size \(1 \times 5\) might have state[3, 8, 0, 12, 8]
, representing one assignment of values to the symbol.Examples
This example creates a
knapsack
model and manipulates its states to test that it behaves as expected.First, create a model.
>>> from dwave.optimization import Model ... >>> model = Model() >>> # Add constants >>> weights = model.constant([10, 20, 5, 15]) >>> values = model.constant([-5, -7, -2, -9]) >>> capacity = model.constant(30) >>> # Add the decision variable >>> items = model.set(4) >>> # add the capacity constraint >>> model.add_constraint(weights[items].sum() <= capacity) >>> # Set the objective >>> model.minimize(values[items].sum())
Lock the model to prevent changes to directed acyclic graph. At any time, you can verify the locked state, which is demonstrated here.
>>> with model.lock(): ... model.is_locked() True
Set a couple of states on the decision variable and verify that the model generates the expected values for the objective.
>>> model.states.resize(2) >>> items.set_state(0, [0, 1]) >>> items.set_state(1, [0, 2, 3]) >>> with model.lock(): ... print(model.objective.state(0) > model.objective.state(1)) True
You can clear the states you set.
>>> model.states.clear() >>> model.states.size() 0
States Methods#
|
Clear any saved states. |
|
Construct states from the given file. |
|
Populate the states from the result of a future computation. |
|
Serialize the states into an existing file. |
|
Resize the number of states. |
|
Block until states are retrieved from any pending future computations. |
|
Number of model states. |
|
Serialize the states to a new file-like object. |