All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sigopt.model.Experiment Maven / Gradle / Ivy

There is a newer version: 6.3.0
Show newest version
package com.sigopt.model;

import com.google.gson.reflect.TypeToken;
import com.sigopt.exception.APIException;
import com.sigopt.net.APIMethodCaller;
import com.sigopt.net.APIPathKey;
import com.sigopt.net.APIResource;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class Experiment extends APIResource {
    String id;
    String type;
    String name;
    List parameters;
    Metric metric;
    List cohorts;

    public Experiment(String id) {
        this.id = id;
    }
    public Experiment(String id, String type, String name, List parameters, Metric metric, List cohorts) {
        this.id = id;
        this.type = type;
        this.name = name;
        this.parameters = parameters;
        this.metric = metric;
        this.cohorts = cohorts;
    }

    @APIPathKey(key="id")
    public String getId() {
        return id;
    }

    public String getType() {
        return type;
    }

    public String getName() {
        return name;
    }

    public List getParameters() {
        return parameters;
    }

    public Experiment addParameter(Parameter parameter) {
        this.parameters.add(parameter);
        return this;
    }

    public List getCohorts() {
        return cohorts;
    }

    public Metric getMetric() {
        return metric;
    }

    public Experiment setMetric(Metric metric) {
        this.metric = metric;
        return this;
    }

    public static APIMethodCaller retrieve(String id) {
        return new APIMethodCaller("get", "/experiments/:id", Experiment.class).addParam("id", id);
    }

    public static APIMethodCaller> all(String clientId) {
        Type type = new TypeToken>() {}.getType();
        return new APIMethodCaller>("get", "/clients/:client_id/experiments", type).addParam("client_id", clientId);
    }

    public static APIMethodCaller create() {
        return new APIMethodCaller("post", "/experiments/create", Experiment.class);
    }

    public static APIMethodCaller create(String clientId) {
        return Experiment.create().addParam("client_id", clientId);
    }

    public static APIMethodCaller create(Experiment e, String clientId) {
        return Experiment.create(clientId).addParam("data", e);
    }

    public APIMethodCaller insert(String clientId) {
        return Experiment.create(this, clientId);
    }

    public static APIMethodCaller update(String id) {
        return new APIMethodCaller("post", "/experiments/:id/update", Experiment.class)
            .addParam("id", id);
    }

    public static APIMethodCaller update(Experiment experiment) {
        return Experiment.update(experiment.id).addParam("data", experiment);
    }

    public APIMethodCaller save() {
        return Experiment.update(this);
    }

    public APIMethodCaller reset() {
        return new APIMethodCaller("post", "/experiments/:id/reset", this, null);
    }

    public APIMethodCaller delete() {
        return new APIMethodCaller("post", "/experiments/:id/delete", this, null);
    }

    public APIMethodCaller> allocate() {
        return cohorts();
    }

    public APIMethodCaller> cohorts() {
        Type type = new TypeToken>() {}.getType();
        return new APIMethodCaller>("get", "/experiments/:id/allocate", this, type);
    }

    public APIMethodCaller createCohort() {
    return Cohort.create(this.getId());
  }

    public APIMethodCaller createCohort(Cohort cohort) {
        return Cohort.create(cohort, this.getId());
  }

    public APIMethodCaller updateCohort() {
        return Cohort.update(this.getId());
    }

    public APIMethodCaller updateCohort(Cohort cohort) {
        return cohort.save(this.getId());
    }

    public APIMethodCaller bestObservation() {
        return new APIMethodCaller("get", "/experiments/:id/bestobservation", this, Observation.class);
    }

    public APIMethodCaller> history() {
        Type type = new TypeToken>() {}.getType();
        return new APIMethodCaller>("get", "/experiments/:id/history", this, type);
    }

    public APIMethodCaller report() {
        return new APIMethodCaller("post", "/experiments/:id/report", this, null);
    }

    public APIMethodCaller report(Observation... observations) {
        if(observations.length == 1) {
            return this.report().addParam("data", observations[0]);
        } else {
            ReportMultidata.Builder builder = new ReportMultidata.Builder();
            for(Observation obs : observations) {
                builder.addObservation(obs);
            }
            return this.reportMulti(builder.build());
        }
    }

    public APIMethodCaller reportMulti() {
        return new APIMethodCaller("post", "/experiments/:id/reportmulti", this, null);
    }

    public APIMethodCaller reportMulti(ReportMultidata observation) {
        return this.reportMulti().addParam("multi_data", observation);
    }

    @Deprecated
    public APIMethodCaller suggestion() {
        return this.suggest();
    }

    public APIMethodCaller suggest() {
        return new APIMethodCaller("post", "/experiments/:id/suggest", this, Suggestion.class);
    }

    @Deprecated
    public APIMethodCaller> suggestions(Integer count) {
        return this.suggestMulti(count);
    }

    public APIMethodCaller> suggestMulti() {
        Type type = new TypeToken>() {}.getType();
        return new APIMethodCaller>("post", "/experiments/:id/suggestmulti", this, type);
    }

    public APIMethodCaller> suggestMulti(Integer count) {
        return this.suggestMulti().addParam("count", count);
    }

    public APIMethodCaller> workers() {
        Type type = new TypeToken>() {}.getType();
        return new APIMethodCaller>("get", "/experiments/:id/workers", this, type);
    }

    public APIMethodCaller releaseWorker() {
        return new APIMethodCaller("post", "/experiments/:experiment_id/releaseworker", Worker.class)
            .addParam("experiment_id", this.getId());
    }

    public APIMethodCaller releaseWorker(String workerId) {
        return this.releaseWorker().addParam("worker_id", workerId);
    }

    public static class Builder {
        String id;
        String type;
        String name;
        List parameters = new ArrayList();
        Metric metric;
        List cohorts;

        public Builder() {
        }

        public Experiment build() {
            return new Experiment(id, type, name, parameters, metric, cohorts);
        }

        public Builder id(String id) {
            this.id = id;
            return this;
        }

        public Builder type(String type) {
            this.type = type;
            return this;
        }

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder addParameter(Parameter parameter) {
            this.parameters.add(parameter);
            return this;
        }

        public Builder parameters(List parameters) {
            this.parameters = parameters;
            return this;
        }

        public Builder metric(Metric metric) {
            this.metric = metric;
            return this;
        }

        public Builder cohorts(List cohorts) {
            this.cohorts = cohorts;
            return this;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy