public SearchResult GetResults(GetResultsRequest req)
        {
            SearchResult sr;

            if (req.searchToken == null | req.searchToken == "")
            {
                sr            = new SearchResult();
                sr.statusCode = WebServiceBackEnd.SC_INVALID_SEARCH_TOKEN;
                sr.statusMsg  = "Invalid Search Token";
            }

            remoteChannel.Register();

            if (remoteObj == null)
            {
                remoteObj = new WebServiceBackEnd();
            }

            bool isLocalReq = HttpContext.Current.Request.Url.IsLoopback;

            if ((remoteObj == null) || !(remoteObj.allowGlobalAccess || isLocalReq))
            {
                return(restrictedAccessResult());
            }

            sr = remoteObj.getResults(req, isLocalReq);
            return(sr);
        }
Beispiel #2
0
        private async Task <int> LoadModelFromJob(string job)
        {
            var getResultRequest = new GetResultsRequest(job);

            getResultRequest.RunRequestSync();
            var responseHash = getResultRequest.GetResponse().resultAddress;

            while (responseHash == "")
            {
                Debug.Log(string.Format("Could not load job {0}. Trying again in 1 seconds.", job));
                await Task.Delay(1000);

                // run the request again
                getResultRequest = new GetResultsRequest(job);
                getResultRequest.RunRequestSync();
                responseHash = getResultRequest.GetResponse().resultAddress;
            }

            // load the model into memory

            var response        = Ipfs.Get <IpfsJob>(responseHash);
            var modelDefinition = response.Model;
            var model           = this.CreateSequential(modelDefinition);

            return(model.Id);
        }
        private async Task <List <Hotel> > GetAllPaginatedResults(GetResultsRequest request)
        {
            request.Paging          = new Models.Request.Paging();
            request.Paging.PageNo   = 1;
            request.Paging.PageSize = 200; // should come from constants//config

            var hotels      = new List <Hotel>();
            var moreResults = true;

            while (moreResults)
            {
                var requestMessage = GetSearchResultsRequestMessage(request);

                var webResponse = await _webClient.PostAsync(requestMessage);

                var result = GetSearchResultsResponse(webResponse);

                if (result.hotels != null && result.hotels.Count > 0)
                {
                    hotels.AddRange(result.hotels);
                    request.Paging.PageNo++;
                }
                else
                {
                    moreResults = false;
                }
            }
            return(hotels);
        }
Beispiel #4
0
        private async Task <bool> CheckModelFromJob(string job)
        {
            var getResultRequest = new GetResultsRequest(job);

            getResultRequest.RunRequestSync();
            var responseHash = getResultRequest.GetResponse().resultAddress;

            return(responseHash != "");
        }
        public static GetResultsRequest ToGetResultsRequest(SearchEvent eventEntry)
        {
            var request = new GetResultsRequest();

            request.SessionId       = eventEntry.HotelSearchQuery.SessionId;
            request.Currency        = eventEntry.HotelSearchQuery.Currency;
            request.Paging          = new Paging();
            request.Paging.PageNo   = 1;
            request.Paging.PageSize = 100;
            return(request);
        }
        private WebClientRequestMessage GetSearchResultsRequestMessage(GetResultsRequest request)
        {
            var requestMessage = new WebClientRequestMessage();

            requestMessage.Url  = Constants.SearchResultsUrls.StageUrl;
            requestMessage.Data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request, Formatting.Indented,
                                                                                     new JsonSerializerSettings {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            }));
            requestMessage.ContentHeaders.Add(Constants.Headers.ContentType, Constants.HeaderValues.ContentType);
            requestMessage.RequestHeaders.Add(Constants.Headers.OskiTenantId, Constants.HeaderValues.OskiTenantId);
            return(requestMessage);
        }
        public async Task <GetResultsResponse> GetResults(GetResultsRequest request)
        {
            var @params = new Requests.get_results();

            if (null != request)
            {
                CopyPropertiesFrom(@params, request);
            }

            var result = await Omp.QueryAsync <Responses.get_results_response>(this.Address, @params);

            return(result?.Beautify());
        }
        public void updateLeaderboard()
        {
            while (true)
            {
                GetResultsRequest getResultsRequest = new GetResultsRequest();

                ResponseInfo response = app.communicator.SocketSendReceive(JsonSerializer.serializeRequest(getResultsRequest, Constants.GET_RESULTS_REQUEST)).Result;

                GetResultsResponse getResultsResponse = JsonDeserializer.deserializeResponse <GetResultsResponse>(response.buffer);

                switch (getResultsResponse.status)
                {
                case Constants.GET_RESULTS_SUCCESS:
                    this.Dispatcher.Invoke(() =>
                    {
                        UpdateLeaderboardUI(getResultsResponse.results);
                    });
                    break;
                }

                System.Threading.Thread.Sleep(100);
            }
        }
