Ejemplo n.º 1
0
 /// <summary>Solve a QUBO problem</summary>
 /// <returns>QUBO solved.</returns>
 /// <exception cref="ApiException">A server side error occurred.</exception>
 public System.Threading.Tasks.Task <QuboAPI> QuboAsync(QuboAPI body)
 {
     return(QuboAsync(body, System.Threading.CancellationToken.None));
 }
Ejemplo n.º 2
0
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <summary>Solve a QUBO problem</summary>
        /// <returns>QUBO solved.</returns>
        /// <exception cref="ApiException">A server side error occurred.</exception>
        public async System.Threading.Tasks.Task <QuboAPI> QuboAsync(QuboAPI body, System.Threading.CancellationToken cancellationToken)
        {
            var urlBuilder_ = new System.Text.StringBuilder();

            urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/api/Qubo");

            var client_ = _httpClient;

            try
            {
                using (var request_ = new System.Net.Http.HttpRequestMessage())
                {
                    var content_ = new System.Net.Http.StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(body, _settings.Value));
                    content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
                    request_.Content             = content_;
                    request_.Method = new System.Net.Http.HttpMethod("POST");
                    request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("text/plain"));

                    PrepareRequest(client_, request_, urlBuilder_);
                    var url_ = urlBuilder_.ToString();
                    request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);
                    PrepareRequest(client_, request_, url_);

                    var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

                    try
                    {
                        var headers_ = System.Linq.Enumerable.ToDictionary(response_.Headers, h_ => h_.Key, h_ => h_.Value);
                        if (response_.Content != null && response_.Content.Headers != null)
                        {
                            foreach (var item_ in response_.Content.Headers)
                            {
                                headers_[item_.Key] = item_.Value;
                            }
                        }

                        ProcessResponse(client_, response_);

                        var status_ = ((int)response_.StatusCode).ToString();
                        if (status_ == "200")
                        {
                            var objectResponse_ = await ReadObjectResponseAsync <QuboAPI>(response_, headers_).ConfigureAwait(false);

                            return(objectResponse_.Object);
                        }
                        else
                        if (status_ == "500")
                        {
                            string responseText_ = (response_.Content == null) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);

                            throw new ApiException("Internal error, please contact support.", (int)response_.StatusCode, responseText_, headers_, null);
                        }
                        else
                        if (status_ != "200" && status_ != "204")
                        {
                            var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);

                            throw new ApiException("The HTTP status code of the response was not expected (" + (int)response_.StatusCode + ").", (int)response_.StatusCode, responseData_, headers_, null);
                        }

                        return(default(QuboAPI));
                    }
                    finally
                    {
                        if (response_ != null)
                        {
                            response_.Dispose();
                        }
                    }
                }
            }
            finally
            {
            }
        }
Ejemplo n.º 3
0
        //solves a qubo problem by posting it to a Meta-Analytics AWS marketplace solver
        static void Main(string[] args)
        {
            try
            {
                if (args.Length < 4)
                {
                    //to solve QUBO model test.txt as with 20 seconds timout as a minimization problem
                    //C:/tmp/test.txt http://<load balancer>/api/QUBO 20 min
                    System.Console.WriteLine("usage QuboClient path_to_problem.txt solverUrl timeoutseconds min_or_max");
                }

                string sFilePath         = args[0];
                string sSolverUrl        = args[1];
                string sTimeOutInSeconds = args[2];
                string sMinMax           = args[3];

                int           nVariables = 0;
                List <Points> inputData  = QuboFileUtils.readQuboInputFromFile(sFilePath, out nVariables);
                QuboAPI       quboApi    = new QuboAPI();
                quboApi.Inputs = inputData;
                quboApi.MinMax = 1;                //maximize;
                if (sMinMax.Equals("min"))
                {
                    quboApi.MinMax = 0;
                }
                quboApi.Rows    = nVariables;
                quboApi.Timeout = Int32.Parse(sTimeOutInSeconds);

                string sVal = quboApi.ToString();

                HttpClient        httpClient = new HttpClient();
                QuboClient.Client quboClient = new QuboClient.Client(sSolverUrl, httpClient);
                System.Threading.Tasks.Task <QuboAPI> solverTask = quboClient.QuboAsync(quboApi);

                //Give the solver an additional 60 seconds to send the model
                //over the network before timing out

                int iTimeOutInMilliseconds = (60 + quboApi.Timeout) * 1000;
                solverTask.Wait();                // iTimeOutInMilliseconds);

                if (solverTask.IsCompletedSuccessfully)
                {
                    double solutionValue = solverTask.Result.SolvedValue;
                    System.Console.WriteLine("solution value : " + solutionValue.ToString());

                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    foreach (int variableResult in solverTask.Result.SolvedResult)
                    {
                        sb.Append(variableResult.ToString());
                    }
                    System.Console.WriteLine(sb.ToString());
                }
                else
                {
                    System.Console.WriteLine("Task Failed");
                }
            }
            catch (Exception error)
            {
                System.Console.WriteLine(error.Message);
            }
        }