Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
operations_research::PathOperator Class Referenceabstract

#include <constraint_solveri.h>

Inheritance diagram for operations_research::PathOperator:
operations_research::IntVarLocalSearchOperator operations_research::LocalSearchOperator operations_research::BaseObject operations_research::BaseInactiveNodeToPathOperator operations_research::Cross operations_research::Exchange operations_research::ExchangeSubtrip operations_research::GroupPairAndRelocateOperator operations_research::IndexPairSwapActiveOperator operations_research::LightPairRelocateOperator operations_research::LinKernighan operations_research::MakeChainInactiveOperator operations_research::MakeInactiveOperator operations_research::MakePairActiveOperator operations_research::MakePairInactiveOperator operations_research::MakeRelocateNeighborsOperator operations_research::PairExchangeOperator operations_research::PairExchangeRelocateOperator operations_research::PairNodeSwapActiveOperator< swap_first > operations_research::PairRelocateOperator operations_research::PathLns operations_research::Relocate operations_research::RelocateAndMakeInactiveOperator operations_research::RelocateExpensiveChain operations_research::RelocateSubtrip operations_research::SwapActiveToShortestPathOperator operations_research::TSPLns operations_research::TSPOpt operations_research::TwoOpt

Classes

struct  IterationParameters
 Set of parameters used to configure how the neighnorhood is traversed. More...
 

Public Member Functions

 PathOperator (const std::vector< IntVar * > &next_vars, const std::vector< IntVar * > &path_vars, IterationParameters iteration_parameters)
 Builds an instance of PathOperator from next and path variables.
 
 PathOperator (const std::vector< IntVar * > &next_vars, const std::vector< IntVar * > &path_vars, int number_of_base_nodes, bool skip_locally_optimal_paths, bool accept_path_end_base, std::function< int(int64_t)> start_empty_path_class, std::function< const std::vector< int > &(int, int)> get_neighbors)
 
 ~PathOperator () override
 
virtual bool MakeNeighbor ()=0
 
void Reset () override
 
bool SkipUnchanged (int index) const override
 
int64_t Next (int64_t node) const
 Returns the node after node in the current delta.
 
int64_t Prev (int64_t node) const
 Returns the node before node in the current delta.
 
int64_t Path (int64_t node) const
 
int number_of_nexts () const
 Number of next variables.
 
- Public Member Functions inherited from operations_research::IntVarLocalSearchOperator
 IntVarLocalSearchOperator (const std::vector< IntVar * > &vars, bool keep_inverse_values=false)
 
 ~IntVarLocalSearchOperator () override
 
bool HoldsDelta () const override
 
void Start (const Assignment *assignment) override
 
virtual bool IsIncremental () const
 
int Size () const
 
int64_t Value (int64_t index) const
 
IntVarVar (int64_t index) const
 Returns the variable of given index.
 
int64_t OldValue (int64_t index) const
 
int64_t PrevValue (int64_t index) const
 
void SetValue (int64_t index, int64_t value)
 
bool Activated (int64_t index) const
 
void Activate (int64_t index)
 
void Deactivate (int64_t index)
 
bool ApplyChanges (Assignment *delta, Assignment *deltadelta) const
 
void RevertChanges (bool change_was_incremental)
 
void AddVars (const std::vector< IntVar * > &vars)
 
bool MakeNextNeighbor (Assignment *delta, Assignment *deltadelta) override
 --— Base operator class for operators manipulating IntVars --—
 
- Public Member Functions inherited from operations_research::LocalSearchOperator
 LocalSearchOperator ()
 
 ~LocalSearchOperator () override
 
virtual const LocalSearchOperatorSelf () const
 
virtual bool HasFragments () const
 
- Public Member Functions inherited from operations_research::BaseObject
 BaseObject ()
 
 BaseObject (const BaseObject &)=delete
 This type is neither copyable nor movable.
 
BaseObjectoperator= (const BaseObject &)=delete
 
