コード例 #1
0
ファイル: test_lcp.cpp プロジェクト: JohnCrash/linear
static void test_pgs()
{
	real * A = makeRandSPDMatrix();
	real * b = makeRandVec2();
	real * x = makeRandVec2();
	real * xx = makeRandVec2();
	real * AA = makeMatrix();
	real * bb = makeRandVec2();
	std::vector<real *> vx;
	copyMatrix(AA,A);
	memcpy(bb,b,NN*sizeof(real));
	//memset(x,0,sizeof(real)*N);
	memset(x,0,NN*sizeof(real));
	//memcpy(xx,b,N*sizeof(real));
	memset(xx,0,sizeof(real)*NN);
	
	printf("--------------------------------------------------------\n");
	printMat("solve lcp A=",A);
	printVec("lcp b=",b);
	printf("--------------------------------------------------------\n");
	int result1 = lcp(A,b,vx,NN);	
	int result2 = lcp_pgs(A,b,x,NN,15,0.001);
	int result3 = Solve_GaussSeidel(AA,bb,xx,NN,15);
	printf("lcp solve:\n");
	printf("--------------------------------------------------------\n");
	printLCPVx(vx);
	printf("lcp_pgs solve %s (%d)\n",result2?"true":"false",result2);
	printVec("lcp_pgs=",x);
	check_lcp_result(AA,bb,x,NN);
	printVec("gs solve :",xx);

	freeMatrix(A);
	freeMatrix(b);
	freeMatrix(x);
	freeMatrix(AA);
	freeMatrix(bb);
	freeLcpSolve(vx);
}
コード例 #2
0
ファイル: lcp_solver_pred.c プロジェクト: radarsat1/siconos
int lcp_solver_pred(n , method_lcp *pt , double *z , double *w ,
                    int firsttime, int *soltype , int *indic , int *indicop ,
                    double *submatlcp , double *submatlcpop ,
                    int *ipiv , int *sizesublcp , int *sizesublcpop ,
                    double *subq , double *bufz , double *newz , double *workspace)
{

  /* subq, bufz, newz, workspace: work vectors*/

  const char lcpkey1[10] = "Lemke", lcpkey2[10] = "PGS", lcpkey3[10] = "CPG";
  const char lcpkey4[10] = "Latin", lcpkey5[10] = "QP", lcpkey6[10] = "NSQP";
  const char lcpkey7[15] = "LexicoLemke", lcpkey8[15] = "NewtonMin";
  const char lcpkey9[15] = "Latin_w", lcpkey10[15] = "NewtonFB", lcpkey11[15] = "PSOR";
  const char lcpkey12[10] = "NLGS";
  const char lcpkey13[10] = "RPGS";

  // Remark: Lemke = LexicoLemke. Only one solver is called: lexicoLemke.

  int i, j, info = 1;

  int     iparamLCP[5];
  double  dparamLCP[5];

  for (i = 0 ; i < 5 ; ++i) iparamLCP[i] = 0;
  for (i = 0 ; i < 5 ; ++i) dparamLCP[i] = 0.0;

  *soltype = 0;
  /*  limqpos = -DBL_EPSILON / sqrt((double) *n); */
  if (firsttime == 0)
  {
    i = 0;
    while ((i < (*n - 1)) && (q[i] >= 0.)) i++;
    if ((i == (*n - 1)) && (q[*n - 1] >= 0.))
    {
      /* TRIVIAL CASE : q >= 0
       * z = 0 and w = q is solution of LCP(q,M)
       */
      for (j = 0 ; j < *n; j++)
      {
        z[j] = 0.0;
        w[j] = q[j];
      }
      *soltype = 1;
      pt->lcp.iter = 0;
      pt->lcp.err  = 0.;
      if (pt->lcp.chat > 0) printf("Trivial case of LCP : positive vector q \n");
      return 0;
    }

    info = predictLCP(q , n , z , w , pt->lcp.tol,
                      indic , indicop , submatlcp , submatlcpop ,
                      ipiv , sizesublcp , sizesublcpop , subq , bufz , newz);

    if (info >= 0)
    {
      *soltype = 2;
      pt->lcp.iter = 1;
      if (pt->lcp.chat > 0) printf("LCP solved by prediction on z,w signs\n");
      return 0;
    }
    else info = 1;
  }

  /* Solver name */
  char * name = options->solverName;

  if (verbose == 1)
    printf(" ========================== Call %s solver for Linear Complementarity problem ==========================\n", name);

  /****** Lemke algorithm ******/
  /* IN: itermax
     OUT: iter */
  if (strcmp(name, "Lemke") == 0 || strcmp(name, "LexicoLemke") == 0)
    lcp_lexicolemke(problem, z , w , &info , options);

  /****** PGS Solver ******/
  /* IN: itermax, tolerance
     OUT: iter, error */
  else if (strcmp(name, "PGS") == 0)
    lcp_pgs(problem, z , w , &info , options);

  /****** CPG Solver ******/
  /* IN: itermax, tolerance
     OUT: iter, error */
  else if (strcmp(name, "CPG") == 0)
    lcp_cpg(problem, z , w , &info , options);

  /****** Latin Solver ******/
  /* IN: itermax, tolerance, k_latin
     OUT: iter, error */
  else if (strcmp(name, "Latin") == 0)
    lcp_latin(problem, z , w , &info , options);

  /****** Latin_w Solver ******/
  /* IN: itermax, tolerance, k_latin, relax
     OUT: iter, error */
  else if (strcmp(name, "Latin_w") == 0)
    lcp_latin_w(problem, z , w , &info , options);

  /****** QP Solver ******/
  /* IN: tolerance
     OUT:
     We assume that the LCP matrix M is symmetric
  */
  else if (strcmp(name, "QP") == 0)
    lcp_qp(problem, z , w , &info , options);

  /****** NSQP Solver ******/
  /* IN: tolerance
     OUT:
  */
  else if (strcmp(name, "NSQP") == 0)
    lcp_nsqp(problem, z , w , &info , options);

  /****** Newton min ******/
  /* IN: itermax, tolerance
     OUT: iter, error
  */
  else if (strcmp(name, "NewtonMin") == 0)
    lcp_newton_min(problem, z , w , &info , options);

  /****** Newton Fischer-Burmeister ******/
  /* IN: itermax, tolerance
     OUT: iter, error
  */
  else if (strcmp(name, "Newton_FB") == 0)
    lcp_newton_FB(problem, z , w , &info , options);

  /****** PSOR Solver ******/
  /* IN: itermax, tolerance, relax
     OUT: iter, error
  */
  else if (strcmp(name, "PSOR") == 0)
    lcp_psor(problem, z , w , &info , options);

  /****** RPGS (Regularized Projected Gauss-Seidel) Solver ******/
  /* IN: itermax, tolerance, rho
     OUT: iter, error
  */
  else if (strcmp(name, "RPGS") == 0)
    lcp_rpgs(problem, z , w , &info , options);

  /****** PATH (Ferris) Solver ******/
  /* IN: itermax, tolerance, rho
     OUT: iter, error
  */
  else if (strcmp(name, "Path") == 0)
    lcp_path(problem, z , w , &info , options);

  else
    printf("LCP_driver error: unknown solver named: %s\n", pt->lcp.name);

  /*************************************************
   *  3 - Check solution validity
   *************************************************/

  /* Warning: it depends on the chosen solver */

  /* Not done for:  PGS, RPGS */
  if ((strcmp(name, "PGS") != 0) && (strcmp(name, "RPGS") != 0))
    info = filter_result_LCP(problem, z, w, options->dparam[0]);



  if (info == 0)
  {
    info = extractLCP(problem->M , z, indic , indicop , submatlcp , submatlcpop , ipiv , sizesublcp , sizesublcpop);
    *soltype = 3;
  }

  return info;

}