public override IList <Node> Solve() { C5.IntervalHeap <Node> heap = new C5.IntervalHeap <Node>(Comparer <Node> .Create((Node f, Node s) => (f.TotalCost).CompareTo(s.TotalCost))); HashSet <Tuple <int, int> > set = new HashSet <Tuple <int, int> >(); for (int i = 0; i < Initial.GetLength(0); i++) { for (int j = 0; j < Initial.GetLength(1); j++) { set.Add(new Tuple <int, int>(i, j)); } } var ini = new Node(set) { Board = Initial }; heap.Add(ini); ISet <Node> visited = new HashSet <Node>(); while (!heap.IsEmpty) { var n = heap.DeleteMin(); if (n.IsFinal) { return(n.Parents); } visited.Add(n); n.GenerateChildren().Where(e => !visited.Contains(e)).ToList().ForEach(e => heap.Add(e)); } return(new List <Node>()); }
private Matrix <double>[] _createRow(int index, Matrix <double> O, Matrix <double> I, Matrix <double> B) { IList <Matrix <double> > list = new List <Matrix <double> >(); if (index == 0) { list.Add(B); list.Add(I); for (int i = 0; i < Initial.GetLength(1) - 2; i++) { list.Add(O); } _temp = list.Count; return(list.ToArray()); } if (index == Initial.GetLength(0) - 1) { for (int i = 0; i < Initial.GetLength(1) - 2; i++) { list.Add(O); } list.Add(I); list.Add(B); return(list.ToArray()); } for (int i = 1; i <= index - 1; i++) { list.Add(O); } list.Add(I); list.Add(B); list.Add(I); for (int i = 1; i <= Initial.GetLength(1) - index - 2; i++) { list.Add(O); } return(list.ToArray()); }
public override IList <Node> Solve() { int r = Initial.GetLength(0), c = Initial.GetLength(1); //Matrix<double> O = Matrix<double>.Build.Dense(r, c, 0), // I = Matrix<double>.Build.DenseDiagonal(r, c, 1), // B = Matrix<double>.Build.Dense(r, c, 0); //for (int i = 0; i < r; i++) //{ // B[i, i] = 1; // if (_isValid(B, i + 1, i)) // B[i + 1, i] = 1; // if (_isValid(B, i, i + 1)) // B[i, i + 1] = 1; //} //IList<Matrix<double>[]> list = new List<Matrix<double>[]>(); //for (int i = 0; i < r; i++) //{ // list.Add(_createRow(i, O, I, B)); //} //var A = Matrix<double>.Build.DenseOfMatrixArray(_createRectangularArray(list)); //var b = Matrix<double>.Build.Dense(r, c, 0); //var copy = Matrix<double>.Build.DenseOfMatrix(A); //Console.WriteLine(A); ////_reduce(A); //A = Matrix<double>.Build.DenseOfArray(_reduce(A.ToArray())); //Console.WriteLine(A); //var R = copy * A.Inverse(); //var result = Matrix<double>.Build.Dense(r, c, 0); //for (int i = 0; i < r; i++) //{ // for (int j = 0; j < c; j++) // { // result[i, j] = (int)(Math.Floor(R[i, j]) * (Initial[i, j] == Node.State.ON ? 1 : 0)); // } //} //IList<Node> nodes = new List<Node>(); //var last = new Node() { Board = Initial }; //nodes.Add(last); //result = result.Map(e => Math.Round(e)); //for (int i = 0; i < r; i++) //{ // for (int j = 0; j < c; j++) // { // if (result[i, j] >= 1) // { // var bb = last.Board.Clone() as Node.State[,]; // _click(bb, i, j); // var node = new Node() { Board = bb }; // nodes.Add(node); // last = node; // } // } //} //return nodes; int[] b = new int[r * c]; int index = 0; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { b[index++] = Initial[i, j]? 1 : 0; } } var ASize = r * c; var A = new MMatrix(ASize, ASize); for (int Arow = 0; Arow < ASize; Arow++) { for (int Acol = 0; Acol < ASize; Acol++) { int i, j, i_, j_ = 0; i = Arow / c; j = Arow % c; i_ = Acol / c; j_ = Acol % c; if (i_ >= 0 && i_ <= ASize && j_ >= 0 && j_ <= ASize) { if (Math.Abs(i - i_) + Math.Abs(j - j_) <= 1) { A.set(Arow, Acol, 1); } else { A.set(Arow, Acol, 0); } } } } for (int i = 0; i < b.Length; i++) { A.setBVector(i, 0, b[i]); } A.reducedRowEchelonForm(); Func <MMatrix, bool> canBeSolved = (m) => { for (int curr_Row = r * c - 1; curr_Row >= 0; curr_Row--) { for (int i = 0; i < r * c; i++) { if (m.get(curr_Row, i) != 0) { return(true); } } if (m.getBVector(curr_Row, 0) != 0) { return(false); } } return(true); }; if (canBeSolved(A)) { int[][] solution = new int[r][]; for (int i = 0; i < r; i++) { solution[i] = new int[c]; } for (int i = 0; i < ASize; i++) { solution[i / r][i % c] = A.getBVector(i, 0); } IList <Node> nodes = new List <Node>(); var last = new Node(new HashSet <Tuple <int, int> >()) { Board = Initial }; nodes.Add(last); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { if (solution[i][j] == 1) { var bb = last.Board.Clone() as Board; bb.Click(i, j); var node = new Node(null) { Board = bb }; nodes.Add(node); last = node; } } } return(nodes); } return(new List <Node>()); }
public override IList <Node> Solve() { if (!(new Solver().CanBeSolved(Initial))) { return(new List <Node>()); } HashSet <Tuple <int, int> > set = new HashSet <Tuple <int, int> >(); for (int i = 0; i < Initial.GetLength(0); i++) { for (int j = 0; j < Initial.GetLength(1); j++) { set.Add(new Tuple <int, int>(i, j)); } } var ini = new Node(set) { Board = Initial }; // async code if (Method == SolveMethod.ASYNC) { ConcurrentQueue <Node> q = new ConcurrentQueue <Node>(); var t = ini.GenerateChildren(); foreach (var x in t) { q.Enqueue(x); } const int maxi = 200000; Tuple <IEnumerable <Node>, int> res = new Tuple <IEnumerable <Node>, int>(null, maxi); IList <Task> tasks = new List <Task>(); object l = new object(); for (int i = 1; i <= 100; ++i) { tasks.Add(Task.Factory.StartNew(() => { Node n; while (q.TryDequeue(out n)) { if (n.IsFinal) { var ps = n.Parents; //lock (l) { if (n.Cost < res.Item2) { res = new Tuple <IEnumerable <Node>, int>(ps, n.Cost); } } continue; } t = n.GenerateChildren(); foreach (var x in t) { //lock (l) { if (x.Cost < res.Item2) { q.Enqueue(x); } } } } } )); } Task.WaitAll(tasks.ToArray()); if (res.Item2 == maxi) { return(new List <Node>()); } return(res.Item1.ToList()); } // sync code Queue <Node> queue = new Queue <Node>(); queue.Enqueue(ini); ISet <Board> visited = new HashSet <Board>(); while (queue.Count != 0) { var n = queue.Dequeue(); if (n.IsFinal) { return(n.Parents); } visited.Add(n.Board); n.GenerateChildren().Where(s => !visited.Contains(s.Board)).ToList().ForEach(queue.Enqueue); } return(new List <Node>()); }