ElfPSP_AntColony
Protein Structure Prediction using Ant Colony Optimization
acosolution.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include <vector>
6 #include <random>
7 #include "vec3.h"
8 
12 class ACOSolution {
13  std::vector<vec3<int>> dVector;
14  std::vector<char> dDirections;
15  bool dError;
17 public:
18 
20  ACOSolution();
21 
23  ACOSolution(std::vector<char> &directions);
24 
26  static const char UP = 0;
27  static const char DOWN = 1;
28  static const char LEFT = 2;
29  static const char RIGHT = 3;
30  static const char FRONT = 4;
33  static const char DIR_TO_CHAR(char d);
34 
36  std::vector<vec3<int>> &vector() { return dVector; }
37  const std::vector<vec3<int>> &vector() const { return dVector; }
41  std::vector<char> &directions(){ return dDirections; }
42  const std::vector<char> &directions() const { return dDirections; }
46  vec3<int> bead(unsigned int i){ return dVector[i]; }
47 
49  char direction(unsigned int i){ return dDirections[i]; }
50 
62  static vec3<int> DIRECTION_VECTOR(vec3<int> prevDirection, char dir);
63 
68 
72  int count_contacts(const HPChain &chain) const;
73 
77  void perturb_one(std::mt19937 &dRandGen);
78 
80  bool has_error() { return dError; }
81 
83  void set_error() { dError = true; }
84 
86  void clear_error() { dError = false; };
87 };
88 
93 std::ostream& operator<<(std::ostream& stream, const ACOSolution &sol);
94 
95 /********/
96 
98 : dVector({{0,0,0}, {1,0,0}}), dError(false)
99 {}
100 
101 
102 inline ACOSolution::ACOSolution(std::vector<char> &directions)
103 : dVector({{0,0,0}, {1,0,0}}), dDirections(directions), dError(false)
104 {
105  // We build the protein coordinates, based on the given directions vector
106  for(char dir: directions){
107  vec3<int> prev = this->previous_direction();
108  vec3<int> delta = DIRECTION_VECTOR(prev, dir);
109  dVector.push_back(dVector.back() + delta);
110  }
111 }
112 
113 inline
114 const char ACOSolution::DIR_TO_CHAR(char d){
115  static const char map[] = { 'U', 'D', 'R', 'L', 'F' };
116  return map[(int) d];
117 }
118 
119 inline
121  if(dir == FRONT){
122  return prevDirection;
123  }
124 
125  // Will be either 1 or -1
126  int sign = prevDirection.x + prevDirection.y + prevDirection.z;
127 
128  struct { char x, z; }
129  isZero = {
130  .x = (prevDirection.x == 0),
131  .z = (prevDirection.z == 0),
132  };
133 
134  vec3<int> retval({0,0,0});
135  if(dir == RIGHT){
136  retval.x = sign * isZero.x;
137  retval.y = sign * !isZero.x;
138  } else if(dir == LEFT){
139  retval.x = -sign * isZero.x;
140  retval.y = -sign * !isZero.x;
141  } else if(dir == UP){
142  retval.z = 1 * isZero.z;
143  retval.y = 1 * !isZero.z;
144  } else /* if(dir == DOWN) */ {
145  retval.z = -1 * isZero.z;
146  retval.y = -1 * !isZero.z;
147  }
148 
149  return retval;
150 }
151 
152 inline
154  return dVector.back() - dVector[dVector.size() - 2];
155 }
156 
157 inline
158 int ACOSolution::count_contacts(const HPChain &chain) const {
159  int contacts = 0;
160 
161  for(unsigned i = 0; i < dVector.size(); i++){
162  if(chain[i] == 'P') continue;
163 
164  for(unsigned j = i+1; j < dVector.size(); j++){
165  if(chain[j] == 'P') continue;
166  int norm1 = (dVector[i] - dVector[j]).norm1();
167  if(norm1 == 1) contacts++;
168  }
169  }
170 
171  return contacts;
172 }
173 
174 inline
175 void ACOSolution::perturb_one(std::mt19937 &gen){
176  std::uniform_real_distribution<> randDist(0.0, 1.0);
177 
178  // Decide where to perturb
179  int randPos = randDist(gen) * dDirections.size();
180 
181  // Perturb that position
182  dDirections[randPos] = randDist(gen) * 5;
183 
184  // Rebuild the vector
185  dVector = {{0,0,0}, {1,0,0}};
186  for(char dir: dDirections){
187  vec3<int> prev = previous_direction();
188  vec3<int> delta = ACOSolution::DIRECTION_VECTOR(prev, dir);
189  dVector.push_back(dVector.back() + delta);
190  }
191 }
192 
193 inline
194 std::ostream& operator<<(std::ostream& stream, const ACOSolution &sol){
195  for(const vec3<int> &point: sol.vector())
196  stream << point << " ";
197  stream << "\n";
198  for(char c: sol.directions())
199  stream << ACOSolution::DIR_TO_CHAR(c);
200 
201  return stream;
202 }
std::vector< char > & directions()
Vector of directions taken by the ant.
Definition: acosolution.h:41
static const char LEFT
Relative direction.
Definition: acosolution.h:28
Encapsulates a string representing an HP chain.
Definition: hpchain.h:8
void perturb_one(std::mt19937 &dRandGen)
The solution has a vector of N directions, this procedure chooses a random direction and randomizes i...
Definition: acosolution.h:175
vec3< int > bead(unsigned int i)
Gets an specific bead.
Definition: acosolution.h:46
const std::vector< vec3< int > > & vector() const
Vector of bead coordinates.
Definition: acosolution.h:37
void set_error()
Sets error to signalize existence of an error.
Definition: acosolution.h:83
std::vector< vec3< int > > dVector
Vector of bead coordinates.
Definition: acosolution.h:13
Represents a triple (x, y, z) of any type.
Definition: vec3.h:12
static vec3< int > DIRECTION_VECTOR(vec3< int > prevDirection, char dir)
Given a previous direction and a relative direction, returns the corresponding delta vector...
Definition: acosolution.h:120
std::vector< char > dDirections
Vector of directions taken by the ant.
Definition: acosolution.h:14
static const char DIR_TO_CHAR(char d)
Maps directions to characters U, D, L, R, F.
Definition: acosolution.h:114
static const char FRONT
Relative direction.
Definition: acosolution.h:30
static const char DOWN
Relative direction.
Definition: acosolution.h:27
vec3< int > previous_direction() const
Returns the relative direction between the last 2 beads.
Definition: acosolution.h:153
__device__ int norm1(int3 a)
Returns the norm 1 of an int3 (norm of the absolute value).
Definition: cuda_device_utilities.cuh:29
bool dError
May be used to indicate errors.
Definition: acosolution.h:15
T z
(x,y,z) coordinates.
Definition: vec3.h:14
static const char UP
Static constants representing relative directions.
Definition: acosolution.h:26
const std::vector< char > & directions() const
Vector of directions taken by the ant.
Definition: acosolution.h:42
std::ostream & operator<<(std::ostream &stream, const ACOSolution &sol)
Prints coordinates and directions of the solution.
Definition: acosolution.h:194
void clear_error()
Clears the internal error state.
Definition: acosolution.h:86
T x
(x,y,z) coordinates.
Definition: vec3.h:14
std::vector< vec3< int > > & vector()
Vector of bead coordinates.
Definition: acosolution.h:36
T y
(x,y,z) coordinates.
Definition: vec3.h:14
static const char RIGHT
Relative direction.
Definition: acosolution.h:29
bool has_error()
Returns current error state.
Definition: acosolution.h:80
Encapsulates a solution that is built by an ant.
Definition: acosolution.h:12
int count_contacts(const HPChain &chain) const
Returns the number of H-H contacts within the solution.
Definition: acosolution.h:158
char direction(unsigned int i)
Gets an specific direction.
Definition: acosolution.h:49
ACOSolution()
Default constructor; starts as an empty solution.
Definition: acosolution.h:97