Exemple #1
0
// number of (i, j) s.t.i < j && xi > xj
// x[] is array to be count, t[] is a tmp array
int inversion(int x[], int t[], int L, int R) {
	if (L >= R) return 0;
	int M = (L + R) >> 1, i, j, k;
	int ret = inversion(x, t, L, M) + inversion(x, t, M+1, R);
	for (i=L, j=M+1, k=0; i<=M && j<=R; ) {
		if (x[i] > x[j]) ret += M - i + 1, t[k++] = x[j++];
		else t[k++] = x[i++];
	}
	for (; i<=M; t[k++]=x[i++]);
	for (; j<=R; t[k++]=x[j++]);
	for (i=L; i<=R; ++i) x[i] = t[i-L];
	return ret;
}
Exemple #2
0
/*------------------------------------------------------------------------
 * kill  --  kill a process and remove it from the system
 *------------------------------------------------------------------------
 */
SYSCALL kill(int pid)
{
	STATWORD ps;    
	struct	pentry	*pptr;		/* points to proc. table for pid*/
	int	dev;
	inversion(pid);
	disable(ps);
	if (isbadpid(pid) || (pptr= &proctab[pid])->pstate==PRFREE) {
		restore(ps);
		return(SYSERR);
	}
	if (--numproc == 0)
		xdone();

	dev = pptr->pdevs[0];
	if (! isbaddev(dev) )
		close(dev);
	dev = pptr->pdevs[1];
	if (! isbaddev(dev) )
		close(dev);
	dev = pptr->ppagedev;
	if (! isbaddev(dev) )
		close(dev);
	
	send(pptr->pnxtkin, pid);

	freestk(pptr->pbase, pptr->pstklen);
	switch (pptr->pstate) {

	case PRCURR:	pptr->pstate = PRFREE;	/* suicide */
			resched();

	case PRWAIT:	semaph[pptr->psem].semcnt++;
			int i;
			for(i=0;i<NLOCKS;i++)
			{
				if(proctab[pid].holding_lock[i].type != -1)
				{
					lockarr[i].lockcnt--;
					lockarr[i].process_lock[pid]=-1;
					if(proctab[pid].holding_lock[i].type==READ)
						lockarr[i].read_count--;
				}
			}

	case PRREADY:	dequeue(pid);
			pptr->pstate = PRFREE;
			break;

	case PRSLEEP:
	case PRTRECV:	unsleep(pid);
						/* fall through	*/
	default:	pptr->pstate = PRFREE;
	}
	restore(ps);
	return(OK);
}
Exemple #3
0
void printPatchListWithLineNumbers(patchList l) {
    l = inversion(l);
    char string[512];
    int i = 0, iIn = 0, iOut = 0;
    
//    “+ k\n” Ajout : la ligne suivante dans le patch est ajoutée sur le flot de sortie après la ligne k du flot d’entrée. Si k = 0, la ligne est insérée avant la première ligne (numérotée 1) du flot d’entrée.
//    “= k\n” Substitution : la ligne k du flot d’entrée est remplacée sur le flot de sortie par la ligne suivante dans le patch.
//    “d k\n” Destruction : la ligne k du flot d’entrée n’est pas recopiée sur le flot de sortie.
    
    while (l != NULL) {
		//printf("iIn: %d, iOut: %d\n", iIn, iOut);
        
        switch (l->op) {
            case ADD:
                i++;
                printf("%d + %d\n",i, iIn);
                
                iOut++;
                i++;
                
                getOutLine(string, iOut);
                printf("%d %s\n", i, string);
                break;
                
            case DEL:
                i++;
                printf("%d - %d\n",i, iIn+1);
                
                iIn++;
                break;
                
            case SUBST:
                i++;
                printf("%d = %d\n", i, iIn+1);
                
                iIn++;
                iOut++;
                i++;
                getOutLine(string, iOut);
                printf("%d %s\n", i, string);
                break;
                
            case COPY:
                iIn++;
                iOut++;
                break;
        }
		l = l->next;
    }
}
int main()
{
	int t,n,i,in,lo_in;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		int a[n];
		for(i=0;i<n;i++)
			scanf("%d",&a[i]);
		in=inversion(a, n);
		lo_in=local_inversion(a,n);
		if(in==lo_in)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}
