For#
- class gamspy.For(index: Parameter, start: int | float | Parameter | ImplicitParameter | Expression | Card | Operation, end: int | float | Parameter | ImplicitParameter | Expression | Card | Operation, step: int | float | Parameter | ImplicitParameter | Expression | Card | Operation = 1, direction: Literal['to', 'downto'] = 'to')[source]#
Bases:
objectA context manager to execute a group of statements iteratively over a numerical range.
The For class maps to the GAMS for statement. It allows you to iterate over a range of numerical values, incrementing or decrementing a scalar parameter at each step. It is useful for iterative algorithmic calculations that require a numerical counter, rather than iterating over elements of a set.
- Parameters:
- indexParameter
A scalar Parameter used as the numerical loop counter.
- startint | float | Parameter | Expression | Card | Operation
The starting value of the loop counter.
- endint | float | Parameter | Expression | Card | Operation
The final value of the loop counter.
- stepint | float | Parameter | Expression | Card | Operation, optional
The increment or decrement step size. Defaults to 1.
- directionLitera[‘to’, ‘downto’]
The direction of the step. ‘to’ steps upwards, ‘downto’ steps downwards. Defaults to ‘to’.
- Attributes:
Examples
1. Simple iteration over a numerical range:
>>> import gamspy as gp >>> m = gp.Container() >>> i = gp.Parameter(m) >>> cnt = gp.Parameter(m, records=0) >>> with gp.For(i, 1, 10): ... cnt[...] += i
2. Iterating backwards When a negative step is provided, the loop iterate downwards.
>>> x = gp.Parameter(m, records=10) >>> with gp.For(i, 10, 1, 2, direction="downto"): ... x[...] = x[...] - 2
3. Using Parameters as loop bounds: You can use other parameters or expressions to define the boundaries of the loop.
>>> start_val = gp.Parameter(m, records=5) >>> end_val = gp.Parameter(m, records=15) >>> with gp.For(i, start_val, end_val): ... cnt[...] += 1
- property Break: None#
Breaks the execution of the current loop prematurely.
This property maps to the GAMS break statement. Note that you can only break out of the innermost loop currently executing. Attempting to break an outer loop from within an inner loop will raise a ValidationError.
- Raises:
- ValidationError
If attempting to break an outer loop without breaking the inner loop first.
Examples
>>> import gamspy as gp >>> m = gp.Container() >>> i = gp.Parameter(m) >>> cnt = gp.Parameter(m, records=0) >>> with gp.For(i, 1, 10) as my_for: ... cnt[...] += 1 ... with gp.If(i == 5): ... my_for.Break # Exits the loop when `i` reaches 5
- property Continue: None#
Skips the remaining statements in the current iteration and proceeds to the next one.
This property maps to the GAMS continue statement. It gives additional control over the execution of loop structures by allowing you to bypass the rest of the loop block for the current counter value.
Examples
>>> import gamspy as gp >>> m = gp.Container() >>> i = gp.Parameter(m) >>> cnt = gp.Parameter(m, records=0) >>> with gp.For(i, 1, 10) as my_for: ... with gp.If(i == 5): ... my_for.Continue # Skips incrementing `cnt` when `i` is 5 ... cnt[...] += 1