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

Objective to be minimized.

states

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.

binary([shape])

Create a binary symbol as a decision variable.

constant(array_like)

Create a constant symbol.

disjoint_bit_sets(primary_set_size, ...)

Create a disjoint-sets symbol as a decision variable.

disjoint_lists(primary_set_size, ...)

Create a disjoint-lists symbol as a decision variable.

integer([shape, lower_bound, upper_bound])

Create an integer symbol as a decision variable.

list(n)

Create a list symbol as a decision variable.

set(n[, min_size, max_size])

Create a set symbol as a decision variable.

Model Methods#

add_constraint(value)

Add a constraint to the model.

decision_state_size()

An estimated size, in bytes, of the model's decision states.

from_file(file, *[, check_header])

Construct a model from the given file.

into_file(file, *[, max_num_states, ...])

Serialize the model into an existing file.

is_locked()

Lock status of the model.

iter_constraints()

Iterate over all constraints in the model.

iter_decisions()

Iterate over all decision variables in the model.

iter_symbols()

Iterate over all symbols in the model.

lock()

Lock the model.

minimize(value)

Set the objective value to minimize.

num_constraints()

Number of constraints in the model.

num_decisions()

Number of independent decision nodes in the model.

num_nodes()

Number of nodes in the directed acyclic graph for the model.

num_symbols()

Number of symbols tracked by the model.

quadratic_model(x, quadratic[, linear])

Create a quadratic model from an array and a quadratic model.

remove_unused_symbols()

Remove unused symbols from the model.

state_size()

An estimate of the size, in bytes, of all states in the model.

to_file(**kwargs)

Serialize the model to a new file-like object.

to_networkx()

Convert the model to a NetworkX graph.

unlock()

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) 
<dwave.optimization.symbols.LessEqual at ...>
>>> # 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()

Clear any saved states.

from_file(file, *[, replace, check_header])

Construct states from the given file.

from_future(future, result_hook)

Populate the states from the result of a future computation.

into_file(file)

Serialize the states into an existing file.

resize(n)

Resize the number of states.

resolve()

Block until states are retrieved from any pending future computations.

size()

Number of model states.

to_file()

Serialize the states to a new file-like object.