ElfPSP_ParallelABC
Protein Structure Prediction using Parallel Artificial Bee Colony Optimization
solution.h
Go to the documentation of this file.
1 #ifndef SOLUTION_H_
2 #define SOLUTION_H_
3 
6 #include <stdlib.h>
7 #include <string.h>
8 #include <math.h>
9 
10 #include <fitness/fitness.h>
11 #include <movelem.h>
12 #include <random.h>
13 
14 #define FITNESS_MIN -1E9
15 
16 #ifndef SOLUTION_SOURCE_CODE
17  #define SOLUTION_INLINE inline
18 #else
19  #define SOLUTION_INLINE extern inline
20 #endif
21 
23 
24 typedef struct Solution_ Solution;
25 
27 SOLUTION_INLINE
29  Solution retval;
30  retval.chain = malloc(sizeof(MovElem) * (hpSize - 1));
31  retval.fitness = FITNESS_MIN;
32  retval.idle_iterations = 0;
33  return retval;
34 }
35 
37 SOLUTION_INLINE
38 Solution Solution_copy(Solution sol, int hpSize){
39  Solution retval;
40  retval.fitness = sol.fitness;
41  retval.idle_iterations = sol.idle_iterations;
42 
43  int chainSize = hpSize - 1;
44 
45  retval.chain = malloc(sizeof(MovElem) * chainSize);
46  memcpy(retval.chain, sol.chain, sizeof(MovElem) * chainSize);
47 
48  return retval;
49 }
50 
52 SOLUTION_INLINE
54  free(sol.chain);
55 }
56 
61 SOLUTION_INLINE
63  Solution sol;
64  int nMovements = hpSize - 1;
65 
66  sol.idle_iterations = 0;
67 
68  // Generate random MovElem *
69  sol.chain = malloc(sizeof(MovElem) * nMovements);
70  int i;
71  for(i = 0; i < nMovements; i++)
72  sol.chain[i] = MovElem_random();
73 
74  sol.fitness = FITNESS_MIN;
75 
76  return sol;
77 }
78 
88 SOLUTION_INLINE
90  int chainSize = hpSize - 1;
91  int pos1 = urandom_max(chainSize);
92  int pos2 = urandom_max(chainSize);
93 
94  pos2 = pos1;
95 
96  Solution retval = Solution_copy(perturb, hpSize);
97  unsigned char elem1 = MovElem_to_number(retval.chain[pos1]);
98  unsigned char elem2 = MovElem_to_number(other.chain[pos2]);
99 
100  char distance = elem2 - (char) elem1;
101 
102  // Generate a number in [0, distance)
103  double aux = drandom_x() * (double) distance;
104 
105  // Fit the number in the discrete space [0, distance]
106  char delta = (char) round(aux);
107 
108  retval.chain[pos1] = MovElem_from_number(elem1 + delta);
109  retval.idle_iterations = 0;
110  retval.fitness = FITNESS_MIN;
111 
112  return retval;
113 }
114 
118 SOLUTION_INLINE
120  if(sol.fitness < (FITNESS_MIN + 0.1)){
121  sol.fitness = FitnessCalc_run2(sol.chain);
122  }
123  return sol.fitness;
124 }
125 
128 SOLUTION_INLINE
130  sol->fitness = fitness;
131 }
132 
136 SOLUTION_INLINE
138  return sol.idle_iterations;
139 }
140 
142 SOLUTION_INLINE
144  sol->idle_iterations++;
145 }
146 
147 
151 SOLUTION_INLINE
153  return (const MovElem *) sol.chain;
154 }
155 
156 
157 #endif
Holds the opaque structure Solution, which shouldn&#39;t be modified by files other than solution files...
RANDOM_INLINE double drandom_x()
Returns a random double within [0,1)
Definition: random.h:18
Public header file with routines for calculating the fitness of a protein represented as a chain of r...
SOLUTION_INLINE void Solution_inc_idle_iterations(Solution *sol)
Increments the number of idle solutions of the given solution.
Definition: solution.h:143
SOLUTION_INLINE int Solution_idle_iterations(Solution sol)
Returns the number of iterations through which the solution didn&#39;t improve.
Definition: solution.h:137
SOLUTION_INLINE Solution Solution_copy(Solution sol, int hpSize)
Returns a deep copy (all memory recursively duplicated) of the given solution.
Definition: solution.h:38
int idle_iterations
Number of iterations through which the food didn&#39;t improve.
Definition: solution_structure_private.h:8
double fitness
Fitness of such solution.
Definition: solution_structure_private.h:7
SOLUTION_INLINE void Solution_set_fitness(Solution *sol, double fitness)
Sets the fitness of a solution.
Definition: solution.h:129
SOLUTION_INLINE Solution Solution_blank(int hpSize)
Returns a Solution whose fields are all uninitialized, but with due memory allocated.
Definition: solution.h:28
MovElem * chain
Position of such solution.
Definition: solution_structure_private.h:6
MOVELEM_INLINE MovElem MovElem_random()
Returns a uniformly random MovElem.
Definition: movelem.h:50
MOVELEM_INLINE unsigned char MovElem_to_number(MovElem elem)
Convert a MovElem to a number.
Definition: movelem.h:70
RANDOM_INLINE unsigned int urandom_max(unsigned int max)
Returns an unsigned integer within [0,max)
Definition: random.h:24
MOVELEM_INLINE MovElem MovElem_from_number(unsigned char num)
Convert a number to MovElem.
Definition: movelem.h:80
SOLUTION_INLINE void Solution_free(Solution sol)
Frees memory allocated for given solution.
Definition: solution.h:53
SOLUTION_INLINE const MovElem * Solution_chain(Solution sol)
Returns the MovChain of the given solution.
Definition: solution.h:152
unsigned char MovElem
Type that holds 2 movements, one for the backbone and one for the side chain.
Definition: movelem.h:21
SOLUTION_INLINE double Solution_fitness(Solution sol)
Returns the fitness of the given solution, calculating it only if needed.
Definition: solution.h:119
Encapsulates a solution, which is a protein conformation that is developed by a bee.
Definition: solution_structure_private.h:5
Routines for manipulating MovElem units, which represent relative movements within a protein...
SOLUTION_INLINE Solution Solution_perturb_relative(Solution perturb, Solution other, int hpSize)
Chooses a random element ELEM1 in &#39;perturb&#39;.
Definition: solution.h:89
SOLUTION_INLINE Solution Solution_random(int hpSize)
Returns a Solution whose movement chain is uniformly random.
Definition: solution.h:62
Routines for random number generation.