Beispiel #1
0
        public TrialConfig GetConfig(string version = "0.01")
        {
            if (status != ClientStatus.GotResponse)
            {
                Debug.Log("Error! Called getConfig() when there is no reply available! Current status is " + status);
            }
            SetStatus(ClientStatus.Ready);
            if (version == "0.01")
            {
                try
                {
                    TrialWithFinished config = JsonConvert.DeserializeObject <TrialWithFinished>(reply);
                    finished   = config.is_finished;
                    baseConfig = config.config;
                }
                catch
                {
                    Debug.LogError("Tried to deserialize invalid server message: " + reply);
                }
            }
            else
            {
                try
                {
                    baseConfig = JsonConvert.DeserializeObject <TrialConfig>(reply);
                }
                catch
                {
                    Debug.LogError("Tried to deserialize invalid server message: " + reply);
                }
            }

            return(baseConfig);
        }
Beispiel #2
0
        public IEnumerator Tell(TrialConfig trialConfig, int outcome)
        {
            TrialWithOutcome message = new TrialWithOutcome(trialConfig, outcome);
            Request          req     = new Request(message, RequestType.tell);

            yield return(StartCoroutine(this.SendRequest(JsonConvert.SerializeObject(req))));
        }
Beispiel #3
0
 public TrialData(string timeStamp, TrialConfig config, int outcome, TrialMetadata extra_info = null)
 {
     this.timeStamp  = timeStamp;
     this.config     = config;
     this.outcome    = outcome;
     this.extra_info = extra_info;
 }
Beispiel #4
0
        public bool probability_space;  //whether to use probability space or latent space

        public QueryMessage(QueryType queryType, TrialConfig x, float y, TrialConfig constraints, bool probability_space)
        {
            this.query_type        = queryType;
            this.x                 = x;
            this.y                 = y;
            this.constraints       = constraints;
            this.probability_space = probability_space;
        }
Beispiel #5
0
        IEnumerator QueryAndDisplayOptimal()
        {
            SetState(ExperimentState.Exploring);
            yield return(StartCoroutine(client.Query(QueryType.max)));

            QueryMessage m      = client.GetQueryResponse();
            TrialConfig  maxLoc = m.x;

            ShowStimuli(maxLoc);
        }
Beispiel #6
0
 public TrialConfig GetConfig()
 {
     if (status != ClientStatus.GotResponse)
     {
         Debug.Log("Error! Called getConfig() when there is no reply available! Current status is " + status);
     }
     status     = ClientStatus.Ready;
     baseConfig = JsonConvert.DeserializeObject <TrialConfig>(reply);
     return(baseConfig);
 }
Beispiel #7
0
        public IEnumerator Tell(TrialConfig trialConfig, int outcome, TrialMetadata metadata = null)
        {
            TrialWithOutcome message = new TrialWithOutcome(trialConfig, outcome);

            Request req;

            if (metadata != null)
            {
                req = new Request(message, RequestType.tell, metadata);
            }
            else
            {
                req = new Request(message, RequestType.tell);
            }
            yield return(StartCoroutine(this.SendRequest(JsonConvert.SerializeObject(req))));
        }
Beispiel #8
0
        public IEnumerator Query(QueryType queryType, TrialConfig x = null, float y = 0, TrialConfig constraints = null, bool probability_space = false)
        {
            if (x == null)
            {
                x = new TrialConfig {
                };
            }
            if (constraints == null)
            {
                constraints = new TrialConfig()
                {
                };
            }

            QueryMessage message = new QueryMessage(queryType, x, y, constraints, probability_space);
            Request      req     = new Request(message, RequestType.query);
            string       s       = JsonConvert.SerializeObject(req);

            yield return(StartCoroutine(this.SendRequest(s)));
        }
Beispiel #9
0
 public TrialWithOutcome(TrialConfig config, int outcome)
 {
     this.config  = config;
     this.outcome = outcome;
 }
Beispiel #10
0
 public TrialWithFinished(TrialConfig config, bool is_finished)
 {
     this.config      = config;
     this.is_finished = is_finished;
 }
