public void CalculatePrediction(int steps_ahead) { if (steps_ahead > 0) { predicted_means_ = new DoubleMatrix[steps_ahead]; predicted_variances_ = new DoubleMatrix[steps_ahead]; DoubleMatrix previous_state_mean = estimated_state_means_[estimated_state_means_.length - 1]; DoubleMatrix previous_state_covariance = estimated_state_covariances_[estimated_state_covariances_.length - 1]; for (int ii = 0; ii < steps_ahead; ii++) { DoubleMatrix a = state_transition_matrix_ref_.mmul(previous_state_mean); DoubleMatrix R = state_transition_matrix_ref_ .mmul(previous_state_covariance) .mmul(state_transition_matrix_ref_.transpose()) .addi(transition_covariance_matrix_ref_); // One-step-ahead observation prediction. predicted_means_[ii] = observation_matrix_ref_.mmul(a); predicted_variances_[ii] = observation_matrix_ref_ .mmul(R) .mmul(observation_matrix_ref_.transpose()) .addi(observation_covariance_matrix_ref_); previous_state_mean = a; previous_state_covariance = R; } } else { // smoothing predicted_means_ = predicted_observation_means_; predicted_variances_ = predicted_observation_covariances_; } }
public void weight_contribution( DoubleMatrix h0, DoubleMatrix v0, DoubleMatrix h1, DoubleMatrix v1) { this.w1 .addi(h0.transpose().mmul(v0).subi(h1.transpose().mmul(v1))) .muli(learningRate / v_data.rows) .subi(weights.mul(weightCost)); this.vb1.addi(v0.sub(v1).muli(learningRate / v_data.rows).columnMeans()); this.hb1.addi(h0.sub(h1).muli(learningRate / v_data.rows).columnMeans()); }
/** * 求广义逆矩阵,使用正交投影方法 * * @param ht * @return */ public DoubleMatrix getMPMatrixByOP(DoubleMatrix H) { DoubleMatrix M; try { M = H.mmul(H.transpose()); // HHt addPositiveValue(M); return H.transpose().mmul(inverse(M)); } catch (LapackException e) { M = H.transpose().mmul(H); // HtH addPositiveValue(M); return inverse(M).mmul(H.transpose()); } }
private void UpdateFilteredStates() { DoubleMatrix state_transition_matrix_T = state_transition_matrix_ref_.transpose(); DoubleMatrix observation_matrix_T = observation_matrix_ref_.transpose(); for (int ii = 0; ii < number_of_observations_; ii++) { DoubleMatrix previous_state_mean = (ii == 0) ? initial_state_mean_ref_ : estimated_state_means_[ii - 1]; DoubleMatrix previous_state_covariance = (ii == 0) ? initial_state_covariance_ref_ : estimated_state_covariances_[ii - 1]; // One-step-ahead state prediction. (i) DoubleMatrix a = state_transition_matrix_ref_.mmul(previous_state_mean); DoubleMatrix R = state_transition_matrix_ref_ .mmul(previous_state_covariance) .mmul(state_transition_matrix_T) .addi(transition_covariance_matrix_ref_); // One-step-ahead observation prediction(ii) predicted_observation_means_[ii] = observation_matrix_ref_.mmul(a); predicted_observation_covariances_[ii] = observation_matrix_ref_ .mmul(R) .mmul(observation_matrix_T) .addi(observation_covariance_matrix_ref_); // if current observation is null: if (observations_ref_[ii] == null) { estimated_state_means_[ii] = a; estimated_state_covariances_[ii] = R; } else { DoubleMatrix inv_predicted_observation_covariances = InverseMatrix.inverse(predicted_observation_covariances_[ii]); DoubleMatrix R_obs_mat_T_inv_pred_obs_cov = R.mmul(observation_matrix_T).mmuli(inv_predicted_observation_covariances); // forward filtering states.(iii) estimated_state_means_[ii] = R_obs_mat_T_inv_pred_obs_cov.mmul( observations_ref_[ii].sub(predicted_observation_means_[ii])) .addi(a); estimated_state_covariances_[ii] = R.sub(R_obs_mat_T_inv_pred_obs_cov.mmul(observation_matrix_ref_).mmuli(R)); estimated_means_[ii] = observation_matrix_ref_.mmul(estimated_state_means_[ii]); estimated_covariances_[ii] = observation_matrix_ref_ .mmul(estimated_state_covariances_[ii]) .mmul(observation_matrix_T) .addi(observation_covariance_matrix_ref_); } } }
/** * 求广义逆矩阵 Theory:最大秩分解 U*S*Vt = A C=U*sqrt(S) D=sqrt(S)*Vt <==> A=C*D MP(A) = * D'*inv(D*D')*inv(C'*C)*C' * * @param ht * @return */ public DoubleMatrix getMPMatrix(DoubleMatrix ht) { int m = ht.rows; int n = ht.columns; DoubleMatrix[] USV = Singular.fullSVD(ht); DoubleMatrix U = USV[0]; double[] s_raw = USV[1].data.clone(); // 奇异值 // 去除0 int k = 0; while (k < s_raw.length && s_raw[k] > 0.1e-5) { k++; } double[] s = new double[k]; for (int i = 0; i < s.length; i++) { s[i] = s_raw[i]; } DoubleMatrix Vt = USV[2].transpose(); // n*n int sn = s.length; for (int i = 0; i < sn; i++) { s[i] = Math.sqrt(s[i]); } // 构造m*sn sn*n矩阵 DoubleMatrix S1 = new DoubleMatrix(m, sn); DoubleMatrix S2 = new DoubleMatrix(sn, n); for (int i = 0; i < sn; i++) { S1.put(i, i, s[i]); S2.put(i, i, s[i]); } DoubleMatrix C = U.mmul(S1); DoubleMatrix D = S2.mmul(Vt); DoubleMatrix DT = D.transpose(); DoubleMatrix DD = D.mmul(DT); DoubleMatrix invDD = inverse(DD); DoubleMatrix DDD = DT.mmul(invDD); DoubleMatrix CT = C.transpose(); DoubleMatrix CC = CT.mmul(C); DoubleMatrix invCC = inverse(CC); DoubleMatrix CCC = invCC.mmul(CT); DoubleMatrix Ainv = DDD.mmul(CCC); return Ainv; }
public static void main(String argv[]) { modshogun.init_shogun_with_defaults(); int dim = 7; DoubleMatrix data = DoubleMatrix.rand(dim, dim); RealFeatures feats = new RealFeatures(data); DoubleMatrix data_T = data.transpose(); DoubleMatrix symdata = data.add(data_T); int cols = (1 + dim) * dim / 2; DoubleMatrix lowertriangle = DoubleMatrix.zeros(1, cols); int count = 0; for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { if (j <= i) { lowertriangle.put(0, count++, symdata.get(i, j)); } } } CustomKernel kernel = new CustomKernel(); kernel.set_triangle_kernel_matrix_from_triangle(lowertriangle); DoubleMatrix km_triangletriangle = kernel.get_kernel_matrix(); kernel.set_triangle_kernel_matrix_from_full(symdata); DoubleMatrix km_fulltriangle = kernel.get_kernel_matrix(); kernel.set_full_kernel_matrix_from_full(data); DoubleMatrix km_fullfull = kernel.get_kernel_matrix(); modshogun.exit_shogun(); }
@Override public NeuralNetwork transpose() { try { Constructor<?> c = Dl4jReflection.getEmptyConstructor(getClass()); c.setAccessible(true); NeuralNetwork ret = (NeuralNetwork) c.newInstance(); ret.setMomentumAfter(momentumAfter); ret.setConcatBiases(concatBiases); ret.setResetAdaGradIterations(resetAdaGradIterations); ret.setVBiasAdaGrad(hBiasAdaGrad); ret.sethBias(vBias.dup()); ret.setvBias(DoubleMatrix.zeros(hBias.rows, hBias.columns)); ret.setnHidden(getnVisible()); ret.setnVisible(getnHidden()); ret.setW(W.transpose()); ret.setL2(l2); ret.setMomentum(momentum); ret.setRenderEpochs(getRenderIterations()); ret.setSparsity(sparsity); ret.setRng(getRng()); ret.setDist(getDist()); ret.setAdaGrad(wAdaGrad); ret.setLossFunction(lossFunction); ret.setOptimizationAlgorithm(optimizationAlgo); ret.setConstrainGradientToUnitNorm(constrainGradientToUnitNorm); return ret; } catch (Exception e) { throw new RuntimeException(e); } }
public DoubleMatrix free_energy(DoubleMatrix v_sample) { DoubleMatrix wv_hb = weights.mmul(v_sample.transpose()).addi(this.hbias.repmat(v_sample.rows, 1).transpose()); DoubleMatrix vb = v_sample.mmul(this.vbias.transpose()); DoubleMatrix hi = MatrixMath.sum(MatrixMath.log(MatrixMath.exp(wv_hb).addi(1.0)), 1); return hi.mul(-1.0).subi(vb); }
/** * Reconstruction entropy. This compares the similarity of two probability distributions, in this * case that would be the input and the reconstructed input with gaussian noise. This will account * for either regularization or none depending on the configuration. * * @return reconstruction error */ public double getReConstructionCrossEntropy() { DoubleMatrix preSigH = input.mmul(W).addRowVector(hBias); DoubleMatrix sigH = sigmoid(preSigH); DoubleMatrix preSigV = sigH.mmul(W.transpose()).addRowVector(vBias); DoubleMatrix sigV = sigmoid(preSigV); DoubleMatrix inner = input.mul(log(sigV)).add(oneMinus(input).mul(log(oneMinus(sigV)))); double ret = inner.rowSums().mean(); if (normalizeByInputRows) ret /= input.rows; return ret; }
private void modifyWeights( DoubleMatrix trainingExample, DoubleMatrix result, DoubleMatrix output, double learningFactor, double momentum, List<DoubleMatrix> previousModifications, EvaluationContext evalCtx) { List<DoubleMatrix> errors = countErrors(result, output); List<Layer> layers = getLayers(); evalCtx.resetContext(); Iterator<ActivationFunction> activationFunctionIter = evalCtx.getActivationFunction(); DoubleMatrix temporalResult = trainingExample; for (int i = 0; i < errors.size(); i++) { DoubleMatrix error = errors.get(i); int layerIndex = i + 1; Layer layer = layers.get(layerIndex); DoubleMatrix previousModification = previousModifications.get(i); ActivationFunction activationFunc = activationFunctionIter.next(); ActivationFunctionDerivative derivative = DerivativeFactory.getInstance().getDerivative(activationFunc); if (layer.includeBias()) { temporalResult = addBiasInput(temporalResult); } DoubleMatrix oldVal = temporalResult.dup(); temporalResult = layer.getWeights().mmul(temporalResult); temporalResult = activationFunc.eval(temporalResult); // dla kazdego neuronu w warstwie for (int j = 0; j < layer.getWeights().rows; j++) { double derVal = derivative.evaluate(temporalResult.get(j)); DoubleMatrix oldDelta = previousModification.getRow(j); DoubleMatrix delta = oldVal.mul(derVal).mul(learningFactor).mul(error.get(j)); delta = delta.transpose(); delta = delta.add(oldDelta.mul(momentum)); previousModification.putRow(j, delta); DoubleMatrix oldWeights = layer.getWeights().getRow(j); DoubleMatrix newWeights = oldWeights.add(delta); layer.getWeights().putRow(j, newWeights); } } }
public static void exportResult( TrueSolutionCTM trueSolutionCTM, TrueSolution[] trueSolution, TrueSolution[] trueSolution1, TrueSolution[] trueSolution2, int limite, String folder) { try { int bdry = 1; // *set boundary type int numSections = trueSolution[0].numSections; int numSections1 = trueSolution1[0].numSections; int numSections2 = trueSolution2[0].numSections; BufferedWriter[] writerSection = new BufferedWriter[numSections]; BufferedWriter[] writerSection1 = new BufferedWriter[numSections1]; BufferedWriter[] writerSection2 = new BufferedWriter[numSections2]; BufferedWriter[] LyapSection = new BufferedWriter[numSections]; BufferedWriter writerTrue; BufferedWriter writerConsensusTerm; BufferedWriter writerAvr; BufferedWriter writerAvr1; BufferedWriter writerAvr2; BufferedWriter Error; BufferedWriter Error1; BufferedWriter Error2; BufferedWriter Lyap; BufferedWriter[] Mode = new BufferedWriter[numSections]; BufferedWriter[] Mode1 = new BufferedWriter[numSections1]; BufferedWriter[] Mode2 = new BufferedWriter[numSections2]; BufferedWriter[] GammaWrite = new BufferedWriter[numSections]; BufferedWriter writerDisagreement; BufferedWriter writerDisagreement1; BufferedWriter writerDisagreement2; System.out.print("Beginning of simulation "); new File("results/" + folder).mkdir(); RoadModel[] roadModels = new RoadModel[numSections]; RoadModel[] roadModels1 = new RoadModel[numSections1]; RoadModel[] roadModels2 = new RoadModel[numSections2]; for (int i = 0; i < numSections; i++) { trueSolution[i].section = trueSolution[i].setSections(); } for (int i = 0; i < numSections1; i++) { trueSolution1[i].section = trueSolution1[i].setSections(); } for (int i = 0; i < numSections2; i++) { trueSolution2[i].section = trueSolution2[i].setSections(); } for (int i = 0; i < numSections; i++) { roadModels[i] = trueSolution[i].setRoadModels(); } for (int i = 0; i < numSections1; i++) { roadModels1[i] = trueSolution1[i].setRoadModels(); } for (int i = 0; i < numSections2; i++) { roadModels2[i] = trueSolution2[i].setRoadModels(); } writerTrue = new BufferedWriter(new FileWriter(new File("results/" + folder + "/" + "trueState.csv"))); writerConsensusTerm = new BufferedWriter( new FileWriter(new File("results/" + folder + "/" + "consensusTerm.csv"))); writerAvr = new BufferedWriter(new FileWriter(new File("results/" + folder + "/" + "writerAvr.csv"))); writerAvr1 = new BufferedWriter( new FileWriter(new File("results/" + folder + "/" + "writerAvr1.csv"))); writerAvr2 = new BufferedWriter( new FileWriter(new File("results/" + folder + "/" + "writerAvr2.csv"))); Error = new BufferedWriter(new FileWriter(new File("results/" + folder + "/" + "Error.csv"))); Error1 = new BufferedWriter(new FileWriter(new File("results/" + folder + "/" + "Error1.csv"))); Error2 = new BufferedWriter(new FileWriter(new File("results/" + folder + "/" + "Error2.csv"))); Lyap = new BufferedWriter(new FileWriter(new File("results/" + folder + "/" + "Lyapunov.csv"))); writerDisagreement = new BufferedWriter( new FileWriter(new File("results/" + folder + "/" + "Disagreement.csv"))); writerDisagreement1 = new BufferedWriter( new FileWriter(new File("results/" + folder + "/" + "Disagreement1.csv"))); writerDisagreement2 = new BufferedWriter( new FileWriter(new File("results/" + folder + "/" + "Disagreement2.csv"))); Estimation[] estimations = new Estimation[numSections]; Estimation[] estimations1 = new Estimation[numSections1]; Estimation[] estimations2 = new Estimation[numSections2]; for (int i = 0; i < numSections; i++) { estimations[i] = new Estimation( roadModels[i], "KCF", true); // *set the last variable to indicate if consensus is needed String S = "results/" + folder + "/" + roadModels[i].nameModel; writerSection[i] = new BufferedWriter(new FileWriter(new File(S + ".csv"))); Mode[i] = new BufferedWriter(new FileWriter(new File(S + "Mode.csv"))); GammaWrite[i] = new BufferedWriter(new FileWriter(new File(S + "Gamma.csv"))); LyapSection[i] = new BufferedWriter(new FileWriter(new File(S + "LyapSection.csv"))); } for (int i = 0; i < numSections1; i++) { estimations1[i] = new Estimation( roadModels1[i], "KCF", false); // *set the last variable to indicate if consensus is needed String S = "results/" + folder + "/" + roadModels1[i].nameModel; writerSection1[i] = new BufferedWriter(new FileWriter(new File(S + "_1.csv"))); Mode1[i] = new BufferedWriter(new FileWriter(new File(S + "Mode1.csv"))); } for (int i = 0; i < numSections2; i++) { estimations2[i] = new Estimation( roadModels2[i], "KCF", false); // *set the last variable to indicate if consensus is needed String S = "results/" + folder + "/" + roadModels2[i].nameModel; writerSection2[i] = new BufferedWriter(new FileWriter(new File(S + "_2.csv"))); Mode2[i] = new BufferedWriter(new FileWriter(new File(S + "Mode2.csv"))); } int overlap = roadModels[0].trueSolution.overlapsec; int cellsec = roadModels[0].section.cellsec; int overlap1 = roadModels1[0].trueSolution.overlapsec; int cellsec1 = roadModels1[0].section.cellsec; int overlap2 = roadModels2[0].trueSolution.overlapsec; int cellsec2 = roadModels2[0].section.cellsec; DoubleMatrix I1 = DoubleMatrix.zeros(roadModels[0].section.cellsec, overlap); for (int i = 0; i < overlap; i++) { I1.put(i, i, 1); } DoubleMatrix I2 = DoubleMatrix.zeros(roadModels[0].section.cellsec, overlap); for (int i = roadModels[0].section.cellsec - overlap; i < roadModels[0].section.cellsec; i++) { I2.put(i, i - (roadModels[0].section.cellsec - overlap), 1); } DoubleMatrix I123 = DoubleMatrix.concatVertically(I1.transpose(), I2.transpose()); DoubleMatrix H_hat = I2.transpose(); for (int i = 0; i < numSections - 2; i++) { H_hat = Concat.Diagonal(H_hat, I123); } H_hat = Concat.Diagonal(H_hat, I1.transpose()); DoubleMatrix L_tilde = DoubleMatrix.zeros((numSections - 1) * 2 * overlap, numSections * cellsec); for (int i = 0; i < numSections - 1; i++) { for (int j = 0; j < overlap; j++) { L_tilde.put(i * (2 * overlap) + j, (i + 1) * (cellsec) + j, 1); } for (int j = 0; j < overlap; j++) { L_tilde.put(i * (2 * overlap) + overlap + j, (i + 1) * (cellsec) - overlap + j, 1); } for (int j = 0; j < overlap; j++) { L_tilde.put(i * (2 * overlap) + j, (i + 1) * (cellsec) - overlap + j, -1); } for (int j = 0; j < overlap; j++) { L_tilde.put(i * (2 * overlap) + overlap + j, (i + 1) * (cellsec) + j, -1); } } DoubleMatrix[] S = new DoubleMatrix[numSections]; trueSolutionCTM.getMeasurement(); for (int i = 0; i < numSections; i++) { if (i == 0) { estimations[i].filter.Ltilde = L_tilde.getRange(i, i + overlap, i, i + 2 * cellsec); } else if (i == numSections - 1) { estimations[i].filter.Ltilde = L_tilde.getRange( (2 * (numSections - 1) - 1) * overlap, (2 * (numSections - 1) - 1) * overlap + overlap, (i - 1) * cellsec, (i + 1) * cellsec); } else { estimations[i].filter.Ltilde = L_tilde.getRange( (2 * i - 1) * overlap, (2 * i + 1) * overlap, (i - 1) * cellsec, (i + 2) * cellsec); } estimations[i].filter.I1 = I1; estimations[i].filter.I2 = I2; } double[] disagAvg = new double[3]; double[] errorAvg = new double[3]; double[] errAvgEntire = new double[3]; for (int k = 0; k < limite; k++) { System.out.println("time step=" + k); for (int i = 0; i < trueSolutionCTM.trueStatesCTM.getRows(); i++) { writerTrue.write(trueSolutionCTM.trueStatesCTM.get(i, 0) + ","); } writerTrue.write("\n"); DoubleMatrix[] mean = new DoubleMatrix[numSections]; DoubleMatrix[] mean_1 = new DoubleMatrix[numSections1]; DoubleMatrix[] mean_2 = new DoubleMatrix[numSections2]; double ErrorAvg = 0; double ErrorEntire = 0; double ErrorAvg1 = 0; double ErrorAvg2 = 0; double LyapAvg = 0; double[] errorAvgk = new double[3]; for (int i = 0; i < numSections; i++) { mean[i] = estimations[i].getDensityMean(); if (numSections == 1) { for (int j = 0; j < mean[i].length; j++) { writerAvr.write(mean[i].get(j) + ","); errorAvgk[0] = (mean[i].get(j) - trueSolutionCTM.trueStatesCTM.get(j, 0)) * (mean[i].get(j) - trueSolutionCTM.trueStatesCTM.get(j, 0)) + errorAvgk[0]; } } else { if (i == 0) { for (int j = 0; j < mean[i].length - overlap; j++) { writerAvr.write(mean[i].get(j) + ","); errorAvgk[0] = (mean[i].get(j) - trueSolutionCTM.trueStatesCTM.get(j, 0)) * (mean[i].get(j) - trueSolutionCTM.trueStatesCTM.get(j, 0)) + errorAvgk[0]; } } else if (i > 0 && i < numSections - 1) { for (int j = 0; j < overlap; j++) { writerAvr.write( (mean[i].get(j) + mean[i - 1].get(cellsec - overlap + j)) / 2 + ","); errorAvgk[0] = ((mean[i].get(j) + mean[i - 1].get(cellsec - overlap + j)) / 2 - trueSolutionCTM.trueStatesCTM.get( i * (mean[i].length - overlap) + j, 0)) * ((mean[i].get(j) + mean[i - 1].get(cellsec - overlap + j)) / 2 - trueSolutionCTM.trueStatesCTM.get( i * (mean[i].length - overlap) + j, 0)) + errorAvgk[0]; } for (int j = 0; j < mean[i].length - 2 * overlap; j++) { writerAvr.write(mean[i].get(overlap + j) + ","); errorAvgk[0] = (mean[i].get(overlap + j) - trueSolutionCTM.trueStatesCTM.get( i * (mean[i].length - overlap) + overlap + j, 0)) * (mean[i].get(overlap + j) - trueSolutionCTM.trueStatesCTM.get( i * (mean[i].length - overlap) + overlap + j, 0)) + errorAvgk[0]; } } else if (i == numSections - 1) { for (int j = 0; j < overlap; j++) { writerAvr.write( (mean[i].get(j) + mean[i - 1].get(cellsec - overlap + j)) / 2 + ","); errorAvgk[0] = ((mean[i].get(j) + mean[i - 1].get(cellsec - overlap + j)) / 2 - trueSolutionCTM.trueStatesCTM.get( i * (mean[i].length - overlap) + j, 0)) * ((mean[i].get(j) + mean[i - 1].get(cellsec - overlap + j)) / 2 - trueSolutionCTM.trueStatesCTM.get( i * (mean[i].length - overlap) + j, 0)) + errorAvgk[0]; } for (int j = 0; j < mean[i].length - overlap; j++) { writerAvr.write(mean[i].get(overlap + j) + ","); errorAvgk[0] = (mean[i].get(overlap + j) - trueSolutionCTM.trueStatesCTM.get( i * (mean[i].length - overlap) + overlap + j, 0)) * (mean[i].get(overlap + j) - trueSolutionCTM.trueStatesCTM.get( i * (mean[i].length - overlap) + overlap + j, 0)) + errorAvgk[0]; } } } for (int j = 0; j < mean[i].length; j++) { writerSection[i].write(mean[i].get(j) + ","); } double errorSection = (((mean[i].sub(trueSolution[i].trueStates)) .transpose() .mmul((mean[i].sub(trueSolution[i].trueStates)))) .get(0, 0)) / ((double) cellsec); Error.write(errorSection + ","); ErrorAvg = ErrorAvg + errorSection / ((double) numSections); writerSection[i].write("\n"); double lyapunovSection = ((mean[i].sub(trueSolution[i].trueStates)).transpose()) .mmul(InverseMatrix.invPoSym(estimations[i].filter.var)) .mmul(mean[i].sub(trueSolution[i].trueStates)) .get(0, 0); double tracevar = 0; for (int c = 0; c < mean[i].getRows(); c++) { tracevar = tracevar + estimations[i].filter.var.get(c, c); } LyapSection[i].write(lyapunovSection + ","); LyapSection[i].write(tracevar + "\n"); if (i != 0) { LyapAvg = LyapAvg + lyapunovSection / ((double) (numSections - 1)); } } errAvgEntire[0] = errorAvgk[0] / ((double) (limite * trueSolutionCTM.cells)) + errAvgEntire[0]; Error.write(ErrorAvg + ","); errorAvg[0] = errorAvg[0] + ErrorAvg / ((double) (limite)); Lyap.write(LyapAvg + "\n"); for (int i = 0; i < numSections1; i++) { mean_1[i] = estimations1[i].getDensityMean(); if (numSections1 == 1) { for (int j = 0; j < mean_1[i].length; j++) { writerAvr1.write(mean_1[i].get(j) + ","); errorAvgk[1] = (mean_1[i].get(j) - trueSolutionCTM.trueStatesCTM.get(j, 0)) * (mean_1[i].get(j) - trueSolutionCTM.trueStatesCTM.get(j, 0)) + errorAvgk[1]; } } else { if (i == 0) { for (int j = 0; j < mean_1[i].length - overlap1; j++) { writerAvr1.write(mean_1[i].get(j) + ","); errorAvgk[1] = (mean_1[i].get(j) - trueSolutionCTM.trueStatesCTM.get(j, 0)) * (mean_1[i].get(j) - trueSolutionCTM.trueStatesCTM.get(j, 0)) + errorAvgk[1]; } } else if (i > 0 && i < numSections1 - 1) { for (int j = 0; j < overlap1; j++) { writerAvr1.write( (mean_1[i].get(j) + mean_1[i - 1].get(cellsec1 - overlap1 + j)) / 2 + ","); errorAvgk[1] = ((mean_1[i].get(j) + mean_1[i - 1].get(cellsec1 - overlap1 + j)) / 2 - trueSolutionCTM.trueStatesCTM.get( i * (mean_1[i].length - overlap1) + j, 0)) * ((mean_1[i].get(j) + mean_1[i - 1].get(cellsec1 - overlap1 + j)) / 2 - trueSolutionCTM.trueStatesCTM.get( i * (mean_1[i].length - overlap1) + j, 0)) + errorAvgk[1]; } for (int j = 0; j < mean_1[i].length - 2 * overlap1; j++) { writerAvr1.write(mean_1[i].get(overlap1 + j) + ","); errorAvgk[1] = (mean_1[i].get(overlap1 + j) - trueSolutionCTM.trueStatesCTM.get( i * (mean_1[i].length - overlap1) + overlap1 + j, 0)) * (mean_1[i].get(overlap1 + j) - trueSolutionCTM.trueStatesCTM.get( i * (mean_1[i].length - overlap1) + overlap1 + j, 0)) + errorAvgk[1]; } } else if (i == numSections1 - 1) { for (int j = 0; j < overlap1; j++) { writerAvr1.write( (mean_1[i].get(j) + mean_1[i - 1].get(cellsec1 - overlap1 + j)) / 2 + ","); errorAvgk[1] = ((mean_1[i].get(j) + mean_1[i - 1].get(cellsec1 - overlap1 + j)) / 2 - trueSolutionCTM.trueStatesCTM.get( i * (mean_1[i].length - overlap1) + j, 0)) * ((mean_1[i].get(j) + mean_1[i - 1].get(cellsec1 - overlap1 + j)) / 2 - trueSolutionCTM.trueStatesCTM.get( i * (mean_1[i].length - overlap1) + j, 0)) + errorAvgk[1]; } for (int j = 0; j < mean_1[i].length - overlap1; j++) { writerAvr1.write(mean_1[i].get(overlap1 + j) + ","); errorAvgk[1] = (mean_1[i].get(overlap1 + j) - trueSolutionCTM.trueStatesCTM.get( i * (mean_1[i].length - overlap1) + overlap1 + j, 0)) * (mean_1[i].get(overlap1 + j) - trueSolutionCTM.trueStatesCTM.get( i * (mean_1[i].length - overlap1) + overlap1 + j, 0)) + errorAvgk[1]; } } } for (int j = 0; j < mean_1[i].length; j++) { writerSection1[i].write(mean_1[i].get(j) + ","); } double errorSection1 = (((mean_1[i].sub(trueSolution1[i].trueStates).transpose()) .mmul((mean_1[i].sub(trueSolution1[i].trueStates)))) .get(0, 0)) / ((double) cellsec1); Error1.write(errorSection1 + ","); ErrorAvg1 = ErrorAvg1 + errorSection1 / ((double) numSections1); writerSection1[i].write("\n"); } Error1.write(ErrorAvg1 + ","); errorAvg[1] = errorAvg[1] + ErrorAvg1 / ((double) (limite)); errAvgEntire[1] = errorAvgk[1] / ((double) (limite * trueSolutionCTM.cells)) + errAvgEntire[1]; for (int i = 0; i < numSections2; i++) { mean_2[i] = estimations2[i].getDensityMean(); if (numSections2 == 1) { for (int j = 0; j < mean_2[i].length; j++) { writerAvr2.write(mean_2[i].get(j) + ","); errorAvgk[2] = (mean_2[i].get(j) - trueSolutionCTM.trueStatesCTM.get(j, 0)) * (mean_2[i].get(j) - trueSolutionCTM.trueStatesCTM.get(j, 0)) + errorAvgk[2]; } } else { if (i == 0) { for (int j = 0; j < mean_2[i].length - overlap2; j++) { writerAvr2.write(mean_2[i].get(j) + ","); errorAvgk[2] = (mean_2[i].get(j) - trueSolutionCTM.trueStatesCTM.get(j, 0)) * (mean_2[i].get(j) - trueSolutionCTM.trueStatesCTM.get(j, 0)) + errorAvgk[2]; } } else if (i > 0 && i < numSections2 - 1) { for (int j = 0; j < overlap2; j++) { writerAvr2.write( (mean_2[i].get(j) + mean_2[i - 1].get(cellsec2 - overlap2 + j)) / 2 + ","); errorAvgk[2] = ((mean_2[i].get(j) + mean_2[i - 1].get(cellsec2 - overlap2 + j)) / 2 - trueSolutionCTM.trueStatesCTM.get( i * (mean_2[i].length - overlap2) + j, 0)) * ((mean_2[i].get(j) + mean_2[i - 1].get(cellsec2 - overlap2 + j)) / 2 - trueSolutionCTM.trueStatesCTM.get( i * (mean_2[i].length - overlap2) + j, 0)) + errorAvgk[2]; } for (int j = 0; j < mean_2[i].length - 2 * overlap2; j++) { writerAvr2.write(mean_2[i].get(overlap2 + j) + ","); errorAvgk[2] = (mean_2[i].get(overlap2 + j) - trueSolutionCTM.trueStatesCTM.get( i * (mean_2[i].length - overlap2) + overlap2 + j, 0)) * (mean_2[i].get(overlap2 + j) - trueSolutionCTM.trueStatesCTM.get( i * (mean_2[i].length - overlap2) + overlap2 + j, 0)) + errorAvgk[2]; } } else if (i == numSections2 - 1) { for (int j = 0; j < overlap2; j++) { writerAvr2.write( (mean_2[i].get(j) + mean_2[i - 1].get(cellsec2 - overlap2 + j)) / 2 + ","); errorAvgk[2] = ((mean_2[i].get(j) + mean_2[i - 1].get(cellsec2 - overlap2 + j)) / 2 - trueSolutionCTM.trueStatesCTM.get( i * (mean_2[i].length - overlap2) + j, 0)) * ((mean_2[i].get(j) + mean_2[i - 1].get(cellsec2 - overlap2 + j)) / 2 - trueSolutionCTM.trueStatesCTM.get( i * (mean_2[i].length - overlap2) + j, 0)) + errorAvgk[2]; } for (int j = 0; j < mean_2[i].length - overlap2; j++) { writerAvr2.write(mean_2[i].get(overlap2 + j) + ","); errorAvgk[2] = (mean_2[i].get(overlap2 + j) - trueSolutionCTM.trueStatesCTM.get( i * (mean_2[i].length - overlap2) + overlap2 + j, 0)) * (mean_2[i].get(overlap2 + j) - trueSolutionCTM.trueStatesCTM.get( i * (mean_2[i].length - overlap2) + overlap2 + j, 0)) + errorAvgk[2]; } } } for (int j = 0; j < mean_2[i].length; j++) { writerSection2[i].write(mean_2[i].get(j) + ","); } double errorSection2 = (((mean_2[i].sub(trueSolution2[i].trueStates)) .transpose() .mmul((mean_2[i].sub(trueSolution2[i].trueStates)))) .get(0, 0)) / ((double) (cellsec2)); Error2.write(errorSection2 + ","); ErrorAvg2 = ErrorAvg2 + errorSection2 / ((double) numSections2); writerSection2[i].write("\n"); } Error2.write(ErrorAvg2 + ","); errorAvg[2] = errorAvg[2] + ErrorAvg2 / ((double) (limite)); errAvgEntire[2] = errorAvgk[2] / ((double) (limite * trueSolutionCTM.cells)) + errAvgEntire[2]; writerAvr.write("\n"); writerAvr1.write("\n"); writerAvr2.write("\n"); Error.write("\n"); Error1.write("\n"); Error2.write("\n"); Section[] secs = new Section[numSections]; Section[] secs1 = new Section[numSections1]; Section[] secs2 = new Section[numSections2]; for (int i = 0; i < numSections; i++) { secs[i] = trueSolution[i].setSections(); // every step, update mode } for (int i = 0; i < numSections1; i++) { secs1[i] = trueSolution1[i].setSections(); } for (int i = 0; i < numSections2; i++) { secs2[i] = trueSolution2[i].setSections(); } for (int i = 0; i < numSections; i++) { secs[i].Estimates = mean[i]; } for (int i = 0; i < numSections1; i++) { secs1[i].Estimates = mean_1[i]; } for (int i = 0; i < numSections2; i++) { secs2[i].Estimates = mean_2[i]; } for (int i = 0; i < numSections; i++) { if (secs[i].getClass().getSimpleName().equals("FC")) { secs[i].getwavefrontfromEstimation(); if (secs[i].wavefront < 0) { secs[i].wavefront = 0; } if (secs[i].wavefront <= cellsec - 2) { if (trueSolution[i].flux(secs[i].Estimates.get(secs[i].wavefront, 0)) - trueSolution[i].flux(secs[i].Estimates.get(secs[i].wavefront + 1, 0)) > 0) { secs[i].wavedirection = 0; } else { secs[i].wavedirection = 1; } } else if (secs[i].wavefront == cellsec - 1) { if (secs[i].index != numSections - 1) { if (trueSolution[i].flux(secs[i].Estimates.get(secs[i].wavefront, 0)) - trueSolution[i + 1].flux(secs[i].Estimates.get(overlap, 0)) > 0) { secs[i].wavedirection = 0; } else { secs[i].wavedirection = 1; } } else { secs[i].wavedirection = 0; } } secs[i].getModelA(); secs[i].getModelB1(); secs[i].getModelB2(); } else if (secs[i].getClass().getSimpleName().equals("CF")) { secs[i].getwavefrontfromEstimation(); secs[i].getModelA(); secs[i].getModelB1(); secs[i].getModelB2(); } } for (int i = 0; i < numSections1; i++) { if (secs1[i].getClass().getSimpleName().equals("FC")) { secs1[i].getwavefrontfromEstimation(); if (secs1[i].wavefront < 0) { secs1[i].wavefront = 0; } if (secs1[i].wavefront <= cellsec1 - 2) { if (trueSolution1[i].flux(secs1[i].Estimates.get(secs1[i].wavefront, 0)) - trueSolution1[i].flux(secs1[i].Estimates.get(secs1[i].wavefront + 1, 0)) > 0) { secs1[i].wavedirection = 0; } else { secs1[i].wavedirection = 1; } } else if (secs1[i].wavefront == cellsec1 - 1) { if (secs1[i].index != numSections1 - 1) { if (trueSolution1[i].flux(secs1[i].Estimates.get(secs1[i].wavefront, 0)) - trueSolution1[i + 1].flux(secs1[i].Estimates.get(overlap1, 0)) > 0) { secs1[i].wavedirection = 0; } else { secs1[i].wavedirection = 1; } } else { secs1[i].wavedirection = 0; } } secs1[i].getModelA(); secs1[i].getModelB1(); secs1[i].getModelB2(); } else if (secs1[i].getClass().getSimpleName().equals("CF")) { secs1[i].getwavefrontfromEstimation(); secs1[i].getModelA(); secs1[i].getModelB1(); secs1[i].getModelB2(); } } for (int i = 0; i < numSections2; i++) { if (secs2[i].getClass().getSimpleName().equals("FC")) { secs2[i].getwavefrontfromEstimation(); if (secs2[i].wavefront < 0) { secs2[i].wavefront = 0; } if (secs2[i].wavefront <= cellsec2 - 2) { if (trueSolution2[i].flux(secs2[i].Estimates.get(secs2[i].wavefront, 0)) - trueSolution2[i].flux(secs2[i].Estimates.get(secs2[i].wavefront + 1, 0)) > 0) { secs2[i].wavedirection = 0; } else { secs2[i].wavedirection = 1; } } else if (secs2[i].wavefront == cellsec2 - 1) { if (secs2[i].index != numSections2 - 1) { if (trueSolution2[i].flux(secs2[i].Estimates.get(secs2[i].wavefront, 0)) - trueSolution2[i + 1].flux(secs2[i].Estimates.get(overlap2, 0)) > 0) { secs2[i].wavedirection = 0; } else { secs2[i].wavedirection = 1; } } else { secs2[i].wavedirection = 0; } } secs2[i].getModelA(); secs2[i].getModelB1(); secs2[i].getModelB2(); } else if (secs2[i].getClass().getSimpleName().equals("CF")) { secs2[i].getwavefrontfromEstimation(); secs2[i].getModelA(); secs2[i].getModelB1(); secs2[i].getModelB2(); } } for (int i = 0; i < numSections; i++) { estimations[i].setSection(secs[i]); } for (int i = 0; i < numSections1; i++) { estimations1[i].setSection(secs1[i]); } for (int i = 0; i < numSections2; i++) { estimations2[i].setSection(secs2[i]); } for (int i = 0; i < numSections; i++) { trueSolution[i].section = secs[i]; } for (int i = 0; i < numSections1; i++) { trueSolution1[i].section = secs1[i]; } for (int i = 0; i < numSections2; i++) { trueSolution2[i].section = secs2[i]; } for (int i = 0; i < numSections; i++) { if (i == 0) { GammaWrite[i].write(estimations[i].filter.gammaStar + ","); } else if (i == numSections - 1) { GammaWrite[i].write(estimations[i].filter.gammaStar + ","); } else { GammaWrite[i].write(estimations[i].filter.gammaStar + ","); } GammaWrite[i].write("\n"); } for (int i = 0; i < numSections; i++) { Mode[i].write(estimations[i].roadModel.section.getClass().getSimpleName() + ","); Mode[i].write( estimations[i].roadModel.section.wavefront + ","); // estimated location of shock Mode[i].write( estimations[i].roadModel.section._wavefront + ","); // true location of shock Mode[i].write(estimations[i].roadModel.section.wavedirection + ","); Mode[i].write(estimations[i].roadModel.section._wavedirection + ","); Mode[i].write("\n"); } for (int i = 0; i < numSections1; i++) { Mode1[i].write(estimations1[i].roadModel.section.getClass().getSimpleName() + ","); Mode1[i].write(estimations1[i].roadModel.section.wavefront + ","); Mode1[i].write(estimations1[i].roadModel.section._wavefront + ","); Mode1[i].write(estimations1[i].roadModel.section.wavedirection + ","); Mode1[i].write(estimations1[i].roadModel.section._wavedirection + ","); Mode1[i].write("\n"); } for (int i = 0; i < numSections2; i++) { Mode2[i].write(estimations2[i].roadModel.section.getClass().getSimpleName() + ","); Mode2[i].write(estimations2[i].roadModel.section.wavefront + ","); Mode2[i].write(estimations2[i].roadModel.section._wavefront + ","); Mode2[i].write(estimations2[i].roadModel.section.wavedirection + ","); Mode2[i].write(estimations2[i].roadModel.section._wavedirection + ","); Mode2[i].write("\n"); } for (int i = 0; i < numSections; i++) { // trueSolution[i].updateMeasurement(); // roadModels[i].updateMeasurement(); estimations[i].filter.getNewParametersFromModel(); } for (int i = 0; i < numSections1; i++) { // trueSolution[i].updateMeasurement(); // roadModels[i].updateMeasurement(); estimations1[i].filter.getNewParametersFromModel(); } for (int i = 0; i < numSections2; i++) { // trueSolution[i].updateMeasurement(); // roadModels[i].updateMeasurement(); estimations2[i].filter.getNewParametersFromModel(); } for (int i = 0; i < numSections; i++) { S[i] = (estimations[i].filter.measure.transpose()) .mmul(InverseMatrix.invPoSym(estimations[i].filter.measureVar)) .mmul(estimations[i].filter.measure); } double disag = 0; for (int i = 0; i < numSections - 1; i++) { disag = disag + (((mean[i] .getRange(cellsec - overlap, cellsec, 0, 1) .sub(mean[i + 1].getRange(0, overlap, 0, 1))) .transpose() .mmul( mean[i] .getRange(cellsec - overlap, cellsec, 0, 1) .sub(mean[i + 1].getRange(0, overlap, 0, 1)))) .get(0, 0)) / ((double) overlap); } disag = disag / ((double) (numSections - 1)); writerDisagreement.write(disag + ","); disagAvg[0] = disagAvg[0] + disag / ((double) limite); for (int i = 0; i < numSections - 1; i++) { writerDisagreement.write( ((mean[i] .getRange(cellsec - overlap, cellsec, 0, 1) .sub(mean[i + 1].getRange(0, overlap, 0, 1))) .transpose() .mmul( mean[i] .getRange(cellsec - overlap, cellsec, 0, 1) .sub(mean[i + 1].getRange(0, overlap, 0, 1)))) .get(0, 0) + ","); } writerDisagreement.write("\n"); double disag1 = 0; for (int i = 0; i < numSections1 - 1; i++) { disag1 = disag1 + (((mean_1[i] .getRange(cellsec1 - overlap1, cellsec1, 0, 1) .sub(mean_1[i + 1].getRange(0, overlap1, 0, 1))) .transpose() .mmul( mean_1[i] .getRange(cellsec1 - overlap1, cellsec1, 0, 1) .sub(mean_1[i + 1].getRange(0, overlap1, 0, 1)))) .get(0, 0)) / ((double) overlap1); } disag1 = disag1 / ((double) (numSections1 - 1)); writerDisagreement1.write(disag1 + ","); disagAvg[1] = disagAvg[1] + disag1 / ((double) limite); for (int i = 0; i < numSections1 - 1; i++) { writerDisagreement1.write( ((mean_1[i] .getRange(cellsec1 - overlap1, cellsec1, 0, 1) .sub(mean_1[i + 1].getRange(0, overlap1, 0, 1))) .transpose() .mmul( mean_1[i] .getRange(cellsec1 - overlap1, cellsec1, 0, 1) .sub(mean_1[i + 1].getRange(0, overlap1, 0, 1)))) .get(0, 0) + ","); } writerDisagreement1.write("\n"); double disag2 = 0; for (int i = 0; i < numSections2 - 1; i++) { disag2 = disag2 + (((mean_2[i] .getRange(cellsec2 - overlap2, cellsec2, 0, 1) .sub(mean_2[i + 1].getRange(0, overlap2, 0, 1))) .transpose() .mmul( mean_2[i] .getRange(cellsec2 - overlap2, cellsec2, 0, 1) .sub(mean_2[i + 1].getRange(0, overlap2, 0, 1)))) .get(0, 0)) / ((double) overlap2); } disag2 = disag2 / ((double) (numSections2 - 1)); writerDisagreement2.write(disag2 + ","); disagAvg[2] = disagAvg[2] + disag2 / ((double) limite); for (int i = 0; i < numSections2 - 1; i++) { writerDisagreement2.write( ((mean_2[i] .getRange(cellsec2 - overlap2, cellsec2, 0, 1) .sub(mean_2[i + 1].getRange(0, overlap2, 0, 1))) .transpose() .mmul( mean_2[i] .getRange(cellsec2 - overlap2, cellsec2, 0, 1) .sub(mean_2[i + 1].getRange(0, overlap2, 0, 1)))) .get(0, 0) + ","); } writerDisagreement2.write("\n"); trueSolutionCTM.update(); // **start defining boundary condition int cells = trueSolutionCTM.trueStatesCTMPrior.getRows(); if (bdry == 0) { double inflow = trueSolutionCTM.receiving(trueSolutionCTM.trueStatesCTMPrior.get(0, 0)); double outflow = trueSolutionCTM.sending(trueSolutionCTM.trueStatesCTMPrior.get(cells - 1, 0)); double upOut = trueSolutionCTM.computeFlux( trueSolutionCTM.trueStatesCTMPrior.get(0, 0), trueSolutionCTM.trueStatesCTMPrior.get(1, 0)); trueSolutionCTM.trueStatesCTM.put( 0, 0, trueSolutionCTM.trueStatesCTMPrior.get(0, 0) + (trueSolutionCTM.dt / trueSolutionCTM.dx) * (inflow - upOut)); double downIn = trueSolutionCTM.computeFlux( trueSolutionCTM.trueStatesCTMPrior.get(cells - 2, 0), trueSolutionCTM.trueStatesCTMPrior.get(cells - 1, 0)); trueSolutionCTM.trueStatesCTM.put( cells - 1, 0, trueSolutionCTM.trueStatesCTMPrior.get(cells - 1, 0) + (trueSolutionCTM.dt / trueSolutionCTM.dx) * (downIn - outflow)); } else if (bdry == 1) { double inflow = 0.1125 + 0.1125 * Math.sin(k * (Math.PI / 4000) + (Math.PI)); if (inflow > trueSolutionCTM.receiving(trueSolutionCTM.trueStatesCTMPrior.get(0, 0))) { inflow = trueSolutionCTM.receiving(trueSolutionCTM.trueStatesCTMPrior.get(0, 0)); } double outflow = trueSolutionCTM.receiving(trueSolutionCTM.trueStatesCTMPrior.get(cells - 1, 0)); // double outflow=0.1125+0.1125*Math.sin(k*(Math.PI/1000)); // if // (outflow>trueSolution[numSections-1].sending(trueSolution[numSections-1].trueStatesPrior.get(cellsec-1,0))){ // // outflow=trueSolution[numSections-1].sending(trueSolution[numSections-1].trueStatesPrior.get(cellsec-1,0)); // } double upOut = trueSolutionCTM.computeFlux( trueSolutionCTM.trueStatesCTMPrior.get(0, 0), trueSolutionCTM.trueStatesCTMPrior.get(1, 0)); trueSolutionCTM.trueStatesCTM.put( 0, 0, trueSolutionCTM.trueStatesCTMPrior.get(0, 0) + (trueSolutionCTM.dt / trueSolutionCTM.dx) * (inflow - upOut)); double downIn = trueSolutionCTM.computeFlux( trueSolutionCTM.trueStatesCTMPrior.get(cells - 2, 0), trueSolutionCTM.trueStatesCTMPrior.get(cells - 1, 0)); trueSolutionCTM.trueStatesCTM.put( cells - 1, 0, trueSolutionCTM.trueStatesCTMPrior.get(cells - 1, 0) + (trueSolutionCTM.dt / trueSolutionCTM.dx) * (downIn - outflow)); } else { double inflow = 0.1125 + 0.1125 * Math.sin(k * (Math.PI / 8000) + (Math.PI)); if (inflow > trueSolutionCTM.receiving(trueSolutionCTM.trueStatesCTMPrior.get(0, 0))) { inflow = trueSolutionCTM.receiving(trueSolutionCTM.trueStatesCTMPrior.get(0, 0)); } double outflow = trueSolutionCTM.receiving(trueSolutionCTM.trueStatesCTMPrior.get(cells - 1, 0)); // double outflow=0.1125+0.1125*Math.sin(k*(Math.PI/1000)); // if // (outflow>trueSolution[numSections-1].sending(trueSolution[numSections-1].trueStatesPrior.get(cellsec-1,0))){ // // outflow=trueSolution[numSections-1].sending(trueSolution[numSections-1].trueStatesPrior.get(cellsec-1,0)); // } double upOut = trueSolutionCTM.computeFlux( trueSolutionCTM.trueStatesCTMPrior.get(0, 0), trueSolutionCTM.trueStatesCTMPrior.get(1, 0)); trueSolutionCTM.trueStatesCTM.put( 0, 0, trueSolutionCTM.trueStatesCTMPrior.get(0, 0) + (trueSolutionCTM.dt / trueSolutionCTM.dx) * (inflow - upOut)); double downIn = trueSolutionCTM.computeFlux( trueSolutionCTM.trueStatesCTMPrior.get(cells - 2, 0), trueSolutionCTM.trueStatesCTMPrior.get(cells - 1, 0)); trueSolutionCTM.trueStatesCTM.put( cells - 1, 0, trueSolutionCTM.trueStatesCTMPrior.get(cells - 1, 0) + (trueSolutionCTM.dt / trueSolutionCTM.dx) * (downIn - outflow)); // *define boundary conditions here } // **end defining boundary condition trueSolutionCTM.getMeasurement(); for (int i = 0; i < numSections; i++) { trueSolution[i].update(); } for (int i = 0; i < numSections; i++) { trueSolution[i].updatemeasurement(); } for (int i = 0; i < numSections1; i++) { trueSolution1[i].update(); } for (int i = 0; i < numSections1; i++) { trueSolution1[i].updatemeasurement(); } for (int i = 0; i < numSections2; i++) { trueSolution2[i].update(); } for (int i = 0; i < numSections2; i++) { trueSolution2[i].updatemeasurement(); } for (int i = 0; i < numSections; i++) { estimations[i].nextStepWithoutUpdateTrue(); } for (int i = 0; i < numSections1; i++) { estimations1[i].nextStepWithoutUpdateTrue(); } for (int i = 0; i < numSections2; i++) { estimations2[i].nextStepWithoutUpdateTrue(); } if (estimations[0].consensus) { for (int i = 0; i < numSections; i++) { estimations[i].filter.ComputeEig(); } for (int i = 0; i < numSections; i++) { if (i == 0) { estimations[i].filter.ComputeGammaStar2(estimations[1].filter); } else if (i == numSections - 1) { estimations[i].filter.ComputeGammaStar1(estimations[numSections - 2].filter); } else { estimations[i].filter.ComputeGammaStar( estimations[i - 1].filter, estimations[i + 1].filter); } } DoubleMatrix[] mean1 = new DoubleMatrix[numSections]; mean1[0] = estimations[0].filter.Consensus2(estimations[1].filter); writerConsensusTerm.write(estimations[0].filter.scaling.get(0) + ","); writerConsensusTerm.write(estimations[0].filter.scaling.get(1) + ","); for (int i = 1; i < numSections - 1; i++) { mean1[i] = estimations[i].filter.Consensus( estimations[i - 1].filter, estimations[i + 1].filter); writerConsensusTerm.write(estimations[i].filter.scaling.get(0) + ","); writerConsensusTerm.write(estimations[i].filter.scaling.get(1) + ","); } mean1[numSections - 1] = estimations[numSections - 1].filter.Consensus1(estimations[numSections - 2].filter); writerConsensusTerm.write(estimations[numSections - 1].filter.scaling.get(0) + ","); writerConsensusTerm.write(estimations[numSections - 1].filter.scaling.get(1) + ","); writerConsensusTerm.write("\n"); for (int i = 0; i < numSections; i++) { estimations[i].filter.mean = mean1[i]; } } } for (int i = 0; i < numSections; i++) { writerSection[i].flush(); writerSection[i].close(); Mode[i].flush(); Mode[i].close(); LyapSection[i].flush(); LyapSection[i].close(); GammaWrite[i].flush(); GammaWrite[i].close(); } for (int i = 0; i < numSections1; i++) { writerSection1[i].flush(); writerSection1[i].close(); Mode1[i].flush(); Mode1[i].close(); } for (int i = 0; i < numSections2; i++) { writerSection2[i].flush(); writerSection2[i].close(); Mode2[i].flush(); Mode2[i].close(); } writerAvr.flush(); writerAvr.close(); writerConsensusTerm.flush(); writerConsensusTerm.close(); writerAvr1.flush(); writerAvr1.close(); writerAvr2.flush(); writerAvr2.close(); Error.flush(); Error.close(); Error1.flush(); Error1.close(); Error2.flush(); Error2.close(); Lyap.flush(); Lyap.close(); writerTrue.flush(); writerTrue.close(); writerDisagreement.flush(); writerDisagreement.close(); writerDisagreement1.flush(); writerDisagreement1.close(); writerDisagreement2.flush(); writerDisagreement2.close(); System.out.print("Disag_avg_DLKCF=" + disagAvg[0] + "\n"); System.out.print("Disag_avg_DLKF=" + disagAvg[1] + "\n"); System.out.print("Disag_avg_LKF=" + disagAvg[2] + "\n"); System.out.print("Err_avg_DLKCF=" + errorAvg[0] + "\n"); System.out.print("Err_avg_DLKF=" + errorAvg[1] + "\n"); System.out.print("Err_avg_LKF=" + errorAvg[2] + "\n"); System.out.println(" End"); } catch (Exception e) { e.printStackTrace(); } }
public static double[] polyFit2D( final DoubleMatrix x, final DoubleMatrix y, final DoubleMatrix z, final int degree) throws IllegalArgumentException { logger.setLevel(Level.INFO); if (x.length != y.length || !x.isVector() || !y.isVector()) { logger.error("polyfit: require same size vectors."); throw new IllegalArgumentException("polyfit: require same size vectors."); } final int numOfObs = x.length; final int numOfUnkn = numberOfCoefficients(degree) + 1; DoubleMatrix A = new DoubleMatrix(numOfObs, numOfUnkn); // designmatrix DoubleMatrix mul; /** Set up design-matrix */ for (int p = 0; p <= degree; p++) { for (int q = 0; q <= p; q++) { mul = pow(y, (p - q)).mul(pow(x, q)); if (q == 0 && p == 0) { A = mul; } else { A = DoubleMatrix.concatHorizontally(A, mul); } } } // Fit polynomial logger.debug("Solving lin. system of equations with Cholesky."); DoubleMatrix N = A.transpose().mmul(A); DoubleMatrix rhs = A.transpose().mmul(z); // solution seems to be OK up to 10^-09! rhs = Solve.solveSymmetric(N, rhs); DoubleMatrix Qx_hat = Solve.solveSymmetric(N, DoubleMatrix.eye(N.getRows())); double maxDeviation = (N.mmul(Qx_hat).sub(DoubleMatrix.eye(Qx_hat.rows))).normmax(); logger.debug("polyfit orbit: max(abs(N*inv(N)-I)) = " + maxDeviation); // ___ report max error... (seems sometimes this can be extremely large) ___ if (maxDeviation > 1e-6) { logger.warn("polyfit orbit: max(abs(N*inv(N)-I)) = {}", maxDeviation); logger.warn("polyfit orbit interpolation unstable!"); } // work out residuals DoubleMatrix y_hat = A.mmul(rhs); DoubleMatrix e_hat = y.sub(y_hat); if (e_hat.normmax() > 0.02) { logger.warn( "WARNING: Max. polyFit2D approximation error at datapoints (x,y,or z?): {}", e_hat.normmax()); } else { logger.info( "Max. polyFit2D approximation error at datapoints (x,y,or z?): {}", e_hat.normmax()); } if (logger.isDebugEnabled()) { logger.debug("REPORTING POLYFIT LEAST SQUARES ERRORS"); logger.debug(" time \t\t\t y \t\t\t yhat \t\t\t ehat"); for (int i = 0; i < numOfObs; i++) { logger.debug( " (" + x.get(i) + "," + y.get(i) + ") :" + "\t" + y.get(i) + "\t" + y_hat.get(i) + "\t" + e_hat.get(i)); } } return rhs.toArray(); }
public static double[] polyFit(DoubleMatrix t, DoubleMatrix y, final int degree) throws IllegalArgumentException { logger.setLevel(Level.INFO); if (t.length != y.length || !t.isVector() || !y.isVector()) { logger.error("polyfit: require same size vectors."); throw new IllegalArgumentException("polyfit: require same size vectors."); } // Normalize _posting_ for numerical reasons final int numOfPoints = t.length; // Check redundancy final int numOfUnknowns = degree + 1; logger.debug("Degree of interpolating polynomial: {}", degree); logger.debug("Number of unknowns: {}", numOfUnknowns); logger.debug("Number of data points: {}", numOfPoints); if (numOfPoints < numOfUnknowns) { logger.error("Number of points is smaller than parameters solved for."); throw new IllegalArgumentException("Number of points is smaller than parameters solved for."); } // Set up system of equations to solve coeff :: Design matrix logger.debug("Setting up linear system of equations"); DoubleMatrix A = new DoubleMatrix(numOfPoints, numOfUnknowns); // work with columns for (int j = 0; j <= degree; j++) { A.putColumn(j, pow(t, j)); } // Fit polynomial through computed vector of phases logger.debug("Solving lin. system of equations with Cholesky."); DoubleMatrix N = A.transpose().mmul(A); DoubleMatrix rhs = A.transpose().mmul(y); // solution seems to be OK up to 10^-09! DoubleMatrix x = Solve.solveSymmetric(N, rhs); DoubleMatrix Qx_hat = Solve.solveSymmetric(N, DoubleMatrix.eye(N.getRows())); double maxDeviation = (N.mmul(Qx_hat).sub(DoubleMatrix.eye(Qx_hat.rows))).normmax(); logger.debug("polyfit orbit: max(abs(N*inv(N)-I)) = " + maxDeviation); // ___ report max error... (seems sometimes this can be extremely large) ___ if (maxDeviation > 1e-6) { logger.warn("polyfit orbit: max(abs(N*inv(N)-I)) = {}", maxDeviation); logger.warn("polyfit orbit interpolation unstable!"); } // work out residuals DoubleMatrix y_hat = A.mmul(x); DoubleMatrix e_hat = y.sub(y_hat); // 0.05 is already 1 wavelength! (?) if (e_hat.normmax() > 0.02) { logger.warn( "WARNING: Max. approximation error at datapoints (x,y,or z?): {}", e_hat.normmax()); } else { logger.debug("Max. approximation error at datapoints (x,y,or z?): {}", e_hat.normmax()); } if (logger.isDebugEnabled()) { logger.debug("REPORTING POLYFIT LEAST SQUARES ERRORS"); logger.debug(" time \t\t\t y \t\t\t yhat \t\t\t ehat"); for (int i = 0; i < numOfPoints; i++) { logger.debug(" " + t.get(i) + "\t" + y.get(i) + "\t" + y_hat.get(i) + "\t" + e_hat.get(i)); } for (int i = 0; i < numOfPoints - 1; i++) { // ___ check if dt is constant, not necessary for me, but may ___ // ___ signal error in header data of SLC image ___ double dt = t.get(i + 1) - t.get(i); logger.debug("Time step between point " + i + 1 + " and " + i + "= " + dt); if (Math.abs(dt - (t.get(1) - t.get(0))) > 0.001) // 1ms of difference we allow... logger.warn("WARNING: Orbit: data does not have equidistant time interval?"); } } return x.toArray(); }
public DoubleMatrix propup(DoubleMatrix v) { return MatrixMath.sigmoid( this.weights.mmul(v.transpose()).transpose().addi(hbias.repmat(v_data.rows, 1))); }
/** * 测试模型 * * @param filename */ public void test_model(String filename, int testCount) { try { this.test_data_Count = testCount; test_set = loadMatrix(filename, test_data_Count, totalColCount); } catch (Exception e) { e.printStackTrace(); } printInfo("测试数据载入完毕…………"); // 测试数据输入矩阵初始化 横向即每列为一组输入 testP = new DoubleMatrix(NumberofInputNeurons, test_data_Count); // 测试机输出矩阵 横向 testT = new DoubleMatrix(NumberofOutputNeurons, test_data_Count); // 初始化 for (int i = 0; i < test_data_Count; i++) { for (int j = 0; j < totalColCount; j++) { if (j < NumberofInputNeurons) { // 输入部分 testP.put(j, i, test_set.get(i, j)); } else { testT.put(j - NumberofInputNeurons, i, test_set.get(i, j)); } } } // end 初始化 printInfo("测试数据初始化完毕…………"); long start_time_test = System.currentTimeMillis(); // 计算输入权重与阈值部分 DoubleMatrix tmpH = new DoubleMatrix(NumberofHiddenNeurons, test_data_Count); tmpH = InputWeight.mmul(testP); printInfo("权重计算完毕…………"); // 加阈值 // 注意横向问题 for (int i = 0; i < NumberofHiddenNeurons; i++) { for (int j = 0; j < test_data_Count; j++) { tmpH.put(i, j, tmpH.get(i, j) + BiasofHiddenNeurons.get(i, 0)); } } printInfo("阈值计算完毕…………"); // 输出矩阵 DoubleMatrix H = new DoubleMatrix(NumberofHiddenNeurons, test_data_Count); if (func.startsWith("sig")) { for (int i = 0; i < NumberofHiddenNeurons; i++) { for (int j = 0; j < test_data_Count; j++) { double tmp = tmpH.get(i, j); tmp = 1.0f / (1 + Math.exp(-tmp)); H.put(i, j, tmp); } } } else { throw new IllegalArgumentException("不支持的激活函数类型"); } printInfo("激活函数计算完毕…………"); // 将H转置 DoubleMatrix Ht = H.transpose(); // 测试误差 DoubleMatrix Yt = new DoubleMatrix(test_data_Count, NumberofOutputNeurons); Yt = Ht.mmul(OutputWeight); printInfo("测试完毕…………"); long end_time_test = System.currentTimeMillis(); TestingTime = (end_time_test - start_time_test) * 1.0f / 1000; double MSE = 0; printInfo("共有" + NumberofOutputNeurons + "个输出值"); for (int out = 0; out < NumberofOutputNeurons; out++) { printInfo("计算第" + (out + 1) + "个输出值"); double mseTem = 0.0; for (int i = 0; i < test_data_Count; i++) { mseTem += (Yt.get(i, out) - testT.get(out, i)) * (Yt.get(i, out) - testT.get(out, i)); } printInfo( "第" + NumberofOutputNeurons + "个输出值计算完毕,MSE=" + Math.sqrt(mseTem / train_data_Count)); MSE += mseTem; } TestingAccuracy = Math.sqrt(MSE / test_data_Count); printInfo("计算MSE完毕…………"); }
/** 训练模型 */ private void train() { printInfo("训练数据载入完毕,开始训练模型………………训练数据规模" + train_set.rows + "x" + train_set.columns); // 输入矩阵初始化 横向即每列为一组输入 /* * 4 8 …… N * * 1 5 …… N * 2 6 …… N * 3 7 …… N */ P = new DoubleMatrix(NumberofInputNeurons, train_data_Count); // 测试机输出矩阵 横向 T = new DoubleMatrix(NumberofOutputNeurons, train_data_Count); // 初始化 for (int i = 0; i < train_data_Count; i++) { for (int j = 0; j < totalColCount; j++) { if (j < NumberofInputNeurons) { // 输入部分 P.put(j, i, train_set.get(i, j)); } else { T.put(j - NumberofInputNeurons, i, train_set.get(i, j)); } } } printInfo("训练矩阵初始化完毕………………开始隐含层神经元权重与阈值"); // end 初始化 long start_time_train = System.currentTimeMillis(); // 随机初始化输入权值矩阵 // 行数为隐含层神经元个数,列数为输入层神经元 /** * W11 W12 W13(第一个隐含神经元对应各输入的权值) W21 W22 W23(第二个隐含神经元对应各输入的权值) ………………………………(第N个隐含神经元对应各输入的权值) */ InputWeight = DoubleMatrix.rand(NumberofHiddenNeurons, NumberofInputNeurons); // 初始化阈值 BiasofHiddenNeurons = DoubleMatrix.rand(NumberofHiddenNeurons, 1); printInfo("隐含层神经元权重与阈值初始化完毕………………开始计算输入权重"); DoubleMatrix tmpH = new DoubleMatrix(NumberofHiddenNeurons, train_data_Count); tmpH = InputWeight.mmul(P); printInfo("输入矩阵计算权重完毕………………开始计算输入权重"); // 加阈值 // 注意横向问题 for (int i = 0; i < NumberofHiddenNeurons; i++) { for (int j = 0; j < train_data_Count; j++) { tmpH.put(i, j, tmpH.get(i, j) + BiasofHiddenNeurons.get(i, 0)); } } printInfo("输入矩阵计算阈值完毕………………开始计算输入激活函数"); // 输出矩阵 DoubleMatrix H = new DoubleMatrix(NumberofHiddenNeurons, train_data_Count); if (func.startsWith("sig")) { for (int i = 0; i < NumberofHiddenNeurons; i++) { for (int j = 0; j < train_data_Count; j++) { double tmp = tmpH.get(i, j); tmp = 1.0f / (1 + Math.exp(-tmp)); H.put(i, j, tmp); } } } else { throw new IllegalArgumentException("不支持的激活函数类型"); } printInfo("输出矩阵计算激活函数完毕,开始求解广义逆………………矩阵规模" + H.columns + "x" + H.rows); // 将H转置 DoubleMatrix Ht = H.transpose(); // 求广义逆 DoubleMatrix Ht_MP = getMPMatrix(Ht); printInfo("输出矩阵广义逆求解完毕………………开始求解输出矩阵权重"); // 隐含层输出权重矩阵= Ht_MP * Tt OutputWeight = Ht_MP.mmul(T.transpose()); printInfo("输出矩阵权重求解完毕………………"); long end_time_train = System.currentTimeMillis(); TrainingTime = (end_time_train - start_time_train) * 1.0f / 1000; printInfo("模型训练完毕………………开始评估模型"); // 误差评估 DoubleMatrix Yt = new DoubleMatrix(train_data_Count, NumberofOutputNeurons); Yt = Ht.mmul(OutputWeight); double MSE = 0; printInfo("共有" + NumberofOutputNeurons + "个输出值"); for (int out = 0; out < NumberofOutputNeurons; out++) { printInfo("计算第" + (out + 1) + "个输出值"); double mseTem = 0.0; for (int i = 0; i < train_data_Count; i++) { System.out.println(Yt.get(i, out) + " " + T.get(out, i)); mseTem += (Yt.get(i, out) - T.get(out, i)) * (Yt.get(i, out) - T.get(out, i)); } printInfo( "第" + NumberofOutputNeurons + "个输出值计算完毕,MSE=" + Math.sqrt(mseTem / train_data_Count)); MSE += mseTem; } TrainingAccuracy = Math.sqrt(MSE / train_data_Count); printInfo("模型评估完毕………………"); }