|
| 1 | +package advisor.client; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.UnsupportedEncodingException; |
| 5 | +import java.text.ParseException; |
| 6 | +import java.util.Iterator; |
| 7 | +import java.util.LinkedList; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import org.apache.http.client.ClientProtocolException; |
| 11 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 12 | +import org.apache.http.client.methods.HttpGet; |
| 13 | +import org.apache.http.client.methods.HttpPost; |
| 14 | +import org.apache.http.client.methods.HttpPut; |
| 15 | +import org.apache.http.entity.StringEntity; |
| 16 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 17 | +import org.apache.http.impl.client.HttpClients; |
| 18 | +import org.apache.http.util.EntityUtils; |
| 19 | +import org.json.JSONArray; |
| 20 | +import org.json.JSONException; |
| 21 | +import org.json.JSONObject; |
| 22 | + |
| 23 | +import com.google.common.annotations.Beta; |
| 24 | + |
| 25 | + |
| 26 | +/** |
| 27 | + * An advisor client for Java |
| 28 | + * Based on the python implementation from https://github.com/tobegit3hub/advisor/tree/master/advisor_client |
| 29 | + * @author AndresM |
| 30 | + * |
| 31 | + */ |
| 32 | +public class AdvisorClient { |
| 33 | + |
| 34 | + private String endpoint="http://0.0.0.0:8000"; |
| 35 | + |
| 36 | + public AdvisorClient(){ |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + public AdvisorClient(String endpoint){ |
| 41 | + this.endpoint=endpoint; |
| 42 | + } |
| 43 | + |
| 44 | + public Study createStudy(String name, JSONObject studyConfiguration, String algorithm) throws ClientProtocolException, IOException, JSONException, ParseException{ |
| 45 | + String url= endpoint+"/suggestion/v1/studies"; |
| 46 | + |
| 47 | + JSONObject requestData= new JSONObject(); |
| 48 | + requestData.put("name", name); |
| 49 | + requestData.put("study_configuration", studyConfiguration); |
| 50 | + requestData.put("algorithm", algorithm); |
| 51 | + |
| 52 | + StringEntity entity = new StringEntity(requestData.toString()); |
| 53 | + CloseableHttpClient httpclient = HttpClients.createDefault(); |
| 54 | + |
| 55 | + HttpPost httpPost= new HttpPost(url); |
| 56 | + httpPost.setEntity(entity); |
| 57 | + httpPost.setHeader("Accept", "application/json"); |
| 58 | + httpPost.setHeader("Content-type", "application/json"); |
| 59 | + |
| 60 | + CloseableHttpResponse responseHttp = httpclient.execute(httpPost); |
| 61 | + if(responseHttp.getStatusLine().getStatusCode()==200){ |
| 62 | + String response=EntityUtils.toString(responseHttp.getEntity()) ; |
| 63 | + JSONObject responseJSON= new JSONObject(response); |
| 64 | + return new Study(responseJSON.getJSONObject("data")); |
| 65 | + } |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + public List<Study> listStudies() throws ClientProtocolException, IOException, JSONException, ParseException{ |
| 70 | + |
| 71 | + LinkedList<Study> list= new LinkedList<Study>(); |
| 72 | + String url= endpoint+"/suggestion/v1/studies"; |
| 73 | + |
| 74 | + CloseableHttpClient httpclient = HttpClients.createDefault(); |
| 75 | + |
| 76 | + HttpGet httpGet= new HttpGet(url); |
| 77 | + httpGet.setHeader("Accept", "application/json"); |
| 78 | + httpGet.setHeader("Content-type", "application/json"); |
| 79 | + |
| 80 | + CloseableHttpResponse responseHttp = httpclient.execute(httpGet); |
| 81 | + if(responseHttp.getStatusLine().getStatusCode()==200){ |
| 82 | + String response=EntityUtils.toString(responseHttp.getEntity()) ; |
| 83 | + JSONObject responseJSON= new JSONObject(response); |
| 84 | + JSONArray responseJSONData=responseJSON.getJSONArray("data"); |
| 85 | + Iterator<Object> studies=responseJSONData.iterator(); |
| 86 | + while(studies.hasNext()){ |
| 87 | + list.add(new Study((JSONObject) studies.next())); |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + return list; |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + public Study getStudyById(int studyId) throws ClientProtocolException, IOException, JSONException, ParseException{ |
| 97 | + String url= endpoint+"/suggestion/v1/studies/"+studyId; |
| 98 | + CloseableHttpClient httpclient = HttpClients.createDefault(); |
| 99 | + |
| 100 | + HttpGet httpGet= new HttpGet(url); |
| 101 | + httpGet.setHeader("Accept", "application/json"); |
| 102 | + httpGet.setHeader("Content-type", "application/json"); |
| 103 | + |
| 104 | + CloseableHttpResponse responseHttp = httpclient.execute(httpGet); |
| 105 | + if(responseHttp.getStatusLine().getStatusCode()==200){ |
| 106 | + String response=EntityUtils.toString(responseHttp.getEntity()) ; |
| 107 | + JSONObject responseJSON= new JSONObject(response); |
| 108 | + |
| 109 | + return new Study(responseJSON.getJSONObject("data")); |
| 110 | + } |
| 111 | + return null; |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + public List<Trial> getSuggestions(int studyId, int trialsNumber) throws ClientProtocolException, IOException, JSONException, ParseException{ |
| 116 | + LinkedList<Trial> list= new LinkedList<Trial>(); |
| 117 | + if(trialsNumber<=0) |
| 118 | + trialsNumber=1; |
| 119 | + String url= endpoint+"/suggestion/v1/studies/"+studyId+"/suggestions"; |
| 120 | + JSONObject requestData= new JSONObject(); |
| 121 | + |
| 122 | + requestData.put("trials_number", trialsNumber); |
| 123 | + StringEntity entity = new StringEntity(requestData.toString()); |
| 124 | + CloseableHttpClient httpclient = HttpClients.createDefault(); |
| 125 | + |
| 126 | + HttpPost httpPost= new HttpPost(url); |
| 127 | + httpPost.setEntity(entity); |
| 128 | + httpPost.setHeader("Accept", "application/json"); |
| 129 | + httpPost.setHeader("Content-type", "application/json"); |
| 130 | + |
| 131 | + CloseableHttpResponse responseHttp = httpclient.execute(httpPost); |
| 132 | + if(responseHttp.getStatusLine().getStatusCode()==200){ |
| 133 | + String response=EntityUtils.toString(responseHttp.getEntity()) ; |
| 134 | + JSONObject responseJSON= new JSONObject(response); |
| 135 | + JSONArray responseJSONData=responseJSON.getJSONArray("data"); |
| 136 | + Iterator<Object> trials=responseJSONData.iterator(); |
| 137 | + while(trials.hasNext()){ |
| 138 | + list.add(new Trial((JSONObject) trials.next())); |
| 139 | + } |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + return list; |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + public boolean isStudyDone(int studyId) throws ClientProtocolException, JSONException, IOException, ParseException{ |
| 148 | + Study study=getStudyById(studyId); |
| 149 | + |
| 150 | + if(study==null) |
| 151 | + return false; |
| 152 | + |
| 153 | + |
| 154 | + if(Study.COMPLETED.equals(study.getStatus())) |
| 155 | + return true; |
| 156 | + |
| 157 | + List<Trial> trials=listTrials(studyId); |
| 158 | + for (Trial trial : trials){ |
| 159 | + if(!Trial.COMPLETED.equals(trial.getStatus())) |
| 160 | + return false; |
| 161 | + } |
| 162 | + String url= endpoint+"/suggestion/v1/studies/"+studyId; |
| 163 | + |
| 164 | + JSONObject putData= new JSONObject(); |
| 165 | + putData.put("status", "Completed"); |
| 166 | + StringEntity entity = new StringEntity(putData.toString()); |
| 167 | + CloseableHttpClient httpclient = HttpClients.createDefault(); |
| 168 | + |
| 169 | + HttpPut httpPut= new HttpPut(url); |
| 170 | + httpPut.setEntity(entity); |
| 171 | + httpPut.setHeader("Accept", "application/json"); |
| 172 | + httpPut.setHeader("Content-type", "application/json"); |
| 173 | + |
| 174 | + CloseableHttpResponse responseHttp = httpclient.execute(httpPut); |
| 175 | + if(responseHttp.getStatusLine().getStatusCode()==200) |
| 176 | + return true; |
| 177 | + |
| 178 | + //Warning: No ok from server |
| 179 | + return true; |
| 180 | + } |
| 181 | + |
| 182 | + public List<Trial> listTrials(int studyId) throws ClientProtocolException, IOException, JSONException, ParseException{ |
| 183 | + LinkedList<Trial> list= new LinkedList<Trial>(); |
| 184 | + String url= endpoint+"/suggestion/v1/studies/"+studyId+"/trials"; |
| 185 | + CloseableHttpClient httpclient = HttpClients.createDefault(); |
| 186 | + |
| 187 | + HttpGet httpGet= new HttpGet(url); |
| 188 | + httpGet.setHeader("Accept", "application/json"); |
| 189 | + httpGet.setHeader("Content-type", "application/json"); |
| 190 | + |
| 191 | + CloseableHttpResponse responseHttp = httpclient.execute(httpGet); |
| 192 | + if(responseHttp.getStatusLine().getStatusCode()==200){ |
| 193 | + String response=EntityUtils.toString(responseHttp.getEntity()) ; |
| 194 | + JSONObject responseJSON= new JSONObject(response); |
| 195 | + JSONArray responseJSONData=responseJSON.getJSONArray("data"); |
| 196 | + Iterator<Object> trials=responseJSONData.iterator(); |
| 197 | + while(trials.hasNext()){ |
| 198 | + list.add(new Trial((JSONObject) trials.next())); |
| 199 | + } |
| 200 | + |
| 201 | + } |
| 202 | + return list; |
| 203 | + } |
| 204 | + |
| 205 | + @Beta |
| 206 | + public List<TrialMetric> listTrialMetrics(int studyId, int trialId){ |
| 207 | + return null; |
| 208 | + } |
| 209 | + |
| 210 | + public Trial getBestTrial(int studyId) throws ClientProtocolException, JSONException, IOException, ParseException{ |
| 211 | + if(!this.isStudyDone(studyId)) |
| 212 | + return null; |
| 213 | + |
| 214 | + Study st= this.getStudyById(studyId); |
| 215 | + JSONObject configuration=st.getStudy_configuration(); |
| 216 | + String studyGoal=configuration.getString("goal"); |
| 217 | + List<Trial> trials=this.listTrials(studyId); |
| 218 | + |
| 219 | + Trial bestTrial=null; |
| 220 | + for (Trial trial : trials) { |
| 221 | + if (trial.getObjectiveValue() != null) { |
| 222 | + if(bestTrial==null) |
| 223 | + bestTrial=trial; |
| 224 | + else if (studyGoal.equals(Study.MAXIMIZE)) { |
| 225 | + if(trial.getObjectiveValue()>bestTrial.getObjectiveValue()){ |
| 226 | + bestTrial=trial; |
| 227 | + } |
| 228 | + } |
| 229 | + else if (studyGoal.equals(Study.MINIMIZE)) { |
| 230 | + if(trial.getObjectiveValue()<bestTrial.getObjectiveValue()){ |
| 231 | + bestTrial=trial; |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + } |
| 237 | + |
| 238 | + |
| 239 | + return bestTrial; |
| 240 | + } |
| 241 | + |
| 242 | + public Trial getTrial(int studyId, int trialId) throws ClientProtocolException, IOException, JSONException, ParseException{ |
| 243 | + String url= endpoint+"/suggestion/v1/studies/"+studyId+"/trials/"+trialId; |
| 244 | + |
| 245 | + CloseableHttpClient httpclient = HttpClients.createDefault(); |
| 246 | + |
| 247 | + HttpGet httpGet= new HttpGet(url); |
| 248 | + httpGet.setHeader("Accept", "application/json"); |
| 249 | + httpGet.setHeader("Content-type", "application/json"); |
| 250 | + |
| 251 | + CloseableHttpResponse responseHttp = httpclient.execute(httpGet); |
| 252 | + if(responseHttp.getStatusLine().getStatusCode()==200){ |
| 253 | + String response=EntityUtils.toString(responseHttp.getEntity()) ; |
| 254 | + JSONObject responseJSON= new JSONObject(response); |
| 255 | + |
| 256 | + return new Trial(responseJSON.getJSONObject("data")); |
| 257 | + } |
| 258 | + return null; |
| 259 | + } |
| 260 | + |
| 261 | + public TrialMetric createTrialMetric(int studyId, int trialId, JSONObject trainingStep, Double objectiveValue) throws ClientProtocolException, IOException, JSONException, ParseException{ |
| 262 | + String url= endpoint+"/suggestion/v1/studies/"+studyId+"/trials/"+trialId+"/metrics"; |
| 263 | + |
| 264 | + JSONObject requestData= new JSONObject(); |
| 265 | + requestData.put("training_step", trainingStep==null?JSONObject.NULL:trainingStep); |
| 266 | + requestData.put("objective_value", objectiveValue); |
| 267 | + |
| 268 | + StringEntity entity = new StringEntity(requestData.toString()); |
| 269 | + CloseableHttpClient httpclient = HttpClients.createDefault(); |
| 270 | + |
| 271 | + HttpPost httpPost= new HttpPost(url); |
| 272 | + httpPost.setEntity(entity); |
| 273 | + httpPost.setHeader("Accept", "application/json"); |
| 274 | + httpPost.setHeader("Content-type", "application/json"); |
| 275 | + |
| 276 | + CloseableHttpResponse responseHttp = httpclient.execute(httpPost); |
| 277 | + if(responseHttp.getStatusLine().getStatusCode()==200){ |
| 278 | + String response=EntityUtils.toString(responseHttp.getEntity()) ; |
| 279 | + JSONObject responseJSON= new JSONObject(response); |
| 280 | + return new TrialMetric(responseJSON.getJSONObject("data")); |
| 281 | + } |
| 282 | + return null; |
| 283 | + |
| 284 | + } |
| 285 | + |
| 286 | + public static void main(String[] args) { |
| 287 | + |
| 288 | + AdvisorClient cl= new AdvisorClient(); |
| 289 | + try { |
| 290 | + JSONObject studyConfiguration= new JSONObject("{\"goal\": \"MAXIMIZE\", \"randomInitTrials\": 3, \"maxTrials\": 5, \"maxParallelTrials\": 1, \"params\": [ { \"parameterName\": \"hidden1\", \"type\": \"INTEGER\", \"minValue\": 1, \"maxValue\": 10, \"scallingType\": \"LINEAR\"}, {\"parameterName\": \"learning_rate\",\"type\": \"DOUBLE\", \"minValue\": 0.01, \"maxValue\": 0.5, \"scallingType\": \"LINEAR\" } ]}"); |
| 291 | + Study st=cl.createStudy("StudyPruebaTest", studyConfiguration, Study.BAYESIAN_OPTIMIZATION); |
| 292 | + System.out.println(st.getAlgorithm()); |
| 293 | + |
| 294 | + } catch (Exception e) { |
| 295 | + // TODO Auto-generated catch block |
| 296 | + e.printStackTrace(); |
| 297 | + } |
| 298 | + |
| 299 | + } |
| 300 | + |
| 301 | +} |
0 commit comments