public void Parser_Test1() { Parser parser = new Parser(); ProblemInput input = parser.ParseFromData(Properties.Resources.ExampleInput); Assert.AreEqual(3, input.NumberOfCachedServers); Assert.AreEqual(2, input.NumberOfEndpoints); Assert.AreEqual(4, input.NumberOfRequestDescription); Assert.AreEqual(5, input.NumberOfVideos); Assert.AreEqual(100, input.ServerCapacity); Assert.AreEqual(50, input.Videos[0].Size); Assert.AreEqual(50, input.Videos[1].Size); Assert.AreEqual(80, input.Videos[2].Size); Assert.AreEqual(30, input.Videos[3].Size); Assert.AreEqual(110, input.Videos[4].Size); Assert.AreEqual(1000, input.Endpoints[0].DataCenterLatency); Assert.AreEqual(3, input.Endpoints[0].ServersLatency.Count); Assert.AreEqual(500, input.Endpoints[1].DataCenterLatency); Assert.AreEqual(0, input.Endpoints[1].ServersLatency.Count); Assert.AreEqual(1500, input.RequestsDescriptions[0].NumOfRequests); Assert.AreEqual(1000, input.RequestsDescriptions[1].NumOfRequests); Assert.AreEqual(500, input.RequestsDescriptions[2].NumOfRequests); Assert.AreEqual(1000, input.RequestsDescriptions[3].NumOfRequests); }
//.#./..#/### => #..#/..../..../#..#"; static void Main(string[] args) { Console.WriteLine("Example: " + Solve(s_example, 2)); Console.WriteLine("Real: " + Solve(ProblemInput.FetchBlocking(s_year, s_day), 5)); Console.WriteLine("Real 2: " + Solve(ProblemInput.FetchBlocking(s_year, s_day), 18)); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("Example: " + Solve(s_example, "abcde")); Console.WriteLine("Real: " + Solve(ProblemInput.FetchBlocking(s_year, s_day), "abcdefghijklmnop")); Console.WriteLine("Example 2: " + Solve2(s_example, "abcde")); Console.WriteLine("Real 2: " + Solve2(ProblemInput.FetchBlocking(s_year, s_day), "abcdefghijklmnop")); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine(Run(s_example)); var input = ProblemInput.FetchBlocking(2017, 6); Console.WriteLine(Run(input)); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine(Run("R2, L3")); Console.WriteLine(Run("R2, R2, R2")); Console.WriteLine(Run("R5, L5, R5, R3")); Console.WriteLine(Run(ProblemInput.FetchBlocking(2016, 1))); // Console.WriteLine(Run2("R8, R4, R4, R8")); Console.WriteLine(Run2(ProblemInput.FetchBlocking(2016, 1))); Console.ReadKey(); }
public void Parser_TestBla() { Parser parser = new Parser(); ProblemInput input = parser.ParseFromData(Properties.Resources.RealInput); List <Server> servers = input.Servers.OrderByDescending(_ => _.Capacity).ToList(); List <Server> servers1 = input.Servers.OrderByDescending(_ => _.Slots).ToList(); List <Server> servers2 = input.Servers.OrderByDescending(_ => ((double)_.Capacity) / _.Slots).ToList(); int count = servers.Sum(_ => _.Slots); }
static void Main(string[] args) { Console.WriteLine(Solve2( @"ULL RRDDD LURDL UUUUD")); Console.WriteLine(Solve2(ProblemInput.FetchBlocking(2016, 2))); Console.ReadKey(); }
public ProblemOutput Solve(ProblemInput problem) { var goldfarbIdnani = new GoldfarbIdnani(problem.Quadratic, problem.Linear, problem.ConstraintMatrix, problem.ConstraintValues); var hasSolution = goldfarbIdnani.Maximize(); return(new ProblemOutput { HasSolution = hasSolution, Value = hasSolution ? goldfarbIdnani.Value:double.NaN, Solution = hasSolution ? goldfarbIdnani.Solution : Enumerable.Repeat(double.NaN, problem.N).ToArray() }); }
public void Parser_Test() { Parser parser = new Parser(); ProblemInput input = parser.ParseFromData(Properties.Resources.TestInput); Assert.AreEqual(2, input.Rows); Assert.AreEqual(5, input.Columns); Assert.AreEqual(2, input.Pools.Count); Assert.AreEqual(1, input.UnavilableSlots.Count); Assert.AreEqual(5, input.Servers.Count); Assert.AreEqual(new Coordinate(0, 0), input.UnavilableSlots[0]); }
public ProblemOutput Solve(ProblemInput problem) { ProblemOutput solution = new ProblemOutput { D = problem.D, HasSolution = false, Value = double.NaN, Solution = Enumerable.Repeat(double.NaN, problem.N).ToArray(), SkipedIndexes = problem.SkipedIndexes }; if (problem.N > 0) { MWArray[] input = new MWArray[4]; MWNumericArray _h = problem.Quadratic; MWNumericArray _f = problem.Linear; MWNumericArray _A = problem.ConstraintMatrix; MWNumericArray _B = problem.ConstraintValues; input[0] = _h; input[1] = _f; input[2] = _A; input[3] = _B; try { SolveQPClass r = new SolveQPClass(); MWArray[] result = new MWArray[3]; r.SolveQP(result.Length, ref result, input); bool success = ((MWNumericArray)result[2]).ToScalarDouble() > 0; if (success) { double[] x = (double[])(((MWNumericArray)result[0]).ToVector(MWArrayComponent.Real)); double val = ((MWNumericArray)result[1]).ToScalarDouble(); solution.HasSolution = success; solution.Value = -val; solution.Solution = x; } r.Dispose(); } catch (Exception e) { return(solution); } } return(solution); }
public OutputList GetUserProblems([FromBody] ProblemInput input) { var querys = from problems in _doContext.Problems.ToList() join answers in _doContext.Answers.ToList() on problems.Id equals answers.ProblemId into JoinedEmpAnswer from answer in JoinedEmpAnswer.DefaultIfEmpty() join users in _doContext.Users.ToList() on answer.UserId equals users.Id into JoinedEmpUser from user in JoinedEmpUser.DefaultIfEmpty() select new ProblemListDto { Id = problems.Id, Title = problems.Title, Info = answer == null ? "" : answer.Content, Another = user == null ? "匿名" : user.UserName, UserId = user == null ? 0 : user.Id, Img = "../../static/hen/问.jpg'" }; querys = querys.Where(x => x.UserId == input.UserId); querys = querys.GroupBy(x => x.Id).Select(a => new ProblemListDto { Id = a.Key, Title = a.FirstOrDefault().Title, Another = a.FirstOrDefault().Another, Info = a.FirstOrDefault().Info, UserId = a.FirstOrDefault().UserId, Img = a.FirstOrDefault().Img }).ToList(); var totel = querys.Count(); var result = _mapper.Map <List <ProblemListDto> >(querys); OutputList outputList = new OutputList { Totel = totel, Data = result }; return(outputList); }
static void Main(string[] args) { Console.WriteLine(Solve2( @"pbga (66) xhth (57) ebii (61) havc (66) ktlj (57) fwft (72) -> ktlj, cntj, xhth qoyq (66) padx (45) -> pbga, havc, qoyq tknk (41) -> ugml, padx, fwft jptl (61) ugml (68) -> gyxo, ebii, jptl gyxo (61) cntj (57)")); Console.WriteLine(Solve2(ProblemInput.FetchBlocking(2017, 7))); Console.ReadKey(); }
public ProblemOutput Solve(ProblemInput problem) { try { GRBEnv env = new GRBEnv(); double[] lb = ArrayHelper.CreateArray <double>(problem.Linear.Length); double[] sol = ArrayHelper.CreateArray <double>(problem.Linear.Length); double val = 0.0; var success = GurobiSolve(env, problem.ConstraintMatrix.GetLength(0), problem.ConstraintMatrix.GetLength(1), problem.Linear, problem.Quadratic, problem.ConstraintMatrix, problem.ConstraintValues, lb, null, null, sol, ref val); var solution = new ProblemOutput { HasSolution = success, Value = success ? val : double.NaN, Solution = success ? sol : Enumerable.Repeat(double.NaN, problem.N).ToArray() }; // Dispose of environment env.Dispose(); return(solution); } catch (GRBException e) { return(null); } }
public void Run(int year, int day) { try { Console.WriteLine(); { var input = Example1; var sw = new Stopwatch(); sw.Start(); var result = Solve1(input); sw.Stop(); Console.Write($"Example 1 ({sw.ElapsedMilliseconds} ms): "); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(result); Console.ResetColor(); } { var input = ProblemInput.FetchBlocking(year, day); var sw = new Stopwatch(); sw.Start(); var result = Solve1(input); sw.Stop(); Console.Write($"Real 1 ({sw.ElapsedMilliseconds} ms): "); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(result); Console.ResetColor(); } Console.WriteLine(); { var input = Example2; var sw = new Stopwatch(); sw.Start(); var result = Solve2(input); sw.Stop(); Console.Write($"Example 2 ({sw.ElapsedMilliseconds} ms): "); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(result); Console.ResetColor(); } { var input = ProblemInput.FetchBlocking(year, day); var sw = new Stopwatch(); sw.Start(); var result = Solve2(input); sw.Stop(); Console.Write($"Real 2 ({sw.ElapsedMilliseconds} ms): "); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(result); Console.ResetColor(); } Console.WriteLine(); Console.ReadKey(); } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e.Message); Console.WriteLine(); var lines = e.StackTrace.Split('\n'); foreach (var line in lines) { var s = line.Split(new string[] { "line" }, StringSplitOptions.None); Console.ForegroundColor = ConsoleColor.Red; Console.Write(s[0]); if (s.Length > 1) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("line" + s[1]); } } Console.ReadKey(); } }
// maximize x'*Q*x + C'*x subject to Ax >= B public ProblemInput Transform(int day, int roomTypes, int categoryTypes, int mealTypes, IEnumerable <ChildRooms> childRooms, double[, ,] a, double[, ,] b, double[,] h, double[, ,] L, double[, ,] U, double[] R, double w, PriceRelation[] T, bool withACondition = true) { var allTypes = roomTypes * categoryTypes * mealTypes; var allTypes2 = allTypes; int minusIndex = 0; int[, ,] minusIndexes = new int[roomTypes, categoryTypes, mealTypes]; var skipedIndexes = GetSkipedIndexes(roomTypes, categoryTypes, mealTypes, a, b, L, U, R); for (var i = 0; i < roomTypes; ++i) { for (var j = 0; j < categoryTypes; ++j) { for (var k = 0; k < mealTypes; ++k) { if (skipedIndexes.Any(s => s.i == i && s.j == j && s.k == k)) { minusIndex++; } else { minusIndexes[i, j, k] = minusIndex; } } } } allTypes2 -= minusIndex; var n = 2 * allTypes2; var m = 4 * allTypes2 + roomTypes + (T.Length + (childRooms.Count() + childRooms.Select(v => v.Parent).Distinct().Count()) * mealTypes) * categoryTypes; var result = new ProblemInput(n, m); result.D = day; result.SkipedIndexes = skipedIndexes; var q = result.Quadratic; var c = result.Linear; var A = result.ConstraintMatrix; // A var B = result.ConstraintValues; // B double[] debug = new double[allTypes2]; minusIndex = 0; for (var i = 0; i < roomTypes; ++i) { B[3 * allTypes2 + i] = -R[i]; for (var j = 0; j < categoryTypes; ++j) { for (var k = 0; k < mealTypes; ++k) { if (!skipedIndexes.Any(s => s.i == i && s.j == j && s.k == k)) { var offset = i * categoryTypes * mealTypes + j * mealTypes + k - minusIndex; q[offset, offset] = 2 * b[i, j, k]; c[offset] = a[i, j, k] - b[i, j, k] * h[i, k]; c[allTypes2 + offset] = -w; A[offset, offset] = 1; A[offset, allTypes2 + offset] = 1; var lowerBound = L[i, j, k] > h[i, k] ? L[i, j, k] : h[i, k]; B[offset] = lowerBound > 0 ? lowerBound : 0; A[allTypes2 + offset, offset] = -1; A[allTypes2 + offset, allTypes2 + offset] = 1; B[allTypes2 + offset] = -U[i, j, k]; if (withACondition) { A[(2 * allTypes2 + offset), offset] = b[i, j, k]; B[2 * allTypes2 + offset] = -a[i, j, k]; } //Group rooms restriction with vacant rooms number B[3 * allTypes2 + i] += a[i, j, k]; A[3 * allTypes2 + i, offset] = -b[i, j, k]; if (childRooms.Any(v => v.Parent == i)) { var childs = childRooms.Where(v => v.Parent == i).ToList(); var total = childs.Sum(v => v.Quantity); foreach (var child in childs) { if (!skipedIndexes.Any(s => s.i == child.Child && s.j == j && s.k == k)) { var koef = ((double)child.Quantity) / (childRooms.Where(v => v.Child == child.Child).Sum(v => v.Quantity) * total); B[3 * allTypes2 + i] += koef * (a[child.Child, j, k] + (child.Quantity * R[i] - R[child.Child])); var newOffset = child.Child * categoryTypes * mealTypes + j * mealTypes + k - minusIndexes[child.Child, j, k]; A[3 * allTypes2 + i, newOffset] = -b[child.Child, j, k] * koef; } } } else if (childRooms.Any(v => v.Child == i)) { foreach (var parent in childRooms.Where(v => v.Child == i)) { if (!skipedIndexes.Any(s => s.i == parent.Parent && s.j == j && s.k == k)) { B[3 * allTypes2 + i] += parent.Quantity * a[parent.Parent, j, k]; var newOffset = parent.Parent * categoryTypes * mealTypes + j * mealTypes + k - minusIndexes[parent.Parent, j, k]; A[3 * allTypes2 + i, newOffset] = -b[parent.Parent, j, k] * parent.Quantity; } } } B[3 * allTypes2 + roomTypes + offset] = 0; A[3 * allTypes2 + roomTypes + offset, allTypes2 + offset] = 1; } else { minusIndex++; } } } } int index = 1; for (var i = 0; i < T.Length; ++i) { for (var j = 0; j < categoryTypes; ++j) { if (!skipedIndexes.Any(s => (s.i == T[i].R1 || s.i == T[i].R2) && s.j == j && (s.k == T[i].M1 || s.k == T[i].M2))) { A[m /*- T.Length * categoryTypes*/ - index, T[i].R2 * categoryTypes * mealTypes + j * mealTypes + T[i].M2 - minusIndexes[T[i].R2, j, T[i].M2]] = 1.0; A[m - index, T[i].R1 * categoryTypes * mealTypes + j * mealTypes + T[i].M1 - minusIndexes[T[i].R1, j, T[i].M1]] = -1.0; B[m - index] = 0; index++; } } } //price restrictions for group rooms for (var j = 0; j < categoryTypes; ++j) { for (var k = 0; k < mealTypes; ++k) { foreach (var child in childRooms) { A[m - index, child.Child *categoryTypes *mealTypes + j * mealTypes + k - minusIndexes[child.Child, j, k]] = -1; A[m - index, child.Parent *categoryTypes *mealTypes + j * mealTypes + k - minusIndexes[child.Parent, j, k]] = 1; B[m - index] = 0; index++; } foreach (var parent in childRooms.Select(v => v.Parent).Distinct().ToList()) { A[m - index, parent *categoryTypes *mealTypes + j * mealTypes + k - minusIndexes[parent, j, k]] = -1; foreach (var child in childRooms.Where(v => v.Parent == parent).ToList()) { A[m - index, child.Child *categoryTypes *mealTypes + j * mealTypes + k - minusIndexes[child.Child, j, k]] = 1; } B[m - index] = 0; index++; } } } return(result); }
public ProblemInput TransformForW(int day, int roomTypes, int categoryTypes, int mealTypes, double[,,] a, double[,,] b, double[,] h, double[,,] L, double[,,] U, double[] R, bool withACondition = true) { var allTypes = roomTypes * categoryTypes * mealTypes; var allTypes2 = allTypes; int minusIndex = 0; int[,,] minusIndexes = new int[roomTypes, categoryTypes, mealTypes]; var skipedIndexes = GetSkipedIndexes(roomTypes, categoryTypes, mealTypes, a, b, L, U, R); for (var i = 0; i < roomTypes; ++i) { for (var j = 0; j < categoryTypes; ++j) { for (var k = 0; k < mealTypes; ++k) { if (skipedIndexes.Any(s => s.i == i && s.j == j && s.k == k)) { minusIndex++; } else { minusIndexes[i, j, k] = minusIndex; } } } } allTypes2 -= minusIndex; var n = allTypes2; var m = 3 * allTypes2; var result = new ProblemInput(n, m); result.D = day; result.SkipedIndexes = skipedIndexes; var q = result.Quadratic; var c = result.Linear; var A = result.ConstraintMatrix; // A var B = result.ConstraintValues; // B minusIndex = 0; for (var i = 0; i < roomTypes; ++i) { for (var j = 0; j < categoryTypes; ++j) { for (var k = 0; k < mealTypes; ++k) { if (!skipedIndexes.Any(s => s.i == i && s.j == j && s.k == k)) { var offset = i * categoryTypes * mealTypes + j * mealTypes + k - minusIndex; q[offset, offset] = 2 * b[i, j, k]; c[offset] = a[i, j, k] - b[i, j, k] * h[i, k]; A[offset, offset] = 1; var koef = h[i, k]; B[offset] = koef > 0 ? koef : 0; A[(allTypes2 + offset), offset] = -1; B[allTypes2 + offset] = -10000000; if (withACondition) { if (a[i, j, k] <= 0) { A[(2 * allTypes2 + offset), offset] = 1; B[2 * allTypes2 + offset] = 0; } else { A[(2 * allTypes2 + offset), offset] = b[i, j, k]; B[2 * allTypes2 + offset] = -a[i, j, k]; } } } else { minusIndex++; } } } } return(result); }