Beispiel #11
0
        /// <summary>
        /// Callback for AEPsych Client Status Changes. This syncs the Experiment class with the AEPsychClient class and manages state transitions.
        /// // Expected order of transitions:
        /// NotConnected =>                           occurs before the experiment begins.
        ///   WaitingForResumeResponse =>             occurs after sending Resume() query to server.
        ///     WaitingForAsk =>                      occurs when resume confirmation is received.
        ///       WaitingForAskResponse =>            occurs while client awaits AEPsych server's test case selection
        ///         ConfigReady =>                    occurs when suggested test case is received from server
        ///           WaitingForTell =>               occurs while waiting for the user to respond to stimulus
        ///            WaitingForTellResponse =>     occurs as soon as user response is sent to server
        ///   WaitingForAsk => ...
        /// </summary>
        /// <param name="oldStatus"></param>
        /// <param name="newStatus"></param>
        void OnStatusChanged(AEPsychClient.ClientStatus oldStatus, AEPsychClient.ClientStatus newStatus)
        {
            AEPsychClient.Log(string.Format("OnStatusChanged Callback. Experiment status: {0}, " +
                                            "old client: {1}, new client: {2}", _experimentState, oldStatus, newStatus));
            if (_experimentState == ExperimentState.NotConnected)
            {
                if (newStatus == AEPsychClient.ClientStatus.GotResponse)
                {
                    OnConnectToServer();
                }
            }
            else if (_experimentState == ExperimentState.Exploring)
            {
                if (newStatus == AEPsychClient.ClientStatus.QuerySent)
                {
                    SetState(ExperimentState.WaitingForQueryResponse);
                }
            }
            else if (_experimentState == ExperimentState.WaitingForTellResponse)
            {
                if (newStatus == AEPsychClient.ClientStatus.GotResponse)
                {
                    if (useModelExploration && !readyToQuery)
                    {
                        // Check if the model is built and ready for queries
                        StartCoroutine(CheckQueryReady());
                    }
                    else
                    {
                        StartCoroutine(EndTrial());
                    }
                }
            }
            else if (_experimentState == ExperimentState.WaitingForResumeResponse)
            {
                if (newStatus == AEPsychClient.ClientStatus.GotResponse)
                {
                    SetState(ExperimentState.WaitingForAsk);
                }
            }
            else if (_experimentState == ExperimentState.WaitingForAskResponse)
            {
                if (newStatus == AEPsychClient.ClientStatus.GotResponse)
                {
                    // We recieved a new config. Store it.
                    config = client.GetConfig();
                    SetState(ExperimentState.ConfigReady);
                    if (!client.finished)
                    {
                        ShowStimuli(config);
                    }
                    else
                    {
                        isDone = true;
                        StartCoroutine(EndTrial());
                    }
                }
            }

            else if (_experimentState == ExperimentState.ConfigReady)
            {
                if (newStatus == AEPsychClient.ClientStatus.Ready)
                {
                    SetState(ExperimentState.WaitingForTell);
                    // Enable or Disable Model Querying based on client status
                    CheckUserResponse(); // Should call ReportResultToServer()
                                         // when response is collected
                }
            }
            else if (_experimentState == ExperimentState.WaitingForAsk)
            {
                if (newStatus == AEPsychClient.ClientStatus.QuerySent)
                {
                    SetState(ExperimentState.WaitingForAskResponse);
                }
            }
            else if (_experimentState == ExperimentState.WaitingForTell)
            {
                if (newStatus == AEPsychClient.ClientStatus.QuerySent)
                {
                    SetState(ExperimentState.WaitingForTellResponse);
                }
            }
            else if (_experimentState == ExperimentState.WaitingForQueryResponse)
            {
                if (newStatus == AEPsychClient.ClientStatus.GotResponse)
                {
                    //QueryMessage m = client.GetQueryResponse();
                    //ReceiveExplorationQuery(m.x);
                    SetState(ExperimentState.Exploring);
                }
            }
            else if (_experimentState == ExperimentState.WaitingForCanModelResponse)
            {
                if (newStatus == AEPsychClient.ClientStatus.GotResponse)
                {
                    readyToQuery = client.GetModelResponse();
                    StartCoroutine(EndTrial());
                }
            }
        }
Beispiel #12
0
        /// <summary>
        /// Called when this game object becomes active. Generates strategies if they are un-initialized
        /// </summary>
        public IEnumerator ConnectAndGenerateStrategies()
        {
            yield return(new WaitForSeconds(0.2f));

            // Set status change callback, which enables our study state machine logic
            client.onStatusChanged += OnStatusChanged;

            // Set up experiment params
            TrialConfig baseConfig = new TrialConfig()
            {
            };

            // Quit if experiment params are invalid
            if (!configGenerator.CheckParamValidity())
            {
                Debug.Break();
            }

            if (configGenerator.isAutomatic) // Initialize the automatically generated strategy
            {
                if (PlayerPrefs.GetString(configGenerator.experimentName + "config") == "Empty")
                {
                    Debug.LogError("Config has zero dimensions. Terminating Experiment...");
                    TerminateExperiment();
                    yield return(null);
                }
                else
                {
                    strategy   = gameObject.AddComponent <AEPsychStrategy>();
                    configPath = configGenerator.GetConfigText();
                    //configPath = PlayerPrefs.GetString(configGenerator.experimentName + "config");
                    yield return(StartCoroutine(strategy.InitStrat(client, configPath: configPath, false)));
                }
            }
            else // Initialize each manually added strategy
            {
                // Ensure that the user has provided at least one strategy config
                if (configGenerator.manualConfigFile == null)
                {
                    Debug.LogError("Must assign at least one strategy config file if using manual configs. Assign a config file in the inspector.");
                    TerminateExperiment();
                    yield return(null);
                }
                if (strategy == null)
                {
                    configPath = Path.Combine(Application.dataPath, "../",
                                              AssetDatabase.GetAssetPath(configGenerator.manualConfigFile));
                    strategy = gameObject.AddComponent <AEPsychStrategy>();
                    yield return(StartCoroutine(strategy.InitStrat(client, configPath: configPath)));
                }
            }

            // Initialize a CSV Writer for each strategy
            if (recordToCSV)
            {
                if (csvFile == null)
                {
                    csvFile = new CSVWriter(GetName());
                }
            }

            SetState(ExperimentState.WaitingForResumeResponse);

            // All strategies created.

            if (startMode == StartType.Automatic)
            {
                BeginExperiment();
            }
            else if (startMode == StartType.PressAnyKey)
            {
                StartCoroutine(WaitForAnyInput());
            }
        }
Beispiel #13
0
 // ______________________________ Section 1 ________________________________
 //
 //              Abstract methods for child class to implement
 // _________________________________________________________________________
 //
 public abstract void ShowStimuli(TrialConfig config);