variable
Variable
¶
Bases: Leaf
Base class for decision variables in optimization problems.
Variable represents decision variables (free parameters) in an optimization problem. These are values that the optimizer can adjust to minimize the objective function while satisfying constraints. Variables can have bounds (min/max) and initial guesses to guide the optimization process.
Unlike Parameters (which are fixed values that can be changed between solves), Variables are optimized by the solver. In trajectory optimization, Variables typically represent discretized state or control trajectories.
Note
Variable is typically not instantiated directly. Instead, use the specialized subclasses State (for state variables with boundary conditions) or Control (for control inputs). These provide additional functionality specific to trajectory optimization.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Name identifier for the variable |
_shape |
tuple[int, ...]
|
Shape of the variable as a tuple (typically 1D) |
_slice |
slice | None
|
Internal slice information for variable indexing |
_min |
ndarray | None
|
Minimum bounds for each element of the variable |
_max |
ndarray | None
|
Maximum bounds for each element of the variable |
_guess |
ndarray | None
|
Initial guess for the variable trajectory (n_points, n_vars) |
Example
Typically, use State or Control instead of Variable directly:¶
pos = openscvx.State("pos", shape=(3,)) u = openscvx.Control("u", shape=(2,))
Source code in openscvx/symbolic/expr/variable.py
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 | |
guess
property
writable
¶
Get the initial guess for the variable trajectory.
The guess provides a starting point for the optimizer. A good initial guess can significantly improve convergence speed and help avoid local minima.
Returns:
| Type | Description |
|---|---|
|
2D array of shape (n_points, n_vars) representing the variable trajectory |
|
|
over time, or None if no guess is provided. |
Example
x = Variable("x", shape=(2,))
Linear interpolation from [0,0] to [10,10] over 50 points¶
x.guess = np.linspace([0, 0], [10, 10], 50) print(x.guess.shape) # (50, 2)
max
property
writable
¶
Get the maximum bounds (upper bounds) for the variable.
Returns:
| Type | Description |
|---|---|
|
Array of maximum values for each element of the variable, or None if unbounded. |
Example
vel = Variable("vel", shape=(3,)) vel.max = [10, 10, 5] print(vel.max) # [10., 10., 5.]
min
property
writable
¶
Get the minimum bounds (lower bounds) for the variable.
Returns:
| Type | Description |
|---|---|
|
Array of minimum values for each element of the variable, or None if unbounded. |
Example
pos = Variable("pos", shape=(3,)) pos.min = [-10, -10, 0] print(pos.min) # [-10., -10., 0.]
_hash_into(hasher: hashlib._Hash) -> None
¶
Hash Variable using its slice (canonical position, name-invariant).
Instead of hashing the variable name, we hash the _slice attribute which represents the variable's canonical position in the unified state/control vector. This ensures that two problems with the same structure but different variable names produce the same hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hasher
|
_Hash
|
A hashlib hash object to update |
required |
Source code in openscvx/symbolic/expr/variable.py
append(other=None, *, min=-np.inf, max=np.inf, guess=0.0)
¶
Append a new dimension to this variable or merge with another variable.
This method extends the variable's dimension by either: 1. Appending another Variable object (concatenating their dimensions) 2. Adding a single new scalar dimension with specified bounds and guess
The bounds and guesses of both variables are concatenated appropriately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Another Variable object to append. If None, adds a single scalar dimension with the specified min/max/guess values. |
None
|
|
min
|
Minimum bound for the new dimension (only used if other is None). Defaults to -np.inf (unbounded below). |
-inf
|
|
max
|
Maximum bound for the new dimension (only used if other is None). Defaults to np.inf (unbounded above). |
inf
|
|
guess
|
Initial guess value for the new dimension (only used if other is None). Defaults to 0.0. |
0.0
|
Example
Create a 2D variable and extend it to 3D:
pos_xy = Variable("pos", shape=(2,))
pos_xy.min = [-10, -10]
pos_xy.max = [10, 10]
pos_xy.append(min=0, max=100) # Add z dimension
print(pos_xy.shape) # (3,)
print(pos_xy.min) # [-10., -10., 0.]
print(pos_xy.max) # [10., 10., 100.]
Merge two variables:
pos = Variable("pos", shape=(3,))
vel = Variable("vel", shape=(3,))
pos.append(vel) # Now pos has shape (6,)
Source code in openscvx/symbolic/expr/variable.py
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 | |