LocalSolver logo
is now
Hexaly logo

We're excited to share that we are moving forward. We're leaving behind the LocalSolver brand and transitioning to our new identity: Hexaly. This represents a leap forward in our mission to enable every organization to make better decisions faster when faced with operational and strategic challenges.

This page is for an old version of Hexaly Optimizer. We recommend that you update your version and read the documentation for the latest stable release.

LSArray Class

class localsolver.LSArray

Value type for array expressions. Such value is obtained with LSExpression.GetArrayValue() or LSSolution.GetArrayValue(LSExpression). An array can contain numbers (int, double) or other LSArray (for multi-dimensional arrays).

Arrays are not decisions and cannot be modified.

Since:7.5
See:LSModel
See:LSExpression
See:LSOperator.Array

Summary

Methods
Count Returns the number of values in the array.
IsBool Returns true if the value at the given position is a boolean.
IsInt Returns true if the value at the given position is an integer.
IsDouble Returns true if the value at the given position is a double.
IsArray Returns true if the value at the given position is an array.
GetIntValue Returns the integer value at the given position.
GetDoubleValue Returns the double value at the given position.
GetArrayValue Returns the array value at the given position.
ToString Returns a string representation of the values in the array in the format { val0, val1, ..., valN }.
CopyTo Copy all the values of this array to the given array.

Instance methods

int Count()

Returns the number of values in the array. Elements in arrays are indexed from 0 to count()-1.

Returns:Number of values in the array.
Return type:int
bool IsBool(int position)

Returns true if the value at the given position is a boolean. You can retrieve the value with GetIntValue().

Arguments:position (int) – Position of the value to query.
Returns:True if the value at the given position is a boolean.
Return type:bool
bool IsInt(int position)

Returns true if the value at the given position is an integer. You can retrieve the value with GetIntValue().

Arguments:position (int) – Position of the value to query.
Returns:True if the value at the given position is an integer.
Return type:bool
bool IsDouble(int position)

Returns true if the value at the given position is a double. You can retrieve the value with GetDoubleValue().

Arguments:position (int) – Position of the value to query.
Returns:True if the value at the given position is a boolean.
Return type:bool
bool IsArray(int position)

Returns true if the value at the given position is an array. You can retrieve the value with GetArrayValue().

Arguments:position (int) – Position of the value to query.
Returns:True if the value at the given position is an array.
Return type:bool
long GetIntValue(int position)

Returns the integer value at the given position. If the value is neither an integer, nor a boolean, an exception is thrown.

Arguments:position (int) – Position of the value to query.
Returns:Integer value.
Return type:long
double GetDoubleValue(int position)

Returns the double value at the given position. If the value is not a double, an exception is thrown.

Arguments:position (int) – Position of the value to query.
Returns:Double value.
Return type:double
LSArray GetArrayValue(int position)

Returns the array value at the given position. If the value is not an array, an exception is thrown.

Arguments:position (int) – Position of the value to query.
Returns:Array value.
Return type:LSArray
string ToString()

Returns a string representation of the values in the array in the format { val0, val1, ..., valN }.

Returns:A String representation of the array
Return type:string
void CopyTo(long[] values)

Copy all the values of this array to the given array. Only the integer values are copied to their corresponding position in the array. Cells of the array given in parameters that correspond to positions of non-integer values in this array remain unchanged.

The length of the array given in parameters can be different from the number of elements in this array. In that case, only the elements that fit in the array are copied.

This method is recommended if you need to access all the values of this array, instead of the roughly equivalent, but less performant, following code:

for(int i = 0; i < Math.Min(values.Length, array.Count()); i++)
{
    if(!array.IsInt(i)) continue;
    values[i] = array.GetIntValue(i);
}
Arguments:values (long[]) – Array that will receive the integer values of this array.
void CopyTo(double[] values)

Copy all the values of this array to the given array. Only the double values are copied to their corresponding position in the array. Cells of the array given in parameters that correspond to positions of non-double values in this array remain unchanged.

The length of the array given in parameters can be different from the number of elements in this array. In that case, only the elements that fit in the array are copied.

This method is recommended if you need to access all the values of this array, instead of the roughly equivalent, but less performant, following code:

for(int i = 0; i < Math.Min(values.Length, array.Count()); i++)
{
    if(!array.IsDouble(i)) continue;
    values[i] = array.GetDoubleValue(i);
}
Arguments:values (double[]) – Array that will receive the double values of this array.