virtual ~BaseObject ()
 
virtual std::string DebugString () const
 

Protected Member Functions

bool MakeOneNeighbor () override
 This method should not be overridden. Override MakeNeighbor() instead.
 
virtual void OnNodeInitialization ()
 
int64_t BaseNode (int i) const
 Returns the ith base node of the operator.
 
int BaseAlternative (int i) const
 Returns the alternative for the ith base node.
 
int64_t BaseAlternativeNode (int i) const
 Returns the alternative node for the ith base node.
 
int BaseSiblingAlternative (int i) const
 Returns the alternative for the sibling of the ith base node.
 
int64_t BaseSiblingAlternativeNode (int i) const
 Returns the alternative node for the sibling of the ith base node.
 
int64_t StartNode (int i) const
 Returns the start node of the ith base node.
 
int64_t EndNode (int i) const
 Returns the end node of the ith base node.
 
const std::vector< int64_t > & path_starts () const
 Returns the vector of path start nodes.
 
int PathClass (int i) const
 Returns the class of the path of the ith base node.
 
int PathClassFromStartNode (int64_t start_node) const
 
virtual bool RestartAtPathStartOnSynchronize ()
 
virtual bool OnSamePathAsPreviousBase (int64_t base_index)
 
virtual int64_t GetBaseNodeRestartPosition (int base_index)
 
virtual void SetNextBaseToIncrement (int64_t base_index)
 
virtual bool ConsiderAlternatives (int64_t base_index) const
 
int64_t OldNext (int64_t node) const
 
int64_t PrevNext (int64_t node) const
 
int64_t OldPrev (int64_t node) const
 
int64_t OldPath (int64_t node) const
 
int CurrentNodePathStart (int64_t node) const
 
int CurrentNodePathEnd (int64_t node) const
 
bool MoveChain (int64_t before_chain, int64_t chain_end, int64_t destination)
 
bool ReverseChain (int64_t before_chain, int64_t after_chain, int64_t *chain_last)
 
bool MakeActive (int64_t node, int64_t destination)
 Insert the inactive node after destination.
 
bool MakeChainInactive (int64_t before_chain, int64_t chain_end)
 
bool SwapActiveAndInactive (int64_t active, int64_t inactive)
 Replaces active by inactive in the current path, making active inactive.
 
void SetNext (int64_t from, int64_t to, int64_t path)
 Sets 'to' to be the node after 'from' on the given path.
 
bool IsPathEnd (int64_t node) const
 
bool IsPathStart (int64_t node) const
 Returns true if node is the first node on the path.
 
bool IsInactive (int64_t node) const
 Returns true if node is inactive.
 
virtual bool InitPosition () const
 
void ResetPosition ()
 
int AddAlternativeSet (const std::vector< int64_t > &alternative_set)
 
template<typename PairType >
void AddPairAlternativeSets (const std::vector< PairType > &pair_alternative_sets)
 
int64_t GetActiveInAlternativeSet (int alternative_index) const
 Returns the active node in the given alternative set.
 
int64_t GetActiveAlternativeNode (int node) const
 Returns the active node in the alternative set of the given node.
 
int GetSiblingAlternativeIndex (int node) const
 Returns the index of the alternative set of the sibling of node.
 
int64_t GetActiveAlternativeSibling (int node) const
 
bool CheckChainValidity (int64_t before_chain, int64_t chain_end, int64_t exclude) const
 
bool HasNeighbors () const
 
int GetNeighborForBaseNode (int64_t base_index) const
 
- Protected Member Functions inherited from operations_research::IntVarLocalSearchOperator
int64_t InverseValue (int64_t index) const
 
int64_t OldInverseValue (int64_t index) const
 
void AddToAssignment (IntVar *var, int64_t value, bool active, std::vector< int > *assignment_indices, int64_t index, Assignment *assignment) const
 

Protected Attributes

const int number_of_nexts_
 
const bool ignore_path_vars_
 

