public ResponseModel Post([FromBody] WCSPModel value) { this._logger.LogInformation(LoggerEvents.RequestPassed, "Processing request ..."); string directoryPath = @""; string fileFullPath; Dictionary <int, string> dict; (fileFullPath, dict) = CreateWCSPFile(value, directoryPath); string output = Toulbar2Operations.RunToulbar2(fileFullPath, true, _logger); // Creating response: var response = new ResponseModel(); response.RawOutput = output; int maxWeight = value.Functions.Select(x => x.Weight).Sum(); var rgx = new Regex(@"New solution: .*\n (.*)"); var rgx2 = new Regex(@"Optimum: (\d+) in (\d+) .* and (\d+\.?\d*)"); var match = rgx.Match(output); if (match.Success) { string[] variables = match.Groups[1].Value.Split(" "); int counter = 0; foreach (string variable in variables) { int v = int.Parse(variable); response.Variables.Add(new Variable() { Name = dict[counter], Value = v }); counter++; } match = rgx2.Match(output); if (match.Success) { int weight = 0; int.TryParse(match.Groups[1].Value, out weight); response.AccomplishementPercentage = (maxWeight - weight) / (double)maxWeight * 100; int memory = 0; int.TryParse(match.Groups[2].Value, out memory); response.Memory = memory; double time = 0; double.TryParse(match.Groups[3].Value, out time); response.Time = time; } } this._logger.LogInformation(LoggerEvents.ResponseCreated, "Succesfully created response"); return(response); }
private (string, Dictionary <int, string>) CreateWCSPFile(WCSPModel value, string directoryPath) { var sb = new StringBuilder(); sb.AppendLine($"problemloader {value.Variables.Count} {value.Variables.Select(x => x.MaxVal + 1).Max()} {value.Functions.Count} {value.UpperBound}"); var variablesMap = new Dictionary <string, int>(); var reverseVariablesMap = new Dictionary <int, string>(); int counter = 0; foreach (Variable v in value.Variables) { variablesMap.Add(v.Name, counter); reverseVariablesMap.Add(counter, v.Name); sb.Append($"{v.MaxVal + 1} "); counter++; } sb.AppendLine(); foreach (Function f in value.Functions) { String[] args = f.Value.Split(" "); sb.Append($"{args.Count()} "); foreach (string arg in args) { sb.Append($"{variablesMap[arg]} "); } sb.Append($"-1 {f.Name} 0 0"); // TODO sb.AppendLine(); } Random random = new Random(); string fileFullPath = $"{directoryPath}{random.Next(10000)}tmp.wcsp"; System.IO.File.WriteAllText(fileFullPath, sb.ToString()); this._logger.LogInformation(LoggerEvents.ResponseCreated, "Succesfully created response"); return(fileFullPath, reverseVariablesMap); }