public DataConverter(BalanceInput balanceInput) { FlowDescription = new List <FlowDescription>(); foreach (Flow flow in balanceInput.Flows) { if (flow.Flows != null) { delta = flow.Delta_error; foreach (FlowDescription flowDescription in flow.Flows) { FlowDescription.Add(new Models.FlowDescription { Id = flowDescription.Id, Destination = flowDescription.Destination, Source = flowDescription.Source, NonMeasured = flowDescription.NonMeasured, Value = flowDescription.Value, Tolerance = flowDescription.Tolerance, LowerBound = flowDescription.LowerBound, UpperBound = flowDescription.UpperBound }); } } else { ok = false; message = "Не найдено ни одного объекта"; } } }
public ActionResult Payment([FromBody] BalanceInput input) { if (!ModelState.IsValid) { return(BadRequest()); } var user = accountService.GetCurrentUserForContext(HttpContext); if (user == null || user.Id != input.UserId) { // User should only be able to alter its own balance return(Unauthorized()); } var transaction = new BalanceTransaction { UserId = user.Id, Amount = input.Amount }; dbcontext.BalanceTransaction.Add(transaction); user.Balance += transaction.Amount; dbcontext.Update(user); dbcontext.SaveChanges(); return(Ok()); }
public BalanceOutput Calculate(BalanceInput balanceInput) { DataConverter dataConverter = new DataConverter(balanceInput); BalanceOutput balanceOutput = dataConverter.Calculate(); return(balanceOutput); }
[TestMethod]//пустой запрос public void TestMethod6() { BalanceInput balanceInput = new BalanceInput(); balanceInput.Flows = new List <Flow>(); balanceInput.Flows.Add(new Flow { Flows = null, Delta_error = 0.001 }); var controller = new BalanceController(); var result = controller.Post(balanceInput) as BalanceOutput; Assert.AreEqual(false, result.IsBalanced); }
public IHttpActionResult UpdateBalanceData([FromBody] BalanceInput input) { if (!ModelState.IsValid) { return(null); } using (var unitofWork = _unitOfWorkFactory.Create()) { unitofWork.BalanceRepository.Update((BalanceObj)input); } return (Redirect( new Uri(ControllerContext.Request.RequestUri.Host + ":" + ControllerContext.Request.RequestUri.Port + "/Balance/RouteBalances"))); }
public async Task <BalanceOutput> GetBalanceGurobi(BalanceInput input) { return(await Task.Run(() => { try { // Решение задачи Solver solver = new Solver(input); solver.BalanceGurobi(); return solver.balanceOutput; } catch (Exception e) { return new BalanceOutput { Status = e.Message, }; } })); }
[TestMethod]//несводимый public void TestMethod3() { List <FlowDescription> flowDescription = new List <FlowDescription>(); flowDescription.Add(new FlowDescription { Id = "1", Destination = "1", Source = null, NonMeasured = false, Value = 10.005, Tolerance = 0.2, LowerBound = 50, UpperBound = 60 }); flowDescription.Add(new FlowDescription { Id = "2", Destination = "", Source = "1", NonMeasured = false, Value = 3.033, Tolerance = 0.121, LowerBound = 0, UpperBound = 10 }); flowDescription.Add(new FlowDescription { Id = "3", Destination = "2", Source = "1", NonMeasured = false, Value = 6.831, Tolerance = 0.683, LowerBound = 0, UpperBound = 10 }); flowDescription.Add(new FlowDescription { Id = "4", Destination = "", Source = "2", NonMeasured = false, Value = 1.985, Tolerance = 0.04, LowerBound = 0, UpperBound = 10 }); flowDescription.Add(new FlowDescription { Id = "5", Destination = "3", Source = "2", NonMeasured = false, Value = 5.093, Tolerance = 0.102, LowerBound = 0, UpperBound = 10 }); flowDescription.Add(new FlowDescription { Id = "6", Destination = null, Source = "3", NonMeasured = false, Value = 4.057, Tolerance = 0.081, LowerBound = 0, UpperBound = 10 }); flowDescription.Add(new FlowDescription { Id = "7", Destination = null, Source = "3", NonMeasured = false, Value = 0.991, Tolerance = 0.02, LowerBound = 0, UpperBound = 10 }); flowDescription.Add(new FlowDescription { Id = "8", Destination = null, Source = "3", NonMeasured = false, Value = 6.667, Tolerance = 0.667, LowerBound = 0, UpperBound = 10 }); BalanceInput balanceInput = new BalanceInput(); balanceInput.Flows = new List <Flow>(); balanceInput.Flows.Add(new Flow { Flows = flowDescription, Delta_error = 0.001 }); var controller = new BalanceController(); var result = controller.Post(balanceInput) as BalanceOutput; Assert.AreEqual(false, result.IsBalanced); }
public async Task <GtRes> Gtest(BalanceInput input) { return(await Task.Run(() => { try { Solver solver = new Solver(input); solver.GlobalTest(); var gt = solver.GTR; return new GtRes { Status = "Success", Result = gt }; } catch (Exception e) { return new GtRes { Status = "Error" }; } })); }
public double[,] getIncidenceMatrix(BalanceInput balanceInput) { Graph graph = new Graph(balanceInput); return(graph.getMatrix()); }
public Graph(BalanceInput balanceInput) { vertexList = new List <Vertex>(); // Определение количества потоков int countOfThreads = balanceInput.BalanceInputVariables.Count(); // Создание вершин графа for (int i = 0; i < countOfThreads; i++) { string sourceId = balanceInput.BalanceInputVariables[i].sourceId; string destinationId = balanceInput.BalanceInputVariables[i].destinationId; if (sourceId != "null") { bool isExisted = false; if (vertexList.Count == 0) { vertexList.Add(new Vertex(sourceId)); isExisted = true; } foreach (Vertex vertex in vertexList) { if (sourceId.Equals(vertex.Id)) { isExisted = true; break; } } if (!isExisted) { vertexList.Add(new Vertex(sourceId)); } } else if (destinationId != "null") { bool isExisted = false; if (vertexList.Count == 0) { vertexList.Add(new Vertex(destinationId)); isExisted = true; } foreach (Vertex vertex in vertexList) { if (destinationId.Equals(vertex.Id)) { isExisted = true; break; } } if (!isExisted && vertexList.Count != 0) { vertexList.Add(new Vertex(destinationId)); } } } // Определение количества вершин int countOfVertexes = vertexList.Count(); // Инициализация матрицы инцидентности matrix = new double[countOfVertexes, countOfThreads]; for (int thread = 0; thread < countOfThreads; thread++) { string sourceId = balanceInput.BalanceInputVariables[thread].sourceId; string destinationId = balanceInput.BalanceInputVariables[thread].destinationId; for (int vertex = 0; vertex < vertexList.Count(); vertex++) { string vertexId = vertexList[vertex].Id; if (destinationId != null && destinationId.Equals(vertexId)) { matrix[vertex, thread] = 1; } if (sourceId != null && sourceId.Equals(vertexId)) { matrix[vertex, thread] = -1; } } } }
public async Task <GlrRes> GlrTest(BalanceInput input) { return(await Task.Run(() => { try { Solver solver = new Solver(input); var(root, flows) = solver.StartGlr(); var nodes = root.Where(x => x.IsLeaf); var results = new List <Glr>(); foreach (var node in nodes) { var flowerrors = new List <Flow>(); var flowCorrections = new List <InputVariables>(); foreach (var flow in node.Item.Flows) { var(i, j, k, n) = flow; var newFlow = new Flow($"Соединяет узлы: {i} -> {j}"); var existingFlowIdx = flows.FindIndex(x => x.Input == i && x.Output == j); if (existingFlowIdx != -1) { var(_, _, existingFlow, _) = flows[existingFlowIdx]; newFlow.Id = input.BalanceInputVariables[existingFlow].id; newFlow.Name = "Поток: " + input.BalanceInputVariables[existingFlow].name; var variable = new InputVariables { id = Guid.NewGuid().ToString(), sourceId = input.BalanceInputVariables[i].id, destinationId = input.BalanceInputVariables[j].id, name = input.BalanceInputVariables[existingFlow].name + " (Доп. поток)", measured = input.BalanceInputVariables[existingFlow].measured, correction = input.BalanceInputVariables[existingFlow].measured + solver.corr[existingFlow], metrologicLowerBound = input.BalanceInputVariables[existingFlow].metrologicLowerBound, metrologicUpperBound = input.BalanceInputVariables[existingFlow].metrologicUpperBound, technologicLowerBound = input.BalanceInputVariables[existingFlow].technologicLowerBound, technologicUpperBound = input.BalanceInputVariables[existingFlow].technologicUpperBound, tolerance = input.BalanceInputVariables[existingFlow].tolerance, isMeasured = true, }; flowCorrections.Add(variable); } flowerrors.Add(newFlow); } results.Add(new Glr { FlowErrors = flowerrors, FlowCorrections = flowCorrections, GlobalTestValue = node.Item.GlobalTestValue }); } return new GlrRes { Status = "Success", Time = solver.GlrTime, Data = results }; } catch (Exception e) { return new GlrRes { Status = "Error" }; } })); }
[TestMethod]//больше потоков public void TestMethod5() { List <FlowDescription> flowDescription = new List <FlowDescription>(); flowDescription.Add(new FlowDescription { Id = "1", Destination = "", Source = "1", NonMeasured = false, Value = 5.035, Tolerance = 0.11, LowerBound = 5.03, UpperBound = 5.04 }); flowDescription.Add(new FlowDescription { Id = "2", Destination = "", Source = "1", NonMeasured = false, Value = 3.002, Tolerance = 0.121, LowerBound = 0, UpperBound = 1000 }); flowDescription.Add(new FlowDescription { Id = "3", Destination = "1", Source = "2", NonMeasured = false, Value = 4.523, Tolerance = 0.2, LowerBound = 0, UpperBound = 1000 }); flowDescription.Add(new FlowDescription { Id = "4", Destination = "1", Source = "3", NonMeasured = false, Value = 3.493, Tolerance = 0.08, LowerBound = 0, UpperBound = 1000 }); flowDescription.Add(new FlowDescription { Id = "5", Destination = "", Source = "2", NonMeasured = false, Value = 4.028, Tolerance = 0.092, LowerBound = 0, UpperBound = 1000 }); flowDescription.Add(new FlowDescription { Id = "6", Destination = "2", Source = "4", NonMeasured = false, Value = 8.544, Tolerance = 0.081, LowerBound = 0, UpperBound = 1000 }); flowDescription.Add(new FlowDescription { Id = "7", Destination = "3", Source = "5", NonMeasured = false, Value = 3.485, Tolerance = 0.02, LowerBound = 0, UpperBound = 1000 }); flowDescription.Add(new FlowDescription { Id = "8", Destination = "3", Source = "", NonMeasured = false, Value = 0.02, Tolerance = 0.005, LowerBound = 0, UpperBound = 1000 }); flowDescription.Add(new FlowDescription { Id = "9", Destination = "5", Source = null, NonMeasured = false, Value = 2.081, Tolerance = 0.12, LowerBound = 0, UpperBound = 1000 }); flowDescription.Add(new FlowDescription { Id = "10", Destination = "4", Source = "6", NonMeasured = false, Value = 2.573, Tolerance = 0.15, LowerBound = 0, UpperBound = 1000 }); flowDescription.Add(new FlowDescription { Id = "11", Destination = "3", Source = "7", NonMeasured = false, Value = 6.028, Tolerance = 0.08, LowerBound = 0, UpperBound = 1000 }); flowDescription.Add(new FlowDescription { Id = "12", Destination = "7", Source = "", NonMeasured = false, Value = 1.014, Tolerance = 0.05, LowerBound = 0, UpperBound = 1000 }); flowDescription.Add(new FlowDescription { Id = "13", Destination = "5", Source = "8", NonMeasured = false, Value = 1.404, Tolerance = 0.081, LowerBound = 0, UpperBound = 1000 }); flowDescription.Add(new FlowDescription { Id = "14", Destination = "6", Source = "8", NonMeasured = false, Value = 2.565, Tolerance = 0.125, LowerBound = 0, UpperBound = 1000 }); flowDescription.Add(new FlowDescription { Id = "15", Destination = "7", Source = "9", NonMeasured = false, Value = 5.012, Tolerance = 0.08, LowerBound = 0, UpperBound = 1000 }); flowDescription.Add(new FlowDescription { Id = "16", Destination = "8", Source = null, NonMeasured = false, Value = 3.04, Tolerance = 0.121, LowerBound = 0, UpperBound = 1000 }); flowDescription.Add(new FlowDescription { Id = "17", Destination = "8", Source = "9", NonMeasured = false, Value = 0.93, Tolerance = 0.071, LowerBound = 0, UpperBound = 1000 }); flowDescription.Add(new FlowDescription { Id = "18", Destination = "9", Source = null, NonMeasured = false, Value = 5.937, Tolerance = 0.125, LowerBound = 0, UpperBound = 1000 }); BalanceInput balanceInput = new BalanceInput(); balanceInput.Flows = new List <Flow>(); balanceInput.Flows.Add(new Flow { Flows = flowDescription, Delta_error = 0.001 }); var controller = new BalanceController(); var result = controller.Post(balanceInput) as BalanceOutput; Assert.AreEqual(true, result.IsBalanced); }
public Solver(BalanceInput balanceInput) { this.inputData = balanceInput; countOfThreads = balanceInput.BalanceInputVariables.Count();// Инициализация количества потоков Graph graph = new Graph(balanceInput); incidenceMatrix = SparseMatrix.OfArray(graph.getIncidenceMatrix(balanceInput));// Матрица инцидентности // Инициализация вектора измеренных значений ( x0 ) measuredValues = new SparseVector(countOfThreads); double[] tol = new double[countOfThreads]; // Инициализация вектора верхних ограничений вектора x metrologicRangeUpperBound = new DenseVector(countOfThreads); technologicRangeUpperBound = new DenseVector(countOfThreads); // Инициализация вектора нижних ограничений вектора x metrologicRangeLowerBound = new DenseVector(countOfThreads); technologicRangeLowerBound = new DenseVector(countOfThreads); // Инициализация вектора абсолютных погрешностей absTolerance = new DenseVector(countOfThreads); //double[] tol = new double[countOfThreads]; double[] measIndicator = new double[countOfThreads]; for (int i = 0; i < countOfThreads; i++) { InputVariables variables = balanceInput.BalanceInputVariables[i]; measuredValues[i] = variables.measured; // Определение матрицы измеряемости if (variables.isMeasured) { measIndicator[i] = 1.0; } else { measIndicator[i] = 0.0; } // Определение матрицы метрологической погрешности if (!variables.isMeasured) { tol[i] = 1.0; } else { double tolerance = 1.0 / Math.Pow(variables.tolerance, 2); if (Double.IsInfinity(tolerance)) { tolerance = 1.0; } if (Double.IsNaN(tolerance)) { throw new Exception("Exception: NaN Value"); } tol[i] = tolerance; } // Определение вектора верхних ограничений вектора x metrologicRangeUpperBound[i] = variables.metrologicUpperBound; technologicRangeUpperBound[i] = variables.technologicUpperBound; // Определение вектора нижних ограничений вектора x metrologicRangeLowerBound[i] = variables.metrologicLowerBound; technologicRangeLowerBound[i] = variables.technologicLowerBound; if (metrologicRangeLowerBound[i] > metrologicRangeUpperBound[i]) { throw new ApplicationException("MetrologicBounds are incorrect"); } if (technologicRangeLowerBound[i] > technologicRangeUpperBound[i]) { throw new ApplicationException("TechnologicBounds are incorrect"); } absTolerance[i] = variables.tolerance; } measureIndicator = SparseMatrix.OfDiagonalArray(countOfThreads, countOfThreads, measIndicator); standardDeviation = SparseMatrix.OfDiagonalArray(countOfThreads, countOfThreads, tol); H = new SparseMatrix(countOfThreads, countOfThreads); H = measureIndicator * standardDeviation; dVector = new SparseVector(countOfThreads); dVector = H * (-1) * measuredValues; // Инициализация вектора b reconciledValues = new SparseVector(incidenceMatrix.RowCount); //GlobalTest(); }
// POST: api/Balance public BalanceOutput Post([FromBody] BalanceInput balanceInput) { CalculatorService calculatorService = new CalculatorService(); return(calculatorService.Calculate(balanceInput)); }