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.

Builtin functions and variables

Core functions

println([arg0[, arg1[, ...]]])

Writes the arguments to the standard output followed by a new line. If an argument is not convertible to a string, an error is thrown. You can use this function with a variadic number of arguments or with the variadic call syntax. If no argument is given, this function simply prints a new empty line.

print([arg0[, arg1[, ...]]])

Same as println() without the new line.

map([arg0[, arg1[, ...]]])

Creates a new map initialized with the given arguments. Keys will start at 0. You can use this function with a variadic number of arguments or with the variadic call syntax. If no argument is given, this function returns an empty map. .

Mathematical operators

Most of these operators can be used in two different modes depending on the type of the given arguments:

  1. With integers or doubles: the modeler performs the asked operation and returns an integer or a double. This mode applies only if all arguments are integers or doubles. As soon as one solver expression is given, the second mode is used. However, you can freely mix integers and doubles: the final result will be casted to double if necessary.
  2. With solver expressions: the expression represented by the operator is created and added to the LocalSolver model. This mode is used as soon as one argument is a solver expression.

For example:

// The sum operator creates and adds a new solver expression
// because at least one argument is neither an integer, nor a double.
x[i in 1..N] <- bool();
s <- sum[i in 1..N](x[i]);

// The sum operator returns the sum of odd numbers between
// 1 and N as an integer because all the operands are integers.
odd <- sum[i in 1..N : i % 2 == 1](i);
bool()

Creates a new boolean decision and adds it to the LocalSolver model. This function always returns a solver expression.

Returns:A solver expression.
float(lb, ub)

Creates a new float decision with the given bounds [lb, ub] and adds it to the LocalSolver model. This function always returns a solver expression. Bounds must be numbers (integers or doubles).

Parameters:
  • lb – Lower bound.
  • ub – Upper bound (included).
Returns:

A solver expression.

int(lb, ub)

Creates a new integer decision with the given bounds [lb, ub] and adds it to the LocalSolver model. This function always returns a solver expression. Bounds must be integers.

Parameters:
  • lb (int) – Lower bound.
  • ub (int) – Upper bound (included).
Returns:

A solver expression.

sum([arg0[, arg1[, ...]]])

Computes or creates a sum expression with the given arguments. Each argument can be a solver expression, a boolean, an integer or a double. It is also possible to use this function with a variadic number of arguments or with the variadic call syntax. If no argument is given, this function returns the integer 0.

Returns:A solver expression if at least one solver expression is given as argument, a number otherwise.
sub(a, b)

Computes the difference between two numbers or creates a sub expression. Each argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if at least one solver expression is given as argument, a number otherwise.
prod([arg0[, arg1[, ...]]])

Computes or creates a product expression with the given arguments. Each argument can be a solver expression, a boolean, an integer or a double. It is also possible to use this function with a variadic number of arguments or with the variadic call syntax. If no argument is given, this function returns the integer 1.

Returns:A solver expression if at least one solver expression is given as argument, a number otherwise.
div(a, b)

Computes the division between two numbers or creates a div expression. Each argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if at least one solver expression is given as argument, a double otherwise.
mod(a, b)

Computes the modulo between two integers or creates a mod expression. Each argument can be a solver expression, a boolean or an integer.

Returns:A solver expression if at least one solver expression is given as argument, an integer otherwise.
min(arg0[, arg1[, arg2[, ...]]])

Computes or creates a min expression with the given arguments. Each argument can be a solver expression, a boolean, an integer or a double. It is also possible to use this function with a variadic number of arguments or with the variadic call syntax, but note that at least one argument is required.

Returns:A solver expression if at least one solver expression is given as argument, a number otherwise.
max(arg0[, arg1[, arg2[, ...]]])

Computes or creates a max expression with the given arguments. Each argument can be a solver expression, a boolean, an integer or a double. It is also possible to use this function with a variadic number of arguments or with the variadic call syntax, but note that at least one argument is required.

Returns:A solver expression if at least one solver expression is given as argument, a number otherwise.
eq(a, b)

Compares the arguments or creates an equality solver expression. Each argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if at least one solver expression is given as argument, a boolean otherwise.
neq(a, b)

Compares the arguments or creates a disequality solver expression. Each argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if at least one solver expression is given as argument, a boolean otherwise.
geq(a, b)

