relentless.model.IndependentVariable#

class relentless.model.IndependentVariable(value, low=None, high=None, name=None)#

Independent quantity.

An independent variable is a quantity that is meant to be adjusted, e.g., by optimization. Optional box constraints (lower and upper bounds) can be specified for the variable. When set, these bounds will be respected and an internal state will track whether the requested quantity was within or outside these bounds. This is useful for performing constrained optimization and for ensuring physical quantities have meaningful values (e.g., lengths should be positive).

Parameters:
  • value (float) – Value of the variable.

  • low (float or None) – Lower bound for the variable (None means no lower bound).

  • high (float or None) – Upper bound for the variable (None means no upper bound).

  • name (str) – Optional name for the variable. The name must be unique. If one is not specified, a mangled name __x[id] will be automatically created, where id is the unique index identifying the variable.

Examples

Create an independent variable:

>>> x = relentless.variable.IndependentVariable(3.0)
>>> print(x)
3.0

The value of an independent variable can be changed:

>>> x.value = -1.0
>>> print(x)
-1.0

Perform in-place arithmetic operations:

>>> x += 4.0
>>> print(x)
3.0
>>> x -= 1.0
>>> print(x)
2.0
>>> x *= 0.5
>>> print(x)
1.0
>>> x /= 2.0
>>> print(x)
0.5

A variable with a lower bound:

>>> v = relentless.variable.IndependentVariable(value=1.0, low=0.0)
>>> v.value
1.0
>>> v.atlow()
False

Bounds are respected and noted when setting values:

>>> v.value = -1.0
>>> v.value
0.0
>>> v.atlow()
True

Methods

at_high()

Check if variable is constrained at the upper bound.

at_low()

Check if variable is constrained at the lower bound.

Attributes

count

high

Upper bound on value.

low

Lower bound on value.

names

value

Value of the variable.

at_high()#

Check if variable is constrained at the upper bound.

Returns:

True if the variable is constrained at the lower bound.

Return type:

bool

at_low()#

Check if variable is constrained at the lower bound.

Returns:

True if the variable is constrained at the lower bound.

Return type:

bool

property high#

Upper bound on value.

Type:

float

property low#

Lower bound on value.

Type:

float

property value#

Value of the variable.

Type:

float