void mexFunction(int nlhs, mxArray* plhs [],int nrhs, const mxArray* prhs [])
{
  
const double sq2=sqrt(2);

double rho=* (double*)mxGetData(prhs[0]);
double norm=* (double*)mxGetData(prhs[1]);
double* table= mxGetPr(prhs[2]);
size_t n= mxGetNumberOfElements(prhs[2]);
double* x= mxGetPr(prhs[3]);
size_t nx=mxGetNumberOfElements(prhs[3]);

plhs[0]=mxCreateDoubleMatrix(1,nx,mxREAL);
double* y=mxGetPr(plhs[0]);

/*mexPrintf("nTable=%u nX=%u \n" ,n,nx); */ 

double y0=0.5;
int pos;
int i;
double t,xv;

for (i=0;i<nx;++i)
{
 xv=normcdf(sq2,x[i]);
/* mexPrintf("x=%f xv=%f \n",x[i],xv); */
 pos= (int) (xv*(n+1));
if(pos<=1)
  y0=table[0];
else if(pos>=n)
  y0=table[n-1];
else
  {
  t= (n+1)*xv-pos;
 /*mexPrintf("pos=%d t=%f, %f<y<%f \n",pos,t,table[pos-1],table[pos]); */
  y0=(1-t)*table[pos-1]+t*table[pos];

  }

y[i]=exp(inversion(rho,norm,y0,xv));
/* mexPrintf("y=%f (%f) \n",y[i],y0); */
}
}
Exemple #6
0
void main_function()
{
  const unsigned SIZE = 8;
  unsigned i, j;
  eoBooleanGenerator gen;

  Chrom chrom(SIZE), chrom2;
  chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));

  std::cout << "chrom:  " << chrom << std::endl;
  chrom[0] = chrom[SIZE - 1] = true; chrom.fitness(binary_value(chrom));
  std::cout << "chrom:  " << chrom << std::endl;
  chrom[0] = chrom[SIZE - 1] = false; chrom.fitness(binary_value(chrom));
  std::cout << "chrom:  " << chrom << std::endl;
  chrom[0] = chrom[SIZE - 1] = true; chrom.fitness(binary_value(chrom));

  std::cout << "chrom.className() = " << chrom.className() << std::endl;

  std::cout << "chrom:  " << chrom << std::endl
       << "chrom2: " << chrom2 << std::endl;

  std::ostringstream os;
  os << chrom;
  std::istringstream is(os.str());
  is >> chrom2; chrom.fitness(binary_value(chrom2));

  std::cout << "\nTesting reading, writing\n";
  std::cout << "chrom:  " << chrom << "\nchrom2: " << chrom2 << '\n';

  std::fill(chrom.begin(), chrom.end(), false);
  std::cout << "--------------------------------------------------"
       << std::endl << "eoMonOp's aplied to .......... " << chrom << std::endl;

  eoInitFixedLength<Chrom>
      random(chrom.size(), gen);

  random(chrom); chrom.fitness(binary_value(chrom));
  std::cout << "after eoBinRandom ............ " << chrom << std::endl;

  eoOneBitFlip<Chrom> bitflip;
  bitflip(chrom); chrom.fitness(binary_value(chrom));
  std::cout << "after eoBitFlip .............. " << chrom << std::endl;

  eoBitMutation<Chrom> mutation(0.5);
  mutation(chrom); chrom.fitness(binary_value(chrom));
  std::cout << "after eoBinMutation(0.5) ..... " << chrom << std::endl;

  eoBitInversion<Chrom> inversion;
  inversion(chrom); chrom.fitness(binary_value(chrom));
  std::cout << "after eoBinInversion ......... " << chrom << std::endl;

  eoBitNext<Chrom> next;
  next(chrom); chrom.fitness(binary_value(chrom));
  std::cout << "after eoBinNext .............. " << chrom << std::endl;

  eoBitPrev<Chrom> prev;
  prev(chrom); chrom.fitness(binary_value(chrom));
  std::cout << "after eoBinPrev .............. " << chrom << std::endl;

  std::fill(chrom.begin(), chrom.end(), false); chrom.fitness(binary_value(chrom));
  std::fill(chrom2.begin(), chrom2.end(), true); chrom2.fitness(binary_value(chrom2));
  std::cout << "--------------------------------------------------"
       << std::endl << "eoBinOp's aplied to ... "
       << chrom << " " << chrom2 << std::endl;

  eo1PtBitXover<Chrom> xover;
  std::fill(chrom.begin(), chrom.end(), false);
  std::fill(chrom2.begin(), chrom2.end(), true);
  xover(chrom, chrom2);
  chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));
  std::cout << "eoBinCrossover ........ " << chrom << " " << chrom2 << std::endl;

  for (i = 1; i < SIZE; i++)
    {
      eoNPtsBitXover<Chrom> nxover(i);
      std::fill(chrom.begin(), chrom.end(), false);
      std::fill(chrom2.begin(), chrom2.end(), true);
      nxover(chrom, chrom2);
      chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));
      std::cout << "eoBinNxOver(" << i << ") ........ "
	   << chrom << " " << chrom2 << std::endl;
    }

  for (i = 1; i < SIZE / 2; i++)
    for (j = 1; j < SIZE / 2; j++)
      {
	eoBitGxOver<Chrom> gxover(i, j);
	std::fill(chrom.begin(), chrom.end(), false);
	std::fill(chrom2.begin(), chrom2.end(), true);
	gxover(chrom, chrom2);
	chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));
	std::cout  << "eoBinGxOver(" << i << ", " << j << ") ..... "
	      << chrom << " " << chrom2 << std::endl;
      }

    // test SGA algorithm
    eoGenContinue<Chrom> continuator1(50);
    eoFitContinue<Chrom> continuator2(65535.f);

    eoCombinedContinue<Chrom> continuator(continuator1, continuator2);

    eoCheckPoint<Chrom> checkpoint(continuator);

    eoStdoutMonitor monitor;

    checkpoint.add(monitor);

    eoSecondMomentStats<Chrom> stats;

    monitor.add(stats);
    checkpoint.add(stats);

    eoProportionalSelect<Chrom> select;
    eoEvalFuncPtr<Chrom>  eval(binary_value);

    eoSGA<Chrom> sga(select, xover, 0.8f, bitflip, 0.1f, eval, checkpoint);

    eoInitFixedLength<Chrom> init(16, gen);
    eoPop<Chrom> pop(100, init);

    apply<Chrom>(eval, pop);

    sga(pop);

    pop.sort();

    std::cout << "Population " << pop << std::endl;

    std::cout << "\nBest: " << pop[0].fitness() << '\n';

  /*

    Commented this out, waiting for a definite decision what to do with the mOp's

    // Check multiOps
    eoMultiMonOp<Chrom> mOp( &next );
    mOp.adOp( &bitflip );
    std::cout << "before multiMonOp............  " << chrom << std::endl;
    mOp( chrom );
    std::cout << "after multiMonOp .............. " << chrom << std::endl;

    eoBinGxOver<Chrom> gxover(2, 4);
    eoMultiBinOp<Chrom> mbOp( &gxover );
    mOp.adOp( &bitflip );
    std::cout << "before multiBinOp............  " << chrom << " " << chrom2 << std::endl;
    mbOp( chrom, chrom2 );
    std::cout << "after multiBinOp .............. " << chrom << " " << chrom2 <<std::endl;
  */
}