Detailed Description

Base class of the local search operators dedicated to path modifications (a path is a set of nodes linked together by arcs). This family of neighborhoods supposes they are handling next variables representing the arcs (var[i] represents the node immediately after i on a path). Several services are provided:

  • arc manipulators (SetNext(), ReverseChain(), MoveChain())
  • path inspectors (Next(), Prev(), IsPathEnd())
  • path iterators: operators need a given number of nodes to define a neighbor; this class provides the iteration on a given number of (base) nodes which can be used to define a neighbor (through the BaseNode method) Subclasses only need to override MakeNeighbor to create neighbors using the services above (no direct manipulation of assignments).

Definition at line 1318 of file constraint_solveri.h.

Constructor & Destructor Documentation

◆ PathOperator() [1/2]

operations_research::PathOperator::PathOperator ( const std::vector< IntVar * > & next_vars,
const std::vector< IntVar * > & path_vars,
IterationParameters iteration_parameters )

Builds an instance of PathOperator from next and path variables.

--— Path-based Operators --—

Definition at line 348 of file local_search.cc.

◆ PathOperator() [2/2]

operations_research::PathOperator::PathOperator ( const std::vector< IntVar * > & next_vars,
const std::vector< IntVar * > & path_vars,
int number_of_base_nodes,
bool skip_locally_optimal_paths,
bool accept_path_end_base,
std::function< int(int64_t)> start_empty_path_class,
std::function< const std::vector< int > &(int, int)> get_neighbors )
inline

Definition at line 1348 of file constraint_solveri.h.

◆ ~PathOperator()

operations_research::PathOperator::~PathOperator ( )
inlineoverride

Definition at line 1357 of file constraint_solveri.h.

Member Function Documentation

◆ AddAlternativeSet()

int operations_research::PathOperator::AddAlternativeSet ( const std::vector< int64_t > & alternative_set)
inlineprotected

Handling node alternatives. Adds a set of node alternatives to the neighborhood. No node can be in two altrnatives.

Definition at line 1541 of file constraint_solveri.h.

◆ AddPairAlternativeSets()

template<typename PairType >
void operations_research::PathOperator::AddPairAlternativeSets ( const std::vector< PairType > & pair_alternative_sets)
inlineprotected

Adds all sets of node alternatives of a vector of alternative pairs. No node can be in two alternatives.

Definition at line 1555 of file constraint_solveri.h.

◆ BaseAlternative()

int operations_research::PathOperator::BaseAlternative ( int i) const
inlineprotected

Returns the alternative for the ith base node.

Definition at line 1397 of file constraint_solveri.h.

◆ BaseAlternativeNode()

int64_t operations_research::PathOperator::BaseAlternativeNode ( int i) const
inlineprotected

Returns the alternative node for the ith base node.

Definition at line 1399 of file constraint_solveri.h.

◆ BaseNode()

int64_t operations_research::PathOperator::BaseNode ( int i) const
inlineprotected

Returns the ith base node of the operator.

Definition at line 1395 of file constraint_solveri.h.

◆ BaseSiblingAlternative()

int operations_research::PathOperator::BaseSiblingAlternative ( int i) const
inlineprotected

Returns the alternative for the sibling of the ith base node.

Definition at line 1407 of file constraint_solveri.h.

◆ BaseSiblingAlternativeNode()

int64_t operations_research::PathOperator::BaseSiblingAlternativeNode ( int i) const
inlineprotected

Returns the alternative node for the sibling of the ith base node.

Definition at line 1411 of file constraint_solveri.h.

◆ CheckChainValidity()

bool operations_research::PathOperator::CheckChainValidity ( int64_t before_chain,
int64_t chain_end,
int64_t exclude ) const
protected

Returns true if the chain is a valid path without cycles from before_chain to chain_end and does not contain exclude.