Compares the arguments or creates an inequality ‘greater than or equal to’. solver expression. Each argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if at least one solver expression is given as argument, a boolean otherwise.
leq(a, b)

Compares the arguments or creates an inequality ‘lower than or equal to’ solver expression. Each argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if at least one solver expression is given as argument, a boolean otherwise.
gt(a, b)

Compares the arguments or creates an inequality ‘greater than’ solver expression. Each argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if at least one solver expression is given as argument, a boolean otherwise.
lt(a, b)

Compares the arguments or creates an inequality ‘lower than’ solver expression. Each argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if at least one solver expression is given as argument, a boolean otherwise.
iif(condition, trueValue, falseValue)

Performs the if or creates a ternary conditional expression. The condition can be a solver expression or a boolean. The trueValue and falseValue can be solver expressions, booleans, integers or doubles. If the condition is a boolean, trueValue or falseValue is returned accordingly without any further treatment. Otherwise a new solver expression is returned.

Returns:A solver expression if the condition is a solver expression, a number otherwise.
not(a)

Negates a boolean or creates a boolean not expression. The argument can be a solver expression or a boolean.

Returns:A solver expression if the argument is a solver expression, a boolean otherwise.
and([arg0[, arg1[, ...]]])

Computes or creates a boolean and expression with the given arguments. Each argument can be a solver expression or a boolean. It is also possible to use this function with a variadic number of arguments or with the variadic call syntax. If no argument is given, this function returns the integer 1.

Returns:A solver expression if at least one solver expression is given as argument, a boolean number otherwise.
or([arg0[, arg1[, ...]]])

Computes or creates a boolean or expression with the given arguments. Each argument can be a solver expression or a boolean. It is also possible to use this function with a variadic number of arguments or with the variadic call syntax. If no argument is given, this function returns the integer 0.

Returns:A solver expression if at least one solver expression is given as argument, a boolean number otherwise.
xor([arg0[, arg1[, ...]]])

Computes or creates a boolean xor expression with the given arguments. Each argument can be a solver expression or a boolean. It is also possible to use this function with a variadic number of arguments or with the variadic call syntax. If no argument is given, this function returns the integer 0.

Returns:A solver expression if at least one solver expression is given as argument, a boolean number otherwise.
abs(a)

Computes the absolute value of a number or creates a abs expression. The argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if the argument is a solver expression, a number otherwise.
dist(a, b)

Computes the distance between two numbers or creates a distance expression. Each argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if at least one solver expression is given as argument, a number otherwise.
pow(a, b)

Computes a power b or creates a pow expression. Each argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if at least one solver expression is given as argument, a double otherwise.
sqrt(a)

Computes or creates a square root expression with the given argument. The argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if the argument is a solver expression, a double otherwise.
log(a)

Computes or creates a natural logartihm expression with the given argument. The argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if the argument is a solver expression, a double otherwise.
exp(a)

Computes or creates an exponential expression with the given argument. The argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if the argument is a solver expression, a double otherwise.
cos(a)

Computes or creates a cosine expression with the given argument. The argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if the argument is a solver expression, a double otherwise.
sin(a)

Computes or creates a sine expression with the given argument. The argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if the argument is a solver expression, a double otherwise.
tan(a)

Computes or creates a tangent expression with the given argument. The argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if the argument is a solver expression, a double otherwise.
ceil(a)

Computes the ceiling of the argument or creates a ceil expression. The argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if the argument is a solver expression, an integer otherwise.
floor(a)

Computes the floor of the argument or creates a floor expression. The argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if the argument is a solver expression, an integer otherwise.
round(a)

Returns the closest integer to the argument or creates a round expression. The argument can be a solver expression, a boolean, an integer or a double.

Returns:A solver expression if the argument is a solver expression, an integer otherwise.
array([arg0[, arg1[, ...]]])
at(arr, index0[, index1[, index2[, ...]]])
scalar(arr1, arr2)
piecewise(x, y, z)
list(range)
count(list)
indexOf(list, elem)
partition(list0[, list1[, list2[, ...]]])
disjoint(list0[, list1[, list2[, ...]]])
call(func[, arg0[, arg1[, ...]]])

Global variables

These variables can be accessed from any function of your code. They control various aspect of LocalSolver. As any other global variables, they can be directly set on the command line.

