private LabeledData[] collect_samples(Func <double[], double[]>[] functions, Task task) { List <LabeledData> ldata = new List <LabeledData>(); foreach (Func <double[], double[]> func in functions) { int j = 0; while (j < 1) { List <double[]> points = get_start_points(j, task, func); for (int k = 0; k < 1; k++) { points.AddRange(testDefWayUntilGood(points.ToArray(), func).ToList <double[]>()); } ShepardApprox def_model = new ShepardApprox(config.FunctionDimension, points.ToArray()); int[] count = new int[config.FunctionDimension]; for (int i = 0; i < config.FunctionDimension; i++) { count[i] = (def_model.Min[i] == def_model.Max[i]) ? 1 : NGRID; } Grid grid = new Grid(def_model.N, def_model.M, def_model.Min, def_model.Max, count); Solver.dist = update_path_to_knowing_points(grid, points.ToArray(), config.FunctionDimension); int n = grid.Node.Length; for (int i = 0; i < grid.Node.Length; i++) { double[] cuurentNode = (double[])grid.Node[i].Clone(); def_model.Calculate(cuurentNode); double[] approxFunctionVal = new double[def_model.M]; for (int k = 0; k < def_model.M; ++k) { approxFunctionVal[k] = cuurentNode[def_model.N + k]; } var realFunctionVal = func(grid.Node[i]); double[] diffs = realFunctionVal.Zip(approxFunctionVal, (d1, d2) => Math.Abs(d1 - d2)).ToArray(); double err = (diffs.Sum() / diffs.Length); //int pointClass = 0; //if (err > config.Approximation) //{ // pointClass = 1; //} double pointClass = err; double[] features = build_features(grid.Node[i], def_model, grid, Solver.dist, points.ToArray(), i); ldata.Add(new LabeledData(features, pointClass)); featureCount = features.Length; } j++; } } return(ldata.ToArray()); }
protected double[][] testDefWayUntilGood(double[][] points, Func <double[], double[]> func) { int pointAmount = config.PointAmount; int i = 0; double maxErr = 10; int goodPr = 0; while (i < 100000000 && maxErr > config.Approximation && goodPr < 50) { ShepardApprox model = new ShepardApprox(config.FunctionDimension, points); Analyzer analyzer = new Analyzer(model, points); analyzer.do_default_analyse(); goodPr = analyzer.getGoodPr(func, config.Approximation); Console.WriteLine("Good pr " + goodPr); double[][] xx = analyzer.Result; int newPointsAmount = Math.Min(config.PredictionPointAmount, xx.Length); pointAmount = pointAmount + newPointsAmount; points = getNewPoints(points, analyzer.Result, newPointsAmount, config.FunctionDimension, func); double[][] new_points = new double[newPointsAmount][]; for (int j = 0; j < newPointsAmount; j++) { new_points[j] = new double[xx[j].Length]; Array.Copy(xx[j], new_points[j], xx[j].Length); model.Calculate(new_points[j]); } double tempErr = 0; for (int k = 0; k < new_points.Length; k++) { double err = Math.Abs(points[pointAmount - newPointsAmount + k][config.FunctionDimension] - new_points[k][config.FunctionDimension]); if (err > tempErr) { tempErr = err; } } maxErr = tempErr; i++; } ShepardApprox model1 = new ShepardApprox(config.FunctionDimension, points); Analyzer analyzer1 = new Analyzer(model1, points); analyzer1.do_default_analyse(); //return analyzer1.getGoodSamples(func, parser.Approximation, goodPr); return(points); }
private double[] build_features(double[] point, IFunction model, Grid grid, double[] distToKnownPoints, double[][] knownPoints = null, int index = -1) { // на сколько образующая домен точка близка // сколько до и после монотонно // расстояние до известной точки Analyzer analyzer = new Analyzer(model, knownPoints); analyzer.do_some_analyse(); // min, max in locality double maxNeighbours = double.MinValue; double minNeighbours = double.MaxValue; foreach (var neighbour in grid.Neighbours(index)) { double[] calcNeighbour = (double[])grid.Node[neighbour].Clone(); model.Calculate(calcNeighbour); double calcNeighbourVal = calcVal(model.M, model.N, calcNeighbour); if (calcNeighbour[calcNeighbour.Length - 1] < minNeighbours) { minNeighbours = calcNeighbourVal; } if (calcNeighbour[calcNeighbour.Length - 1] > maxNeighbours) { maxNeighbours = calcNeighbourVal; } } // current val double[] curentNode = (double[])grid.Node[index].Clone(); model.Calculate(curentNode); double curentNodeVal = calcVal(model.M, model.N, curentNode); if (curentNodeVal < minNeighbours) { minNeighbours = curentNodeVal; } if (curentNodeVal > maxNeighbours) { maxNeighbours = curentNodeVal; } List <double[]> temp_points = new List <double[]>(); temp_points = knownPoints.ToList(); temp_points.RemoveAt(analyzer.Domain(grid.Node[index])); ShepardApprox new_model = new ShepardApprox(model.N, temp_points.ToArray()); double[] old_model_point = (double[])grid.Node[index].Clone(); model.Calculate(old_model_point); double[] new_model_point = (double[])grid.Node[index].Clone(); new_model.Calculate(new_model_point); double err = 0; for (int k = 0; k < model.M; ++k) { double newErr = Math.Abs(old_model_point[model.N + k] - new_model_point[model.N + k]); if (newErr > err) { err = newErr; } } this.featureCount = 4; double[] features = new double[featureCount]; features[0] = maxNeighbours - curentNodeVal; features[1] = curentNodeVal - minNeighbours; features[2] = distToKnownPoints[index]; features[3] = err; return(features); }