Rejects chain if chain_end is not after before_chain on the path or if the chain contains exclude. Given before_chain is the node before the chain, if before_chain and chain_end are the same the chain is rejected too. Also rejects cycles (cycle detection is detected through chain length overflow).

Definition at line 866 of file local_search.cc.

◆ ConsiderAlternatives()

virtual bool operations_research::PathOperator::ConsiderAlternatives ( int64_t base_index) const
inlineprotectedvirtual

Indicates if alternatives should be considered when iterating over base nodes.

Reimplemented in operations_research::PairRelocateOperator.

Definition at line 1464 of file constraint_solveri.h.

◆ CurrentNodePathEnd()

int operations_research::PathOperator::CurrentNodePathEnd ( int64_t node) const
inlineprotected

Definition at line 1489 of file constraint_solveri.h.

◆ CurrentNodePathStart()

int operations_research::PathOperator::CurrentNodePathStart ( int64_t node) const
inlineprotected

Definition at line 1485 of file constraint_solveri.h.

◆ EndNode()

int64_t operations_research::PathOperator::EndNode ( int i) const
inlineprotected

Returns the end node of the ith base node.

Definition at line 1423 of file constraint_solveri.h.

◆ GetActiveAlternativeNode()

int64_t operations_research::PathOperator::GetActiveAlternativeNode ( int node) const
inlineprotected

Returns the active node in the alternative set of the given node.

Definition at line 1571 of file constraint_solveri.h.

◆ GetActiveAlternativeSibling()

int64_t operations_research::PathOperator::GetActiveAlternativeSibling ( int node) const
inlineprotected

Returns the active node in the alternative set of the sibling of the given node.

Definition at line 1582 of file constraint_solveri.h.

◆ GetActiveInAlternativeSet()

int64_t operations_research::PathOperator::GetActiveInAlternativeSet ( int alternative_index) const
inlineprotected

Returns the active node in the given alternative set.

Definition at line 1565 of file constraint_solveri.h.

◆ GetBaseNodeRestartPosition()

virtual int64_t operations_research::PathOperator::GetBaseNodeRestartPosition ( int base_index)
inlineprotectedvirtual

Returns the index of the node to which the base node of index base_index must be set to when it reaches the end of a path. By default, it is set to the start of the current path. When this method is called, one can only assume that base nodes with indices < base_index have their final position.

Reimplemented in operations_research::MakeChainInactiveOperator, operations_research::MakePairActiveOperator, operations_research::PairExchangeRelocateOperator, operations_research::PairNodeSwapActiveOperator< swap_first >, operations_research::PairRelocateOperator, and operations_research::TwoOpt.

Definition at line 1454 of file constraint_solveri.h.

◆ GetNeighborForBaseNode()

int operations_research::PathOperator::GetNeighborForBaseNode ( int64_t base_index) const
inlineprotected

Definition at line 1598 of file constraint_solveri.h.

◆ GetSiblingAlternativeIndex()

int operations_research::PathOperator::GetSiblingAlternativeIndex ( int node) const
inlineprotected

Returns the index of the alternative set of the sibling of node.

Definition at line 1575 of file constraint_solveri.h.

◆ HasNeighbors()

bool operations_research::PathOperator::HasNeighbors ( ) const
inlineprotected

Definition at line 1594 of file constraint_solveri.h.

◆ InitPosition()

virtual bool operations_research::PathOperator::InitPosition ( ) const
inlineprotectedvirtual

Returns true if the operator needs to restart its initial position at each call to Start()

Definition at line 1532 of file constraint_solveri.h.

◆ IsInactive()

bool operations_research::PathOperator::IsInactive ( int64_t node) const
inlineprotected

Returns true if node is inactive.

Definition at line 1526 of file constraint_solveri.h.

◆ IsPathEnd()

bool operations_research::PathOperator::IsPathEnd ( int64_t node) const
inlineprotected

Returns true if node is the last node on the path; defined by the fact that node is outside the range of the variable array.

Definition at line 1520 of file constraint_solveri.h.

