Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add beam #47

Merged
merged 14 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed bug in assignment and copy operators. New try for testing beam,…
… which is also semi fixed
  • Loading branch information
okkevaneck committed Feb 6, 2023
commit ecf25ff5e2a3aadecab6c471abd8694dc0bf6187
Binary file modified archives/prospr_core.tar.gz
Binary file not shown.
Binary file modified archives/prospr_core.zip
Binary file not shown.
8 changes: 4 additions & 4 deletions prospr/core/src/beam_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ Protein* beam_search(Protein* protein, int beam_width) {
std::cout << "cur_protein before: " << cur_protein << "\n";
std::cout << "cur_expansion before: " << cur_expansion << "\n";

cur_expansion = *protein;
// std::memcpy(&cur_expansion, protein, sizeof(Protein));
cur_expansion = cur_protein;
std::cout << "cur_expansion mid: " << cur_expansion << "\n";
cur_expansion.place_amino(m);

std::cout << "cur_protein after: " << cur_protein << "\n";
std::cout << "cur_expansion after: " << cur_expansion << "\n";
std::cout << "cur_protein after: " << cur_protein << "\n";

cur_prioprot = {cur_expansion, comp_score(&cur_expansion)};
cur_proteins.push(cur_prioprot);
Expand All @@ -116,7 +116,7 @@ Protein* beam_search(Protein* protein, int beam_width) {
cur_proteins.empty();
}

std::memcpy(protein, &beam[0].protein, sizeof(Protein));
*protein = beam[0].protein;
std::cout << "Best score: " << protein->get_score() << "\n";

/* First protein in priority queue will have highest score. */
Expand Down
58 changes: 37 additions & 21 deletions prospr/core/src/protein.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ Protein::Protein(const Protein &other) {
this->max_weights = other.max_weights;

/* Create new AminoAcid objects for all amino acids. */
AminoAcid* new_aa;
this->amino_acids.clear();
for (AminoAcid* other_aa : other.amino_acids) {
AminoAcid* new_aa = new AminoAcid(*other_aa);
new_aa = new AminoAcid(*other_aa);
this->amino_acids.push_back(new_aa);
}

/* Copy placement of other protein and map to new AminoAcids. */
this->space.clear();
for (auto &item : other.space) {
this->space[item.first] = this->amino_acids[item.second->get_index()];
}
Expand Down Expand Up @@ -134,26 +137,39 @@ Protein::Protein(std::string sequence, int dim, std::string model,
}
}

///* Overload assignment operator for copy-assignments. */
//Protein &Protein::operator=(const Protein& protein) {
// protein.sequence = this->sequence;
// protein.dim = this->dim;
// protein.bond_values = this->bond_values;
// protein.weighted_amino_acids = this->weighted_amino_acids;
// protein.max_weights = this->max_weights;
//
// /* Copy state of protein. */
// protein.space = this->space;
// protein.amino_acids = this->amino_acids;
// protein.cur_len = this->cur_len;
// protein.last_move = this->last_move;
// protein.last_pos = this->last_pos;
// protein.score = this->score;
// protein.aminos_placed = this->aminos_placed;
// protein.solutions_checked = this->solutions_checked;
//
// return *this;
//}
/* Overload assignment operator for copy-assignments. */
Protein& Protein::operator=(const Protein& other) {
/* Set model essentials. */
this->sequence = other.sequence;
this->dim = other.dim;
this->bond_values = other.bond_values;
this->weighted_amino_acids = other.weighted_amino_acids;
this->max_weights = other.max_weights;

/* Create new AminoAcid objects for all amino acids. */
AminoAcid* new_aa;
this->amino_acids.clear();
for (AminoAcid* other_aa : other.amino_acids) {
new_aa = new AminoAcid(*other_aa);
this->amino_acids.push_back(new_aa);
}

/* Copy placement of other protein and map to new AminoAcids. */
this->space.clear();
for (auto &item : other.space) {
this->space[item.first] = this->amino_acids[item.second->get_index()];
}

/* Copy state of protein's values. */
this->cur_len = other.cur_len;
this->last_move = other.last_move;
this->last_pos = other.last_pos;
this->score = other.score;
this->aminos_placed = other.aminos_placed;
this->solutions_checked = other.solutions_checked;

return *this;
}

/* Returns the Protein's sequence. */
std::string Protein::get_sequence() {
Expand Down
4 changes: 2 additions & 2 deletions prospr/core/src/protein.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class Protein {
std::map<std::string, int> bond_values={},
bool bond_symmetry=true);

// /* Overload assignment operator for copy-assignments. */
// Protein &Protein::operator=(const Protein &protein);
/* Overload assignment operator for copy-assignments. */
Protein& operator=(const Protein &protein);

/* Returns the Protein's sequence. */
std::string get_sequence();
Expand Down
Binary file added prospr/core/tests/test_algorithms
Binary file not shown.
3 changes: 2 additions & 1 deletion prospr/core/tests/test_algorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ void test_depth_first_bnb() {
/* Test functionality of depth_first_bnb. */
void test_beam_search() {
/* Check if 2D solutions are found correctly with a beam width of -1. */
Protein* protein = new Protein("PHPHPHPPH", 2, "HP");
// Protein* protein = new Protein("PHPHPHPPH", 2, "HP");
Protein* protein = new Protein("HPPH", 2, "HP");
protein = beam_search(protein, -1);
assert (protein->get_score() == -3);
std::cout << "\t2D Protein solution scores matches with beam_width=-1.\n";
Expand Down