Skip to content

Commit

Permalink
Permutation subsequence inversion neighborhood.
Browse files Browse the repository at this point in the history
  • Loading branch information
baol committed Oct 20, 2009
1 parent d2ba296 commit 9d7a258
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
8 changes: 4 additions & 4 deletions metslib/mets.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mets::swap_elements::operator==(const mets::mana_move& o) const
//________________________________________________________________________

void
mets::swap_subsequence::apply(mets::feasible_solution& s)
mets::invert_subsequence::apply(mets::feasible_solution& s)
{
mets::permutation_problem& sol =
reinterpret_cast<mets::permutation_problem&>(s);
Expand All @@ -62,11 +62,11 @@ mets::swap_subsequence::apply(mets::feasible_solution& s)
}

bool
mets::swap_subsequence::operator==(const mets::mana_move& o) const
mets::invert_subsequence::operator==(const mets::mana_move& o) const
{
try {
const mets::swap_subsequence& other =
dynamic_cast<const mets::swap_subsequence&>(o);
const mets::invert_subsequence& other =
dynamic_cast<const mets::invert_subsequence&>(o);
return (this->p1 == other.p1 && this->p2 == other.p2);
} catch (std::bad_cast& e) {
return false;
Expand Down
30 changes: 25 additions & 5 deletions metslib/mets.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,12 @@ namespace mets {
///
/// @see mets::permutation_problem, mets::mana_move
///
class swap_subsequence : public mets::mana_move
class invert_subsequence : public mets::mana_move
{
public:

/// @brief A move that swaps from and to.
swap_subsequence(int from, int to)
invert_subsequence(int from, int to)
: p1(from), p2(to)
{ }

Expand All @@ -417,7 +417,7 @@ namespace mets {
/// tabu list.
mana_move*
clone() const
{ return new swap_subsequence(p1, p2); }
{ return new invert_subsequence(p1, p2); }

/// @brief An hash function used by the tabu list (the hash value is
/// used to insert the move in an hash set).
Expand All @@ -437,8 +437,8 @@ namespace mets {
int p1; ///< the first element to swap
int p2; ///< the second element to swap

template <typename>
friend class swap_neighborhood;
// template <typename>
// friend class invert_full_neighborhood;
};


Expand Down Expand Up @@ -665,6 +665,26 @@ namespace mets {

};

/// @brief Generates a the full subsequence inversion neighborhood.
class invert_full_neighborhood : public mets::move_manager
{
public:
invert_full_neighborhood(int size) : move_manager()
{
for(int ii(0); ii!=size; ++ii)
for(int jj(0); jj!=size; ++jj)
if(ii != jj)
moves_m.push_back(new invert_subsequence(ii,jj));
}

/// @brief Dtor.
~invert_full_neighborhood() { }

/// @brief Selects a different set of moves at each iteration.
void refresh(mets::feasible_solution& s) { }

};

/// @}

/// @brief Functor class to permit hash_set of moves (used by tabu list)
Expand Down
24 changes: 12 additions & 12 deletions test/permutation_problem_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,53 +45,53 @@ int main()
return 1;
}
}
// test swap_subsequence (1)
// test invert_subsequence (1)
{
p pi(10);
mets::swap_subsequence move(0,9);
mets::invert_subsequence move(0,9);
move.apply(pi);

int check[]={9,8,7,6,5,4,3,2,1,0};
if(pi.pi_m != std::vector<int>(&check[0], &check[10]))
{
cerr << "Failed swap_subsequence (1)." << endl;
cerr << "Failed invert_subsequence (1)." << endl;
return 1;
}
}
// test swap_subsequence (2)
// test invert_subsequence (2)
{
p pi(9);
mets::swap_subsequence move(0,8);
mets::invert_subsequence move(0,8);
move.apply(pi);

int check[]={8,7,6,5,4,3,2,1,0};

if(pi.pi_m != std::vector<int>(&check[0], &check[9]))
{
cerr << "Failed swap_subsequence (2)." << endl;
cerr << "Failed invert_subsequence (2)." << endl;
return 1;
}
}

// test swap_subsequence (3)
// test invert_subsequence (3)
{
p pi(9);
mets::swap_subsequence move(2,5);
mets::invert_subsequence move(2,5);
move.apply(pi);

int check[]={0,1,5,4,3,2,6,7,8};

if(pi.pi_m != std::vector<int>(&check[0], &check[9]))
{
cerr << "Failed swap_subsequence (3)." << endl;
cerr << "Failed invert_subsequence (3)." << endl;
return 1;
}
}

// test swap_subsequence (4)
// test invert_subsequence (4)
{
p pi(9);
mets::swap_subsequence move(5,2);
mets::invert_subsequence move(5,2);
move.apply(pi);

int check[]={7,6,5,3,4,2,1,0,8};
Expand All @@ -102,7 +102,7 @@ int main()

if(pi.pi_m != std::vector<int>(&check[0], &check[9]))
{
cerr << "Failed swap_subsequence (4)." << endl;
cerr << "Failed invert_subsequence (4)." << endl;
return 1;
}
}
Expand Down

0 comments on commit 9d7a258

Please sign in to comment.