◆ IsPathStart()

bool operations_research::PathOperator::IsPathStart ( int64_t node) const
inlineprotected

Returns true if node is the first node on the path.

Definition at line 1523 of file constraint_solveri.h.

◆ MakeActive()

bool operations_research::PathOperator::MakeActive ( int64_t node,
int64_t destination )
protected

Insert the inactive node after destination.

Definition at line 465 of file local_search.cc.

◆ MakeChainInactive()

bool operations_research::PathOperator::MakeChainInactive ( int64_t before_chain,
int64_t chain_end )
protected

Makes the nodes on the chain starting after before_chain and ending at chain_end inactive.

Definition at line 475 of file local_search.cc.

◆ MakeNeighbor()

◆ MakeOneNeighbor()

bool operations_research::PathOperator::MakeOneNeighbor ( )
overrideprotectedvirtual

This method should not be overridden. Override MakeNeighbor() instead.

Need to revert changes here since MakeNeighbor might have returned false and have done changes in the previous iteration.

Reimplemented from operations_research::IntVarLocalSearchOperator.

Reimplemented in operations_research::RelocateExpensiveChain, and operations_research::TSPLns.

Definition at line 395 of file local_search.cc.

◆ MoveChain()

bool operations_research::PathOperator::MoveChain ( int64_t before_chain,
int64_t chain_end,
int64_t destination )
protected

Moves the chain starting after the node before_chain and ending at the node chain_end after the node destination

Definition at line 419 of file local_search.cc.

◆ Next()

int64_t operations_research::PathOperator::Next ( int64_t node) const
inline

Returns the node after node in the current delta.

Definition at line 1365 of file constraint_solveri.h.

◆ number_of_nexts()

int operations_research::PathOperator::number_of_nexts ( ) const
inline

Number of next variables.

Definition at line 1384 of file constraint_solveri.h.

◆ OldNext()

int64_t operations_research::PathOperator::OldNext ( int64_t node) const
inlineprotected

Definition at line 1466 of file constraint_solveri.h.

◆ OldPath()

int64_t operations_research::PathOperator::OldPath ( int64_t node) const
inlineprotected

Definition at line 1481 of file constraint_solveri.h.

◆ OldPrev()

int64_t operations_research::PathOperator::OldPrev ( int64_t node) const
inlineprotected

Definition at line 1476 of file constraint_solveri.h.

◆ OnNodeInitialization()

virtual void operations_research::PathOperator::OnNodeInitialization ( )
inlineprotectedvirtual

Called by OnStart() after initializing node information. Should be overridden instead of OnStart() to avoid calling PathOperator::OnStart explicitly.

Definition at line 1392 of file constraint_solveri.h.

◆ OnSamePathAsPreviousBase()

virtual bool operations_research::PathOperator::OnSamePathAsPreviousBase ( int64_t base_index)
inlineprotectedvirtual

Returns true if a base node has to be on the same path as the "previous" base node (base node of index base_index - 1). Useful to limit neighborhood exploration to nodes on the same path.

Todo
(user): ideally this should be OnSamePath(int64_t node1, int64_t node2); it's currently way more complicated to implement.

Reimplemented in operations_research::MakeChainInactiveOperator, operations_research::MakePairActiveOperator, operations_research::PairExchangeRelocateOperator, operations_research::PairNodeSwapActiveOperator< swap_first >, operations_research::PairRelocateOperator, operations_research::Relocate, and operations_research::TwoOpt.

Definition at line 1448 of file constraint_solveri.h.

◆ Path()

int64_t operations_research::PathOperator::Path ( int64_t node) const
inline

Returns the index of the path to which node belongs in the current delta. Only returns a valid value if path variables are taken into account.

Definition at line 1379 of file constraint_solveri.h.

◆ path_starts()

const std::vector< int64_t > & operations_research::PathOperator::path_starts ( ) const
inlineprotected

Returns the vector of path start nodes.

