Class HexalyOptimizer
- java.lang.Object
-
- com.hexaly.optimizer.HexalyOptimizer
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class HexalyOptimizer extends java.lang.Object implements java.lang.AutoCloseable
Hexaly Optimizer environment. Main class of Hexaly Optimizer library. Here are described the basic steps for using Hexaly Optimizer:- Build your model (
HxModel
) by creating some expressions (HxExpression
). - If desired, parameterize and add phases to the optimizer (
HxParam
,HxPhase
). - Run the optimizer (
HexalyOptimizer
). - Retrieve the best solution found by the optimizer (
HxSolution
). - Consult the statistics of the resolution (
HxStatistics
).
Note that this API is not thread safe. If multiple threads access and modify a same Hexaly Optimizer environment, it must be synchronized. The only methods that can be used without synchronization are
getState()
andstop()
. Please consultHxVersion
for copyright and version info.
-
-
Constructor Summary
Constructors Constructor Description HexalyOptimizer()
Constructs a complete Hexaly Optimizer environment and takes a token license.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addCallback(HxCallbackType type, HxCallback callback)
Adds a new callback for a specific event type.void
close()
Deletes the Hexaly objects and release the license token.HxInconsistency
computeInconsistency()
Computes an inconsistency core for this model.HxPhase
createPhase()
Adds a new phase to this Hexaly Optimizer environment.void
delete()
Deletes the Hexaly Optimizer objects and release the license token.boolean
equals(java.lang.Object obj)
HxModel
getModel()
Gets the model associated to this Hexaly Optimizer environment.int
getNbPhases()
Gets the number of phases.HxParam
getParam()
Gets the parameters of this Hexaly Optimizer environment.HxPhase
getPhase(int phaseIndex)
Gets the phase with the given index.HxSolution
getSolution()
Gets the best solution found by the optimizer.HxState
getState()
Gets the state of this Hexaly Optimizer environment.HxStatistics
getStatistics()
Gets the statistics of this Hexaly Optimizer environment.int
hashCode()
void
loadEnvironment(java.lang.String filename)
Import a complete environment or a model from a file.boolean
removeCallback(HxCallbackType type, HxCallback callback)
Removes the callback for the given event type.void
saveEnvironment(java.lang.String filename)
Export a complete environment or a model to a file.void
solve()
Solves the model.void
stop()
Aborts the resolution previously launched usingsolve()
.java.lang.String
toString()
Returns a String representation of this Hexaly Optimizer environment.
-
-
-
Constructor Detail
-
HexalyOptimizer
public HexalyOptimizer()
Constructs a complete Hexaly Optimizer environment and takes a token license. If no token is available or if the token server is not accessible, an exception is thrown. The token license is released when the destructor of this Hexaly Optimizer environment is called.
-
-
Method Detail
-
delete
public void delete()
Deletes the Hexaly Optimizer objects and release the license token. Calling this method releases the native memory used by Hexaly Optimizer and makes the Hexaly Optimizer license available for another Hexaly Optimizer model.
-
close
public void close()
Deletes the Hexaly objects and release the license token. Calling this method releases the native memory used by Hexaly Optimizer and makes the Hexaly license available for another Hexaly model.- Specified by:
close
in interfacejava.lang.AutoCloseable
-
getState
public HxState getState()
Gets the state of this Hexaly Optimizer environment. This method can be called in any state. In particular, this method can be called in stateHxState.Running
.- Returns:
- State of Hexaly Optimizer.
-
getModel
public HxModel getModel()
Gets the model associated to this Hexaly Optimizer environment. Once the model is created and closed, the optimizer can be launched withsolve()
.- Returns:
- Model.
-
getParam
public HxParam getParam()
Gets the parameters of this Hexaly Optimizer environment.- Returns:
- Parameters.
-
solve
public void solve()
Solves the model. This method returns only when the time limit, the iteration limit, the objective bounds are reached or when the methodstop()
is called. The model must be closed to launch the resolution.
-
computeInconsistency
public HxInconsistency computeInconsistency()
Computes an inconsistency core for this model. Only allowed in stateHxState.Stopped
.- Returns:
- The inconsistency core.
-
stop
public void stop()
Aborts the resolution previously launched usingsolve()
. If no resolution was launched, this method does nothing. Called from another thread or callback, this method enables users to stop the resolution properly. Solution and statistics remain valid. This method can be called in any state, notably in stateHxState.Running
. Note that the effect of this method is not immediate. In particular when called within a callback, the resolution will not be stopped before the end of the callback.
-
createPhase
public HxPhase createPhase()
Adds a new phase to this Hexaly Optimizer environment. Only allowed in statesHxState.Modeling
orHxState.Stopped
.- Returns:
- Created phase.
-
getPhase
public HxPhase getPhase(int phaseIndex)
Gets the phase with the given index.- Parameters:
phaseIndex
- Index of the phase.- Returns:
- Phase.
-
getNbPhases
public int getNbPhases()
Gets the number of phases.- Returns:
- Number of phases.
-
getSolution
public HxSolution getSolution()
Gets the best solution found by the optimizer. If the optimizer has not been started at least once, all the decision variables of the solution are set to 0, or to their closest bound if 0 is not part of their domain (such a solution may be infeasible).. Only allowed in statesHxState.Paused
orHxState.Stopped
.- Returns:
- Best solution.
-
getStatistics
public HxStatistics getStatistics()
Gets the statistics of this Hexaly Optimizer environment. Statistics are reset to zero before each resolution. Only allowed in statesHxState.Paused
orHxState.Stopped
. Note that for performance reasons, this function always returns the same object.- Returns:
- Statistics.
- See Also:
HxStatistics
-
toString
public java.lang.String toString()
Returns a String representation of this Hexaly Optimizer environment. This representation provides useful info related to the model, the parameters, and the phases (if any). Useful for debugging or logging purposes.- Overrides:
toString
in classjava.lang.Object
- Returns:
- String representation of this Hexaly Optimizer.
-
saveEnvironment
public void saveEnvironment(java.lang.String filename)
Export a complete environment or a model to a file. Currently, this function supports 2 file formats:- HXB: with this format, the complete environment (model, parameters, solution, etc.) is exported. This format is useful to debug or replay a model in the same conditions. Since the produced file is binary, it offers good performance and space efficiency. This format was also known as LSB which corresponds to its former file extension (.lsb). It is still supported by this method but it is deprecated.
- HXM: with this format, only the model is exported in the Hexaly modeling language. This format is useful to have a quick view of the optimization model or to perform some modifications. However, the file may be heavy for big models and the exact reproductibility is not guaranteed since the parameters from one execution to another may differ.
- Parameters:
filename
- Name of the file.- Since:
- 3.0
- See Also:
loadEnvironment(java.lang.String)
-
loadEnvironment
public void loadEnvironment(java.lang.String filename)
Import a complete environment or a model from a file. Only allowed in stateHxState.Modeling
. The current model must be empty.The only format supported is LSB.
The chosen file format is determined by the file suffix. An exception is thrown if the provided file suffix is not supported. The suffix may optionally be followed by .gz. In that case, this function uncompress the stream before reading.
- Parameters:
filename
- Name of the file.- Since:
- 3.0
- See Also:
saveEnvironment(java.lang.String)
-
addCallback
public void addCallback(HxCallbackType type, HxCallback callback)
Adds a new callback for a specific event type. The callback will be called time the given event occurs.The same callback can be used for different events. Only allowed in states
HxState.Stopped
orHxState.Modeling
.Note: When a callback is called, the optimizer is paused. In that state, you can call all the methods marked as "allowed in state
HxState.Paused
". Calling any other method will throw an error. For example, you can stop the resolution from a callback, retrieve the current solution or retrieve the statistics of the optimizer but you can't remove a constraint.- Parameters:
type
- Event to watch.callback
- Callback. Cannot be null.- Since:
- 4.0
- See Also:
HxCallback
-
removeCallback
public boolean removeCallback(HxCallbackType type, HxCallback callback)
Removes the callback for the given event type.- Parameters:
type
- Event.callback
- User callback to delete.- Returns:
- false if the callback was not added for the given event, true otherwise.
- Since:
- 4.0
- See Also:
addCallback(com.hexaly.optimizer.HxCallbackType, com.hexaly.optimizer.HxCallback)
,HxCallback
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
-