lsTimeLimit
lsIterationLimit
lsNbThreads

Number of threads used to parallelize the search. The default value is 2. By increasing the number of threads, you increase the robustness of the solver (that is, the chance to find better solutions) and the speed of the search. However, we recommend you to avoid running a number of threads which exceeds the number of cores available on your machine.

Furthermore, if you use native functions with LSP, we strongly recommend you to disable the multi-threading of the search and sets this parameter to 1 due to thread contention issue. Please read Performance issues for more details.

lsSeed

The seed of the pseudorandom number generator used by LocalSolver internally to perform its local search. This number must be positive. Default value is 0.

lsTimeBetweenDisplays

Interval in seconds between two displays during the search. This number must be positive. Default value is 1.

lsVerbosity

Verbosity level of the solver. The amount of information displayed during the search depends on this parameter. There are 3 different levels:

  • 0: All traces are disabled
  • 1: Normal verbosity. Displays only the most crucial indicators: objectives, elapsed time and number of iterations.
  • 2: Wordy level. Displays advanced statistics like the percentage of accepted, infeasible and rejected moves.
lsAnnealingLevel

Annealing level of the local search. This level must be an integer between 0 and 9. If set to 0, the search heuristic is equivalent to a standard descent: moves deteriorating the current solution are rejected. By increasing this parameter, you increase the number of uphill moves (that is, moves deteriorating the objective value of the current solution): this increases chances to reach better solutions (diversification), but slows the convergence of the search.

Deprecated functions

These functions are deprecated. They are replaced by modules of the standard library. They are only provided for compatibility reasons. New features will only be added through modules. We strongly discourage you to use them in your new projects.

Warning

These functions are deprecated and will be removed in a next version of LocalSolver. Use the provided modules instead.

split(string, delimiter)

Split a string according the provided delimiter. Use the more general string.split() function of the string module.

toString(value)

Returns the string representation of a value.

toInt(string)

Returns the int representation of the provided string. Use string.toInt() function of the string module instead.

toDouble(string)

Returns the floating-point representation of the provided string. Use string.toDouble() function of the string module instead.

trim(string)

Returns a copy of the string with leading and trailing whitespaces removed. Use string.trim() function of the string module instead.

length(string)

Returns the length of the string. Use string.length() function of the string module instead.

substring(string, start)
substring(string, start, length)

Returns a new string that is a substring of the provided string. Use string.substring() function of the string module instead.

startsWith(string, prefix)

Returns true if the string starts with the given prefix. Use string.startsWith() function of the string module instead.

endsWith(string, prefix)

Returns true if the string ends with the given suffix. Use string.endsWith() function of the string module instead.

lowerCase(string)

Returns a new string with all the characters converted to lower case. Use string.toLowerCase() function of the string module instead.

upperCase(string)

Returns a new string with all the characters converted to upper case. Use string.toUpperCase() function of the string module instead.

replace(string, search, replace)

Return a copy of the string with all occurrences of substring search replaced by replace. Use string.replace() function of the string module instead.

openRead(filename)

Opens file filename in reading mode. For a better support of Unicode characters, use the function io.openRead() instead.

openWrite(filename)

Opens file filename in writing mode. For a better support of Unicode characters, use the function io.openWrite() instead.

openAppend(filename)

Opens file filename in appending mode. For a better support of Unicode characters, use the function io.openAppend() instead.

close(file)

Closes the current file. Use the function inputstream.close() or outputstream.close() of the io module instead.

eof(file)

Returns true if the end of file is reached. Use the function inputstream.eof() of the io module instead.

readInt(file)

Reads and returns the next integer available in the file. Use the function inputstream.readInt() of the io module instead.

readDouble(file)

Reads and returns the next floating-point number available in the file. Use the function inputstream.readDouble() of the io module instead.

readString(file)

Reads and returns the next sequence of non whitespace characters. Use the function inputstream.readString() of the io module instead.

readln(file)

Reads ands returns the next line in the stream. Use the function inputstream.readln() of the io module instead.

add(map, value)

Adds the given value in the map. Use the function map.add() of the map module instead.

keys(map)

Returns the keys of the map in a new map. Use the function map.keys() of the map module instead.

values(map)

Returns the values of the map in a new map. Use the function map.values() of the map module instead.

error(message)

Quit LocalSolver and print the given message as an error. If you want to throw errors, use exceptions instead.