Definition at line 1425 of file constraint_solveri.h.

◆ PathClass()

int operations_research::PathOperator::PathClass ( int i) const
inlineprotected

Returns the class of the path of the ith base node.

Definition at line 1427 of file constraint_solveri.h.

◆ PathClassFromStartNode()

int operations_research::PathOperator::PathClassFromStartNode ( int64_t start_node) const
inlineprotected

Definition at line 1428 of file constraint_solveri.h.

◆ Prev()

int64_t operations_research::PathOperator::Prev ( int64_t node) const
inline

Returns the node before node in the current delta.

Definition at line 1371 of file constraint_solveri.h.

◆ PrevNext()

int64_t operations_research::PathOperator::PrevNext ( int64_t node) const
inlineprotected

Definition at line 1471 of file constraint_solveri.h.

◆ Reset()

void operations_research::PathOperator::Reset ( )
overridevirtual

Reimplemented from operations_research::LocalSearchOperator.

Reimplemented in operations_research::TwoOpt.

Definition at line 386 of file local_search.cc.

◆ ResetPosition()

void operations_research::PathOperator::ResetPosition ( )
inlineprotected

Reset the position of the operator to its position when Start() was last called; this can be used to let an operator iterate more than once over the paths.

Definition at line 1536 of file constraint_solveri.h.

◆ RestartAtPathStartOnSynchronize()

virtual bool operations_research::PathOperator::RestartAtPathStartOnSynchronize ( )
inlineprotectedvirtual

When the operator is being synchronized with a new solution (when Start() is called), returns true to restart the exploration of the neighborhood from the start of the last paths explored; returns false to restart the exploration at the last nodes visited. This is used to avoid restarting on base nodes which have changed paths, leading to potentially skipping neighbors.

Todo
(user): remove this when automatic detection of such cases in done.

Reimplemented in operations_research::MakePairActiveOperator, and operations_research::PairNodeSwapActiveOperator< swap_first >.

Definition at line 1441 of file constraint_solveri.h.

◆ ReverseChain()

bool operations_research::PathOperator::ReverseChain ( int64_t before_chain,
int64_t after_chain,
int64_t * chain_last )
protected

Reverses the chain starting after before_chain and ending before after_chain

Definition at line 442 of file local_search.cc.

◆ SetNext()

void operations_research::PathOperator::SetNext ( int64_t from,
int64_t to,
int64_t path )
inlineprotected

Sets 'to' to be the node after 'from' on the given path.

Definition at line 1509 of file constraint_solveri.h.

◆ SetNextBaseToIncrement()

virtual void operations_research::PathOperator::SetNextBaseToIncrement ( int64_t base_index)
inlineprotectedvirtual

Set the next base to increment on next iteration. All base > base_index will be reset to their start value.

Definition at line 1459 of file constraint_solveri.h.

◆ SkipUnchanged()

bool operations_research::PathOperator::SkipUnchanged ( int index) const
overridevirtual
Todo
(user): Make the following methods protected.

Reimplemented from operations_research::IntVarLocalSearchOperator.

Definition at line 407 of file local_search.cc.

◆ StartNode()

int64_t operations_research::PathOperator::StartNode ( int i) const
inlineprotected

Returns the start node of the ith base node.

Definition at line 1421 of file constraint_solveri.h.

◆ SwapActiveAndInactive()

bool operations_research::PathOperator::SwapActiveAndInactive ( int64_t active,
int64_t inactive )
protected

Replaces active by inactive in the current path, making active inactive.

Definition at line 492 of file local_search.cc.

Member Data Documentation

◆ ignore_path_vars_

const bool operations_research::PathOperator::ignore_path_vars_
protected

Definition at line 1606 of file constraint_solveri.h.

◆ number_of_nexts_

const int operations_research::PathOperator::number_of_nexts_
protected

Definition at line 1605 of file constraint_solveri.h.


The documentation for this class was generated from the following files: