| 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 |
package dr.evomodel.antigenic; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
import dr.inference.model.*; |
| 31 |
import dr.inference.operators.*; |
| 32 |
import dr.math.MathUtils; |
| 33 |
import dr.xml.*; |
| 34 |
|
| 35 |
import java.util.logging.Logger; |
| 36 |
|
| 37 |
|
| 38 |
import dr.inference.model.Parameter; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class CRPClusterRandomWalkOperator extends AbstractAdaptableOperator { |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public static final String WINDOW_SIZE = "windowSize"; |
| 53 |
public static final String VIRUS_LOCATIONS = "virusLocations"; |
| 54 |
public static final String CLUSTER_ASSIGNMENTS = "clusterAssignments"; |
| 55 |
public static final String CRP_CLUSTER_RW_OPERATOR= "CRPClusterRandomWalkOperator"; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
private final MatrixParameter virusLocations; |
| 60 |
private final Parameter assignments; |
| 61 |
private double windowSize; |
| 62 |
|
| 63 |
public CRPClusterRandomWalkOperator( Parameter assignments, MatrixParameter virusLocations,double windowSize, double weight){ |
| 64 |
super(AdaptationMode.ADAPTATION_ON); |
| 65 |
|
| 66 |
this.virusLocations=virusLocations; |
| 67 |
this.windowSize = windowSize; |
| 68 |
this.assignments=assignments; |
| 69 |
setWeight(weight); |
| 70 |
|
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
public double doOperation() { |
| 76 |
|
| 77 |
int max = findMax(assignments); |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
int group = MathUtils.nextInt(max+1); |
| 83 |
|
| 84 |
|
| 85 |
if (windowSize>50){ |
| 86 |
windowSize=50; |
| 87 |
} |
| 88 |
|
| 89 |
double draw0 = (2.0 * MathUtils.nextDouble() - 1.0) * windowSize; |
| 90 |
double draw1 = (2.0 * MathUtils.nextDouble() - 1.0) * windowSize; |
| 91 |
|
| 92 |
|
| 93 |
for (int i = 0; i<assignments.getDimension(); i++){ |
| 94 |
if ((int)assignments.getParameterValue(i)==group){ |
| 95 |
double newValue0 = virusLocations.getParameter(i).getParameterValue(0) + draw0; |
| 96 |
double newValue1 = virusLocations.getParameter(i).getParameterValue(1) + draw1; |
| 97 |
|
| 98 |
|
| 99 |
virusLocations.setParameterValue(2*i,newValue0); |
| 100 |
virusLocations.setParameterValue(2*i+1,newValue1); |
| 101 |
|
| 102 |
virusLocations.fireParameterChangedEvent(2*i,Variable.ChangeType.VALUE_CHANGED); |
| 103 |
virusLocations.fireParameterChangedEvent(2*i+1,Variable.ChangeType.VALUE_CHANGED); |
| 104 |
|
| 105 |
|
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
return 0.0; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
private int findMax(Parameter assignments){ |
| 122 |
int max=0; |
| 123 |
for (int i=0; i<assignments.getDimension(); i++ ){ |
| 124 |
if (assignments.getParameterValue(i)>max){ |
| 125 |
max = (int) assignments.getParameterValue(i); |
| 126 |
} |
| 127 |
} |
| 128 |
return max; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
private void printInformation(double[] par, int length) { |
| 135 |
StringBuffer sb = new StringBuffer("\n \n double vector \n"); |
| 136 |
for(int j=0; j<length; j++){ |
| 137 |
sb.append(par[j] + "\t"); |
| 138 |
} |
| 139 |
Logger.getLogger("dr.evomodel").info(sb.toString()); }; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
private void printInformation(Parameter par, String lala) { |
| 146 |
StringBuffer sb = new StringBuffer("\n "); |
| 147 |
sb.append(lala); |
| 148 |
sb.append("\t\t") ; |
| 149 |
|
| 150 |
for (int i=0; i<par.getDimension(); i++){ |
| 151 |
sb.append(par.getParameterValue(i)+ "\t"); } |
| 152 |
|
| 153 |
Logger.getLogger("dr.evomodel").info(sb.toString()); }; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
private void printInformation(int par, String lala) { |
| 160 |
StringBuffer sb = new StringBuffer("\n"); |
| 161 |
|
| 162 |
sb.append(lala); |
| 163 |
sb.append("\t\t"); |
| 164 |
sb.append(par); |
| 165 |
|
| 166 |
Logger.getLogger("dr.evomodel").info(sb.toString()); }; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
private void printInformation(double par, String lala) { |
| 173 |
StringBuffer sb = new StringBuffer("\n"); |
| 174 |
|
| 175 |
sb.append(lala); |
| 176 |
sb.append("\t\t"); |
| 177 |
sb.append(par); |
| 178 |
|
| 179 |
Logger.getLogger("dr.evomodel").info(sb.toString()); }; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public String getAdaptableParameterName() { |
| 185 |
return "windowSize"; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public String getOperatorName() { |
| 192 |
return CRP_CLUSTER_RW_OPERATOR; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public double getAdaptableParameter() { |
| 198 |
return Math.log(windowSize); |
| 199 |
} |
| 200 |
|
| 201 |
public void setAdaptableParameter(double value) { |
| 202 |
windowSize = Math.exp(value); |
| 203 |
} |
| 204 |
|
| 205 |
public double getRawParameter() { |
| 206 |
return windowSize; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() { |
| 212 |
|
| 213 |
public String getParserName() { |
| 214 |
return CRP_CLUSTER_RW_OPERATOR; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
double weight = xo.getDoubleAttribute(WEIGHT); |
| 225 |
double windowSize = xo.getDoubleAttribute(WINDOW_SIZE); |
| 226 |
|
| 227 |
|
| 228 |
MatrixParameter virusLocations = (MatrixParameter) xo.getElementFirstChild(VIRUS_LOCATIONS); |
| 229 |
Parameter assignments = (Parameter) xo.getElementFirstChild(CLUSTER_ASSIGNMENTS); |
| 230 |
return new CRPClusterRandomWalkOperator(assignments,virusLocations,windowSize,weight); |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public String getParserDescription() { |
| 239 |
return "This element returns an operator moves simultaneously locations for all viruses in same cluster in ddCRP and BMDS."; |
| 240 |
} |
| 241 |
|
| 242 |
public Class getReturnType() { |
| 243 |
return CRPClusterRandomWalkOperator.class; |
| 244 |
} |
| 245 |
|
| 246 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 247 |
return rules; |
| 248 |
} |
| 249 |
|
| 250 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 251 |
new ElementRule(VIRUS_LOCATIONS, MatrixParameter.class), |
| 252 |
new ElementRule(CLUSTER_ASSIGNMENTS, Parameter.class), |
| 253 |
|
| 254 |
AttributeRule.newDoubleRule(WEIGHT), |
| 255 |
AttributeRule.newDoubleRule(WINDOW_SIZE), |
| 256 |
}; |
| 257 |
}; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|