-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvienna_constraint.h
184 lines (157 loc) · 6.8 KB
/
vienna_constraint.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/******************************************************************************
* Copyright (C) 2014 Juan Antonio Garcia Martin , Peter Clote, Ivan Dotu *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************/
#include <string>
#include <vector>
#include <map>
#include <limits.h>
#include <ctime>
#include "base/logging.h"
#include "util/string_array.h"
#include "constraint_solver/constraint_solver.h"
#include "constraint_solver/constraint_solveri.h"
#include "rna_plugin.h"
namespace operations_research {
// This constraint maintains:
//
//
class ViennaConstraint : public Constraint {
public:
// This constructor does not take any ownership on its arguments.
ViennaConstraint(Solver* const s, const std::vector<IntVar*>& vars, const std::vector<int64>& structure, int dangles, std::string rnaLib, std::string energyModel, double foldTemp, int cutPoint, int posLeft, int posRight, const IntVar* varLeft,const IntVar* varRight, double upperMFE, double lowerMFE) : Constraint(s), vars_(vars), structure_(structure), cutPoint_(cutPoint), posLeft_(posLeft), posRight_(posRight), varLeft_(varLeft), varRight_(varRight), upperMFE_((float)upperMFE), lowerMFE_((float)lowerMFE) {
int i;
n=size();
v = newRNAplugin(rnaLib,n,dangles);
v->setEnergyModel(energyModel);
v->setTemperature(foldTemp);
if(cutPoint!= -1){
v->setCutPoint(cutPoint_);
}
targetStr = (char *) malloc(sizeof(char)*(n+1));
for(i=1;i<=n;i++){
if(structure[i]<0){
targetStr[i-1]='.';
}
else if(structure[i]<i){
targetStr[i-1]=')';
}
else{
targetStr[i-1]='(';
}
}
targetStr[n]='\0';
mfeStr = (int *) malloc(sizeof(int)*(n+1));
// Initialize variables for checking sides
if(varLeft_!= NULL || varRight_!= NULL){
checkSides= 1;
nSides = size()+(varLeft_==NULL? 0: 1)+(varRight_==NULL? 0: 1);
vSides = newRNAplugin(rnaLib,nSides,dangles);
vSides->setEnergyModel(energyModel);
vSides->setTemperature(foldTemp);
mfeStrSides = (int *) malloc(sizeof(int)*(nSides+1));
if(cutPoint!= -1){
vSides->setCutPoint(cutPoint_+(varLeft_==NULL? 0: 1));
}
if(varLeft_!=NULL){
leftIt_ = varLeft_->MakeDomainIterator(true);
}
if(varRight_!=NULL){
rightIt_ = varRight_->MakeDomainIterator(true);
}
}
else{
checkSides= 0;
}
/* // TIME FOR RESTART
nRestarts=0;
time(&timeLastFail);*/
}
virtual ~ViennaConstraint() {
free(targetStr);
delete v;
}
// Adds observers (named Demon) to variable events. These demons are
// responsible for implementing the propagation algorithm of the
// constraint.
virtual void Post() {
// Create a demon 'global_demon' that will bind events on
// variables to the calling of the 'InitialPropagate()' method. As
// this method is expensive, 'global_demon' has a low priority. As
// such, InitialPropagate will be called after all normal demons
// and constraints have reached a fixed point. Note
// that ownership of the 'global_demon' belongs to the solver.
//Demon* const global_demon = solver()->MakeDelayedConstraintInitialPropagateCallback(this);
Demon* const global_demon = solver()->MakeConstraintInitialPropagateCallback(this);
// Attach to all variables.
for (int i = 0; i < size(); ++i) {
vars_[i]->WhenBound(global_demon);
}
}
protected:
std::vector<IntVar*> vars_;
const std::vector<int64> structure_;
int* mfeStr;
int cutPoint_;
int posLeft_;
int posRight_;
int64 size() const { return vars_.size(); };
RNAPlugin* v;
int n;
char* targetStr;
double upperMFE_;
double lowerMFE_;
//Variables for checking sides
int nSides;
int* mfeStrSides;
int checkSides;
const IntVar* varLeft_;
const IntVar* varRight_;
IntVarIterator* leftIt_;
IntVarIterator* rightIt_;
RNAPlugin* vSides;
/* // TIME FOR RESTART
int nRestarts;
time_t timeLastFail;*/
};
class ViennaConstraintUndet : public ViennaConstraint {
public:
ViennaConstraintUndet(Solver* const s, const std::vector<IntVar*>& vars, const std::vector<int64>& structure, int dangles, std::string rnaLib, std::string energyModel, double foldTemp, int cutPoint, int posLeft, int posRight, const IntVar* varLeft,const IntVar* varRight, double upperMFE, double lowerMFE) : ViennaConstraint(s,vars,structure,dangles,rnaLib,energyModel,foldTemp,cutPoint, posLeft, posRight, varLeft, varRight, upperMFE, lowerMFE) {}
// This is the main propagation method.
//
// It scans all variables are bound, storing those variables with more than one element in its domain
//
// If all variables are bound, calculates the MFE structure and compares with the target structure
//
// If the structure is not correct and there are no more possible values it sends the fail signal
//
// I not, it removes the assigned value from the domain of one the free variables
virtual void InitialPropagate();
};
class ViennaConstraintDet : public ViennaConstraint {
public:
ViennaConstraintDet(Solver* const s, const std::vector<IntVar*>& vars, const std::vector<int64>& structure, int dangles, std::string rnaLib, std::string energyModel, double foldTemp, int cutPoint, int posLeft, int posRight, const IntVar* varLeft,const IntVar* varRight, double upperMFE, double lowerMFE) : ViennaConstraint(s,vars,structure,dangles,rnaLib,energyModel,foldTemp,cutPoint, posLeft, posRight, varLeft, varRight, upperMFE, lowerMFE) {}
// This is the main propagation method.
//
// It scans all variables are bound, storing those variables with more than one element in its domain
//
// If all variables are bound, calculates the MFE structure and compares with the target structure
//
// If the structure is not correct and there are no more possible values it sends the fail signal
//
// I not, it removes the assigned value from the domain of one the free variables
virtual void InitialPropagate();
};
}