Beispiel #9
0
        protected override Task <ActionResult <ResultContainer> > OnGetResultsAsync(GetResultsRequest request)
        {
            var result = (ActionResult <ResultContainer>) new ObjectResult(new ResultContainer());

            return(Task.FromResult(result));
        }
 public async Task <List <Hotel> > GetSearchResults(GetResultsRequest request)
 {
     return(await GetAllPaginatedResults(request));
 }
        public SearchResult getResults(GetResultsRequest req, bool isLocalReq)
        {
            int    startIndex  = req.startIndex;
            string searchToken = req.searchToken;

            SearchResult sr = new SearchResult();

            sr.numResults = 0;

            if (!sessionTable.ContainsKey(searchToken))
            {
                sr.statusCode = SC_INVALID_SEARCH_TOKEN;
                sr.statusMsg  = "Error: Invalid Search Token";
                Logger.Log.Warn("GetResults: Invalid Search Token received ");
                return(sr);
            }

            ArrayList results = ((SessionData)sessionTable[searchToken]).results;

            if (results == null)
            {
                sr.statusCode = SC_INVALID_SEARCH_TOKEN;
                sr.statusMsg  = "Error: Invalid Search Token";
                Logger.Log.Warn("GetResults: Invalid Search Token received ");
                return(sr);
            }

            lock (results.SyncRoot) {             //Lock results ArrayList to prevent more Hits getting added till we've processed doQuery
                int i = 0;

                if (startIndex < results.Count)
                {
                    sr.numResults = (results.Count < startIndex + MAX_RESULTS_PER_CALL) ? (results.Count - startIndex): MAX_RESULTS_PER_CALL;
                }

                sr.hitResults = new HitResult[sr.numResults];

                string hitUri;
                for (int k = startIndex; (i < sr.numResults) && (k < results.Count); k++)
                {
                    Hit h = (Hit)results[k];

                    sr.hitResults[i] = new HitResult();

// GetResults will NOT return Snippets by default. Client must make explicit GetSnippets request to get snippets for these hits.
// Not initializing sr.hitResults[i].snippet implies there is no <snippets> element in HitResult XML response.

                    hitUri = h.UriAsString;
                    if (isLocalReq || hitUri.StartsWith(NetworkedBeagle.BeagleNetPrefix))
                    {
                        sr.hitResults[i].uri = hitUri;
                    }
                    else
                    {
                        sr.hitResults[i].uri = AccessFilter.TranslateHit(h);
                    }

                    sr.hitResults[i].resourceType = h.Type;
                    sr.hitResults[i].mimeType     = h.MimeType;
                    sr.hitResults[i].source       = h.Source;
                    sr.hitResults[i].score        = h.Score;

                    int plen = h.Properties.Count;
                    sr.hitResults[i].properties = new HitProperty[plen];
                    for (int j = 0; j < plen; j++)
                    {
                        Property p = (Property)h.Properties[j];
                        sr.hitResults[i].properties[j]            = new HitProperty();
                        sr.hitResults[i].properties[j].PKey       = p.Key;
                        sr.hitResults[i].properties[j].PVal       = p.Value;
                        sr.hitResults[i].properties[j].IsMutable  = p.IsMutable;
                        sr.hitResults[i].properties[j].IsSearched = p.IsSearched;
                    }

                    sr.hitResults[i].hashCode = h.GetHashCode();

                    i++;
                }
            }             //end lock

            sr.totalResults = results.Count;

            sr.firstResultIndex = startIndex;
            sr.searchToken      = "";

            if (sr.totalResults > 0)
            {
                sr.searchToken = searchToken;
            }

            sr.statusCode = SC_QUERY_SUCCESS;
            sr.statusMsg  = "Success";
            //Console.WriteLine("WebServiceQuery: Total Results = "  + sr.totalResults);
            return(sr);
        }
        public string processMessage(string json_message, MonoBehaviour owner)
        {
            //Debug.LogFormat("<color=green>SyftController.processMessage {0}</color>", json_message);

            Command msgObj = JsonUtility.FromJson <Command> (json_message);

            try
            {
                switch (msgObj.objectType)
                {
                case "Optimizer":
                {
                    if (msgObj.functionCall == "create")
                    {
                        string optimizer_type = msgObj.tensorIndexParams[0];

                        // Extract parameters
                        List <int> p = new List <int>();
                        for (int i = 1; i < msgObj.tensorIndexParams.Length; i++)
                        {
                            p.Add(int.Parse(msgObj.tensorIndexParams[i]));
                        }
                        List <float> hp = new List <float>();
                        for (int i = 0; i < msgObj.hyperParams.Length; i++)
                        {
                            hp.Add(float.Parse(msgObj.hyperParams[i]));
                        }

                        Optimizer optim = null;

                        if (optimizer_type == "sgd")
                        {
                            optim = new SGD(this, p, hp[0], hp[1], hp[2]);
                        }
                        else if (optimizer_type == "rmsprop")
                        {
                            optim = new RMSProp(this, p, hp[0], hp[1], hp[2], hp[3]);
                        }
                        else if (optimizer_type == "adam")
                        {
                            optim = new Adam(this, p, hp[0], hp[1], hp[2], hp[3], hp[4]);
                        }

                        return(optim.Id.ToString());
                    }
                    else
                    {
                        Optimizer optim = this.getOptimizer(msgObj.objectIndex);
                        return(optim.ProcessMessage(msgObj, this));
                    }
                }

                case "FloatTensor":
                {
                    if (msgObj.objectIndex == 0 && msgObj.functionCall == "create")
                    {
                        FloatTensor tensor = floatTensorFactory.Create(_shape: msgObj.shape, _data: msgObj.data, _shader: this.Shader);
                        return(tensor.Id.ToString());
                    }
                    else
                    {
                        FloatTensor tensor = floatTensorFactory.Get(msgObj.objectIndex);
                        // Process message's function
                        return(tensor.ProcessMessage(msgObj, this));
                    }
                }

                case "IntTensor":
                {
                    if (msgObj.objectIndex == 0 && msgObj.functionCall == "create")
                    {
                        int[] data = new int[msgObj.data.Length];
                        for (int i = 0; i < msgObj.data.Length; i++)
                        {
                            data[i] = (int)msgObj.data[i];
                        }
                        IntTensor tensor = intTensorFactory.Create(_shape: msgObj.shape, _data: data, _shader: this.Shader);
                        return(tensor.Id.ToString());
                    }
                    else
                    {
                        IntTensor tensor = intTensorFactory.Get(msgObj.objectIndex);
                        // Process message's function
                        return(tensor.ProcessMessage(msgObj, this));
                    }
                }

                case "agent":
                {
                    if (msgObj.functionCall == "create")
                    {
                        Layer     model     = (Layer)getModel(int.Parse(msgObj.tensorIndexParams[0]));
                        Optimizer optimizer = optimizers[int.Parse(msgObj.tensorIndexParams[1])];
                        return(new Syft.NN.RL.Agent(this, model, optimizer).Id.ToString());
                    }

                    //Debug.Log("Getting Model:" + msgObj.objectIndex);
                    Syft.NN.RL.Agent agent = this.getAgent(msgObj.objectIndex);
                    return(agent.ProcessMessageLocal(msgObj, this));
                }

                case "model":
                {
                    if (msgObj.functionCall == "create")
                    {
                        string model_type = msgObj.tensorIndexParams[0];

                        Debug.LogFormat("<color=magenta>createModel:</color> {0}", model_type);

                        if (model_type == "linear")
                        {
                            return(this.BuildLinear(msgObj.tensorIndexParams).Id.ToString());
                        }
                        else if (model_type == "relu")
                        {
                            return(this.BuildReLU().Id.ToString());
                        }
                        else if (model_type == "log")
                        {
                            return(this.BuildLog().Id.ToString());
                        }
                        else if (model_type == "dropout")
                        {
                            return(this.BuildDropout(msgObj.tensorIndexParams).Id.ToString());
                        }
                        else if (model_type == "sigmoid")
                        {
                            return(this.BuildSigmoid().Id.ToString());
                        }
                        else if (model_type == "sequential")
                        {
                            return(this.BuildSequential().Id.ToString());
                        }
                        else if (model_type == "softmax")
                        {
                            return(this.BuildSoftmax(msgObj.tensorIndexParams).Id.ToString());
                        }
                        else if (model_type == "logsoftmax")
                        {
                            return(this.BuildLogSoftmax(msgObj.tensorIndexParams).Id.ToString());
                        }
                        else if (model_type == "tanh")
                        {
                            return(new Tanh(this).Id.ToString());
                        }
                        else if (model_type == "crossentropyloss")
                        {
                            return(new CrossEntropyLoss(this, int.Parse(msgObj.tensorIndexParams[1])).Id.ToString());
                        }
                        else if (model_type == "categorical_crossentropy")
                        {
                            return(new CategoricalCrossEntropyLoss(this).Id.ToString());
                        }
                        else if (model_type == "nllloss")
                        {
                            return(new NLLLoss(this).Id.ToString());
                        }
                        else if (model_type == "mseloss")
                        {
                            return(new MSELoss(this).Id.ToString());
                        }
                        else if (model_type == "embedding")
                        {
                            return(new Embedding(this, int.Parse(msgObj.tensorIndexParams[1]), int.Parse(msgObj.tensorIndexParams[2])).Id.ToString());
                        }
                        else
                        {
                            Debug.LogFormat("<color=red>Model Type Not Found:</color> {0}", model_type);
                        }
                    }
                    else
                    {
                        //Debug.Log("Getting Model:" + msgObj.objectIndex);
                        Model model = this.getModel(msgObj.objectIndex);
                        return(model.ProcessMessage(msgObj, this));
                    }
                    return("Unity Error: SyftController.processMessage: Command not found:" + msgObj.objectType + ":" + msgObj.functionCall);
                }

                case "controller":
                {
                    if (msgObj.functionCall == "num_tensors")
                    {
                        return(floatTensorFactory.Count() + "");
                    }
                    else if (msgObj.functionCall == "num_models")
                    {
                        return(models.Count + "");
                    }
                    else if (msgObj.functionCall == "new_tensors_allowed")
                    {
                        Debug.LogFormat("New Tensors Allowed:{0}", msgObj.tensorIndexParams[0]);
                        if (msgObj.tensorIndexParams[0] == "True")
                        {
                            allow_new_tensors = true;
                        }
                        else if (msgObj.tensorIndexParams[0] == "False")
                        {
                            allow_new_tensors = false;
                        }
                        else
                        {
                            throw new Exception("Invalid parameter for new_tensors_allowed. Did you mean true or false?");
                        }

                        return(allow_new_tensors + "");
                    }
                    else if (msgObj.functionCall == "load_floattensor")
                    {
                        FloatTensor tensor = floatTensorFactory.Create(filepath: msgObj.tensorIndexParams[0], _shader: this.Shader);
                        return(tensor.Id.ToString());
                    }
                    else if (msgObj.functionCall == "set_seed")
                    {
                        Random.InitState(int.Parse(msgObj.tensorIndexParams[0]));
                        return("Random seed set!");
                    }
                    else if (msgObj.functionCall == "concatenate")
                    {
                        List <int> tensor_ids = new List <int>();
                        for (int i = 1; i < msgObj.tensorIndexParams.Length; i++)
                        {
                            tensor_ids.Add(int.Parse(msgObj.tensorIndexParams[i]));
                        }
                        FloatTensor result = Functional.Concatenate(floatTensorFactory, tensor_ids, int.Parse(msgObj.tensorIndexParams[0]));
                        return(result.Id.ToString());
                    }
                    else if (msgObj.functionCall == "ones")
                    {
                        int[] dims = new int[msgObj.tensorIndexParams.Length];
                        for (int i = 0; i < msgObj.tensorIndexParams.Length; i++)
                        {
                            dims[i] = int.Parse(msgObj.tensorIndexParams[i]);
                        }
                        FloatTensor result = Functional.Ones(floatTensorFactory, dims);
                        return(result.Id.ToString());
                    }
                    else if (msgObj.functionCall == "randn")
                    {
                        int[] dims = new int[msgObj.tensorIndexParams.Length];
                        for (int i = 0; i < msgObj.tensorIndexParams.Length; i++)
                        {
                            dims[i] = int.Parse(msgObj.tensorIndexParams[i]);
                        }
                        FloatTensor result = Functional.Randn(floatTensorFactory, dims);
                        return(result.Id.ToString());
                    }
                    else if (msgObj.functionCall == "random")
                    {
                        int[] dims = new int[msgObj.tensorIndexParams.Length];
                        for (int i = 0; i < msgObj.tensorIndexParams.Length; i++)
                        {
                            dims[i] = int.Parse(msgObj.tensorIndexParams[i]);
                        }
                        FloatTensor result = Functional.Random(floatTensorFactory, dims);
                        return(result.Id.ToString());
                    }
                    else if (msgObj.functionCall == "zeros")
                    {
                        int[] dims = new int[msgObj.tensorIndexParams.Length];
                        for (int i = 0; i < msgObj.tensorIndexParams.Length; i++)
                        {
                            dims[i] = int.Parse(msgObj.tensorIndexParams[i]);
                        }
                        FloatTensor result = Functional.Zeros(floatTensorFactory, dims);
                        return(result.Id.ToString());
                    }
                    else if (msgObj.functionCall == "model_from_json")
                    {
                        Debug.Log("Loading Model from JSON:");
                        var json_str = msgObj.tensorIndexParams[0];
                        var config   = JObject.Parse(json_str);

                        Sequential model;

                        if ((string)config["class_name"] == "Sequential")
                        {
                            model = this.BuildSequential();
                        }
                        else
                        {
                            return("Unity Error: SyftController.processMessage: while Loading model, Class :" + config["class_name"] + " is not implemented");
                        }

                        for (int i = 0; i < config["config"].ToList().Count; i++)
                        {
                            var layer_desc        = config["config"][i];
                            var layer_config_desc = layer_desc["config"];

                            if ((string)layer_desc["class_name"] == "Linear")
                            {
                                int previous_output_dim;

                                if (i == 0)
                                {
                                    previous_output_dim = (int)layer_config_desc["batch_input_shape"][layer_config_desc["batch_input_shape"].ToList().Count - 1];
                                }
                                else
                                {
                                    previous_output_dim = (int)layer_config_desc["units"];
                                }

                                string[] parameters = new string[] { "linear", previous_output_dim.ToString(), layer_config_desc["units"].ToString(), "Xavier" };
                                Layer    layer      = this.BuildLinear(parameters);
                                model.AddLayer(layer);

                                string activation_name = layer_config_desc["activation"].ToString();

                                if (activation_name != "linear")
                                {
                                    Layer activation;
                                    if (activation_name == "softmax")
                                    {
                                        parameters = new string[] { activation_name, "1" };
                                        activation = this.BuildSoftmax(parameters);
                                    }
                                    else if (activation_name == "relu")
                                    {
                                        activation = this.BuildReLU();
                                    }
                                    else
                                    {
                                        return("Unity Error: SyftController.processMessage: while Loading activations, Activation :" + activation_name + " is not implemented");
                                    }
                                    model.AddLayer(activation);
                                }
                            }
                            else
                            {
                                return("Unity Error: SyftController.processMessage: while Loading layers, Layer :" + layer_desc["class_name"] + " is not implemented");
                            }
                        }

                        return(model.Id.ToString());
                    }
                    return("Unity Error: SyftController.processMessage: Command not found:" + msgObj.objectType + ":" + msgObj.functionCall);
                }

                case "Grid":
                    if (msgObj.functionCall == "learn")
                    {
                        var inputId  = int.Parse(msgObj.tensorIndexParams[0]);
                        var targetId = int.Parse(msgObj.tensorIndexParams[1]);

                        return(this.grid.Run(inputId, targetId, msgObj.configurations, owner));
                    }

                    if (msgObj.functionCall == "getResults")
                    {
                        // TODO -- This will be converted to poll blockchain
                        //         It is written to poll IPFS right now so it
                        //         appears to be working client side.

                        var experiment = Ipfs.Get <IpfsExperiment>(msgObj.experimentId);
                        var results    = experiment.jobs.Select((job) =>
                        {
                            var getResultRequest = new GetResultsRequest(job);
                            getResultRequest.RunRequestSync();
                            var responseHash = getResultRequest.GetResponse().resultAddress;

                            // load the model into memory
                            var modelDefinition = Ipfs.Get <IpfsJob>(responseHash).Model;
                            var model           = this.grid.CreateSequential(modelDefinition);

                            return(model.Id);
                        });

                        var modelIdsString = JsonConvert.SerializeObject(results.ToArray());
                        return(modelIdsString);
                    }

                    break;

                default:
                    break;
                }
            }
            catch (Exception e)
            {
                Debug.LogFormat("<color=red>{0}</color>", e.ToString());
                return("Unity Error: " + e.ToString());
            }

            // If not executing createTensor or tensor function, return default error.
            return("Unity Error: SyftController.processMessage: Command not found:" + msgObj.objectType + ":" + msgObj.functionCall);
        }
        /// <inheritdoc />
        /// <summary>
        /// Returns the most recent score for each person with scores.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        protected override async Task <ActionResult <ResultContainer> > OnGetResultsAsync(GetResultsRequest request)
        {
            if (!int.TryParse(request.ContextId, out var contextId))
            {
                var name = $"{nameof(request)}.{nameof(request.ContextId)}";
                ModelState.AddModelError(name, $"The {name} field cannot be converted into a course id.");
            }

            if (!int.TryParse(request.LineItemId, out var lineItemId))
            {
                var name = $"{nameof(request)}.{nameof(request.LineItemId)}";
                ModelState.AddModelError(name, $"The {name} field cannot be converted into a gradebook column id.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(new ValidationProblemDetails(ModelState)));
            }

            if (!await _courseValidator.UserHasAccess(contextId))
            {
                return(Unauthorized(new ProblemDetails
                {
                    Title = "Not authorized",
                    Detail = "User not authorized to access the requested course."
                }));
            }

            var course = await _context.GetCourseAsync(contextId);

            if (course == null)
            {
                return(NotFound(new ProblemDetails
                {
                    Title = ReasonPhrases.GetReasonPhrase(StatusCodes.Status404NotFound),
                    Detail = "Course not found"
                }));
            }

            if (course.GradebookColumns.All(c => c.Id != lineItemId))
            {
                return(NotFound(new ProblemDetails
                {
                    Title = ReasonPhrases.GetReasonPhrase(StatusCodes.Status404NotFound),
                    Detail = "Gradebook column not found"
                }));
            }

            var gradebookColumn = await _context.GetGradebookColumnAsync(lineItemId);

            var results = gradebookColumn.Scores
                          .OrderBy(s => s.TimeStamp)
                          .GroupBy(s => s.PersonId)
                          .Select(g => new Result
            {
                Id      = Request.GetDisplayUrl().EnsureTrailingSlash() + g.Key,
                Comment = $"Last score of {g.Count()} attempt/s."
                          + $"<p><div>First Score: {g.First().ScoreGiven:N1}</div>"
                          + $"<div>Highest Score: {g.Max(x => x.ScoreGiven):N1}</div>"
                          + $"<div>Lowest Score: {g.Min(x => x.ScoreGiven):N1}</div></p>",
                ResultMaximum = g.Max(x => x.ScoreMaximum),
                ResultScore   = g.Last().ScoreGiven,
                ScoreOf       = Url.Link(Constants.ServiceEndpoints.Ags.LineItemService,
                                         new { contextId = request.ContextId, lineItemId = request.LineItemId }),
                UserId = g.Key.ToString()
            })
                          .ToList();

            if (request.UserId.IsPresent())
            {
                results = results.Where(r => r.UserId == request.UserId).ToList();
            }

            return(new ResultContainer(results));
        }