Example #1
0
/* To print the statistics of the solution */
void print_summary(solution_t *soln) {
    
    job_t *jobs = soln->jobs;
    int *jobs_order = soln->jobs_order;
    int first_job, last_job;

    printf("-----------------\n");
    printf("SUMMARY\n");
    printf("-----------------\n");
    first_job = jobs_order[0]; 
    last_job = jobs_order[numnodes]; 
    printf("Time span: %f\n", get_end_time(last_job, soln)-get_start_time(first_job, soln));
    printf("Objective function: %f\n", objective_function(soln));
    printf("\n");
    printf("Number of late jobs: %d\n", get_num_late_jobs(soln));
    printf("Sum of all tardiness: %f\n", get_total_lateness(soln));
    printf("Max tardiness: %f\n", get_max_lateness(soln));
    printf("Average tardiness: %f\n", get_average_lateness(soln));
    printf("Variance of tardiness: %f\n", get_variance_lateness(soln));
    printf("Standard deviation of tardiness: %f\n", get_standard_deviation_lateness(soln));
    printf("\n");
    printf("Number of early jobs: %d\n", get_num_early_jobs(soln));
    printf("Sum of all earliness: %f\n", get_total_earliness(soln));
    printf("Average earliness: %f\n", get_average_earliness(soln));
    printf("\n");
    printf("Total travelling time: %f\n", get_total_travelling_time(soln));
    printf("Total waiting time: %f\n", get_total_waiting_time(soln));
    printf("-----------------\n");

}
int main(int argc, char *argv[])
{
  std::vector<double> min_p = {-1000, -2000};
  std::vector<double> max_p = {1000, 4000};
  std::vector<double> wts(2000, 0.6); // each of 2000 iterations have weight 0.6

  // perform ppso
  auto ret = ppso::Particle_Swarm{
    objective_function(),
    2, // number of dimensions
    min_p, // vector of minimum values for each dimension
    max_p, // vector of maximum values for each dimension
    std::max(std::thread::hardware_concurrency(), (unsigned int)2), // number of groups (threads)
    20, // number of particles
    2000.0, // maximum velocity
    wts, // vector of weights for each iteration
    2, // personal influence constant
    2, // group influence constant
    4, // every 4 iterations, communication strategy 1
    8, // every 8 iterations, communication strategy 2
    2000 // 2000 iterations
  }.run();

  // print x*
  std::cout << std::fixed << "x* = [ ";
  for (double x: ret.first) {
    std::cout << x << " ";
  }
  std::cout << "]'\n";

  // print f(x*)
  std::cout << "f(x*) = " << ret.second << std::endl;
  
  return 0;
}
Example #3
0
void evaluate_fitness(Population * population, mySlimTree* SlimTree, TCity * queryObject, stDistance range)
{
	double x, y;
	long i;
	for( i = 0; i < POPULATION_SIZE; ++i)
	{
		decoder(&population->individuals[i]);
		
		if(MAXIMIZATION)
		{
			population->individuals[i].fitness = objective_function(population->individuals[i].phenotype, SlimTree, queryObject, range);
		} else
		{
			population->individuals[i].fitness = (-1 * objective_function(population->individuals[i].phenotype, SlimTree, queryObject, range));
		}
	}
}
Example #4
0
void get_fitness( int n_pop, int n_dim, double *position, double *fitness )
{
    int p;

    for( p=0; p<n_pop; p++ )
    {
        fitness[p] = objective_function( n_dim, &position[p*n_dim] );
    }
}
Example #5
0
void MySimulation::measure() {
    bool is_past_burnin=(istep_>=burnin_);
    if (verbose_) {
        std::cout << istep_ << " " << is_past_burnin << " " << x_ << " " << y_ << "\n";
    }
    if (!is_past_burnin) return;

    measurements["objective"] << objective_function(x_,y_);
}
Example #6
0
static double Maximize_Quality_Golden_Section(MVertex *ver, double xTarget,
                                              double yTarget, double zTarget,
                                              const std::vector<MElement *> &lt,
                                              double const tol, double &q)
{
  const double lambda = 0.5 * (std::sqrt(5.0) - 1.0);
  const double mu = 0.5 * (3.0 - std::sqrt(5.0)); // = 1 - lambda
  double a = 0.0;
  double b = 2.0;

  double x1 = b - lambda * (b - a);
  double x2 = a + lambda * (b - a);
  double fx1 = objective_function(x1, ver, xTarget, yTarget, zTarget, lt);
  double fx2 = objective_function(x2, ver, xTarget, yTarget, zTarget, lt);

  if(tol < 0.0) return fx1 > fx2 ? x1 : x2;

  while(!Stopping_Rule(a, b, tol)) {
    // printf("GOLDEN : %g %g (%12.5E,%12.5E)\n",a,b,fa,fb);
    if(fx1 < fx2) {
      a = x1;
      if(Stopping_Rule(a, b, tol)) break;
      x1 = x2;
      fx1 = fx2;
      x2 = b - mu * (b - a);
      fx2 = objective_function(x2, ver, xTarget, yTarget, zTarget, lt);
    }
    else {
      b = x2;
      if(Stopping_Rule(a, b, tol)) break;
      x2 = x1;
      fx2 = fx1;
      x1 = a + mu * (b - a);
      fx1 = objective_function(x1, ver, xTarget, yTarget, zTarget, lt);
    }
  }
  // printf("finally : %g %g (%12.5E,%12.5E)\n",a,b,fa,fb);
  q = std::min(fx1, fx2);
  return a;
}
Example #7
0
static void _relocateVertexOfPyramid(MVertex *ver,
                                     const std::vector<MElement *> &lt,
                                     double relax)
{
  if(ver->onWhat()->dim() != 3) return;
  double x = 0.0, y = 0.0, z = 0.0;
  int N = 0;
  MElement *pyramid = NULL;

  for(std::size_t i = 0; i < lt.size(); i++) {
    double XCG = 0.0, YCG = 0.0, ZCG = 0.0;
    if(lt[i]->getNumVertices() == 5)
      pyramid = lt[i];
    else {
      for(std::size_t j = 0; j < lt[i]->getNumVertices(); j++) {
        XCG += lt[i]->getVertex(j)->x();
        YCG += lt[i]->getVertex(j)->y();
        ZCG += lt[i]->getVertex(j)->z();
      }
      x += XCG;
      y += YCG;
      z += ZCG;
      N += lt[i]->getNumVertices();
    }
  }
  x /= N;
  y /= N;
  z /= N;

  if(pyramid) {
    MFace q = pyramid->getFace(4);
    double A = q.approximateArea();
    SVector3 n = q.normal();
    n.normalize();
    SPoint3 c = q.barycenter();
    SVector3 d(x - c.x(), y - c.y(), z - c.z());
    if(dot(n, d) < 0) n = n * (-1.0);
    double H = .5 * sqrt(fabs(A));
    double XOPT = c.x() + relax * H * n.x();
    double YOPT = c.y() + relax * H * n.y();
    double ZOPT = c.z() + relax * H * n.z();
    double FULL_MOVE_OBJ =
      objective_function(1.0, ver, XOPT, YOPT, ZOPT, lt, true);
    // printf("relax %g obj %g\n",relax,FULL_MOVE_OBJ);
    if(FULL_MOVE_OBJ > 0.1) {
      ver->x() = XOPT;
      ver->y() = YOPT;
      ver->z() = ZOPT;
      return;
    }
  }
}
Example #8
0
main()
{
//	nevaluations = 0;

		float values[nov] ;
		float cost , penalty;
		values[0] = 1  ;
		values[1] = 3  ;
		penalty = 0.0;
		cost = objective_function( values, &penalty);
		printf(" ofn = %15.15f , sumconstr = %6.8f ", cost,penalty);

}
Example #9
0
static void _relocateVertexGolden(MVertex *ver,
                                  const std::vector<MElement *> &lt,
                                  double relax, double tol)
{
  if(ver->onWhat()->dim() != 3) return;
  double x = 0.0, y = 0.0, z = 0.0;
  int N = 0;
  for(std::size_t i = 0; i < lt.size(); i++) {
    // TODO C++11 use std::accumulate instead
    double XCG = 0.0, YCG = 0.0, ZCG = 0.0;
    for(std::size_t j = 0; j < lt[i]->getNumVertices(); j++) {
      XCG += lt[i]->getVertex(j)->x();
      YCG += lt[i]->getVertex(j)->y();
      ZCG += lt[i]->getVertex(j)->z();
    }
    x += XCG;
    y += YCG;
    z += ZCG;
    N += lt[i]->getNumVertices();
  }

  double NO_MOVE_OBJ = objective_function(0.0, ver, x / N, y / N, z / N, lt);
  if(NO_MOVE_OBJ > 0.1) return;
  double FULL_MOVE_OBJ = objective_function(1.0, ver, x / N, y / N, z / N, lt);
  if(FULL_MOVE_OBJ > NO_MOVE_OBJ) {
    ver->x() = x / N;
    ver->y() = y / N;
    ver->z() = z / N;
    return;
  }

  double q;
  double xi = relax * Maximize_Quality_Golden_Section(ver, x / N, y / N, z / N,
                                                      lt, tol, q);
  ver->x() = (1. - xi) * ver->x() + xi * x / N;
  ver->y() = (1. - xi) * ver->y() + xi * y / N;
  ver->z() = (1. - xi) * ver->z() + xi * z / N;
}
Example #10
0
static double Maximize_Quality_Golden_Section(MVertex *ver, GFace *gf,
                                              SPoint3 &p1, SPoint3 &p2,
                                              const std::vector<MElement *> &lt,
                                              double tol, double &worst)
{
  const double lambda = 0.5 * (std::sqrt(5.0) - 1.0);
  const double mu = 0.5 * (3.0 - std::sqrt(5.0)); // = 1 - lambda
  double a = 0.0;
  double b = 1.0;

  worst = objective_function(0.0, ver, gf, p1, p2, lt);

  if(worst > 0.5) return 0.0;

  double x1 = b - lambda * (b - a);
  double x2 = a + lambda * (b - a);
  double fx1 = objective_function(x1, ver, gf, p1, p2, lt);
  double fx2 = objective_function(x2, ver, gf, p1, p2, lt);

  if(tol < 0.0) return fx1 > fx2 ? x1 : x2;

  while(!Stopping_Rule(a, b, tol)) {
    // printf("GOLDEN : %g %g (%12.5E,%12.5E)\n",a,b,fa,fb);
    if(fx1 < fx2) {
      a = x1;
      if(Stopping_Rule(a, b, tol)) break;
      x1 = x2;
      fx1 = fx2;
      x2 = b - mu * (b - a);
      fx2 = objective_function(x2, ver, gf, p1, p2, lt);
    }
    else {
      b = x2;
      if(Stopping_Rule(a, b, tol)) break;
      x2 = x1;
      fx2 = fx1;
      x1 = a + mu * (b - a);
      fx1 = objective_function(x1, ver, gf, p1, p2, lt);
    }
  }
  double final = objective_function(a, ver, gf, p1, p2, lt);
  if(final < worst) return 0.0;
  worst = final;
  // printf("finally : %g %g (%12.5E,%12.5E)\n",a,b,fa,fb);
  return a;
}
Example #11
0
// Set the objective function and gradients for GSL minimizer
void set_functions(const gsl_vector *x, void *params, double *f, gsl_vector *g)
{
    *f = objective_function(x, params);
    get_gradients(x, params, g);
}
Example #12
0
double* minimize(double x[], double y[], int n, int k)
{
	double  **simplex=create_simplex(n); /* create a new simplex */
	
	double *function_values=(double*)malloc((n+1)*sizeof(double)); /* function values for a new simplex */
	function_values=call_functions(x,y,simplex,n,k); /* store function values for every vertex of a simplex in an array */
	
	int worst, second_worst, best; /* variable to store indecies for worst, second_word and best vortex */
	double min, max; /* max and min values for function values */

	int i,j; /* indecies for for loop */

	double* centroid=(double*)malloc(n*sizeof(double)); /* centroid */

	double alpha=1, beta=0.5, gamma=2, delta=0.5; /* scalar parameteras */

	double *vertex_r=(double*)malloc(n*sizeof(double)); /* reflection point */
	double f_r; /* function value at the reflection point */

	double *vertex_e=(double*)malloc(n*sizeof(double)); /* expansion point */
	double f_e; /* function value at the expansion point */

	double *vertex_c=(double*)malloc(n*sizeof(double)); /* contraction point */
	double f_c; /* function value at the contraction point */

	int intterations_max=5000;
	int itteration=0;

	double conv_test; /* check every 42 itterations if function has changde less than 0.01% */
	double diff=42.0;

	do
	{
	/* 1. step Ordering*/

	worst=0, second_worst=0, best=0;
	min=function_values[0], max=function_values[0];
	
	for(i=1;i<(n+1);i++)
	{
		if(min>=function_values[i])
		{
			min=function_values[i];
			best=i;
		}
		if(max<=function_values[i])
		{
			max=function_values[i];
			second_worst=worst;
			worst=i;
		}
	}

	/* 2. step Centroid */

	for(i=0;i<n;i++)
		centroid[i]=0.0;

	for (i=0; i<n; i++)
	{
		for(j=0;j<(n+1);j++)
		{
			if(j!=worst)
			{
				centroid[i]+=simplex[j][i];
			}
		}
		centroid[i]=centroid[i]/n;		
	}

	/* 3. Step Transformation */

	/* Reflect */

	for(i=0;i<n;i++)
	{
		vertex_r[i]=centroid[i]+alpha*(centroid[i]-simplex[worst][i]);
	}
	f_r=objective_function(x,y,vertex_r,n,k);

	if(function_values[best]<=f_r && f_r<function_values[second_worst])
	{
		for(i=0;i<n;i++)
		{
			simplex[worst][i]=vertex_r[i];
		}
		function_values[worst]=f_r;
	}
	else if (f_r<function_values[best]) /* Expand */
	{
		for(i=0;i<n;i++)
		{
			vertex_e[i]=centroid[i]+gamma*(vertex_r[i]-centroid[i]);
		}
		f_e=objective_function(x,y,vertex_e,n,k);
		if(f_e<f_r)
		{
			for(i=0;i<n;i++)
			{
				simplex[worst][i]=vertex_e[i];
			}
			function_values[worst]=f_e;
		}
		else if(f_e>=f_r)
		{
			for(i=0;i<n;i++)
			{
				simplex[worst][i]=vertex_e[i];
			}
			function_values[worst]=f_e;
		}
	}
	else if (f_r>=function_values[second_worst]) /* Contract */
	{
		if(function_values[second_worst]<=f_r && f_r<function_values[worst]) /* Outside */
		{
			for(i=0;i<n;i++)
			{
				vertex_c[i]=centroid[i]+beta*(vertex_r[i]-centroid[i]);
			}
			f_c=objective_function(x,y,vertex_c,n,k);
			if(f_c<=f_r)
			{
				for(i=0;i<n;i++)
				{
					simplex[worst][i]=vertex_c[i];
				}
				function_values[worst]=f_c;
			}
		}
		else if(f_r>=function_values[worst]) /* Inside */
		{
			for(i=0;i<n;i++)
			{
				vertex_c[i]=centroid[i]+beta*(simplex[worst][i]-centroid[i]);
			}
			f_c=objective_function(x,y,vertex_c,n,k);
			if(f_c<function_values[worst])
			{
				for(i=0;i<n;i++)
				{
					simplex[worst][i]=vertex_c[i];
				}
				function_values[worst]=f_c;
			}
		}
		else /* Shrink */
		{
			for(i=0;i<(n+1);i++)
			{
				if(i!=best)
				{
					for(j=0;i<n;j++)
					{
						simplex[i][j]=simplex[best][j]+delta*(simplex[i][j]-simplex[best][j]);
					}
				}
				
			}
			function_values=call_functions(x,y,simplex,n,k);
		}
	}	

	if(itteration%42==0)
	{
		if(itteration>0)
		{
			diff=fabs(conv_test-function_values[best]);
		}
		conv_test=function_values[best];
	}

	itteration++;

	} while (itteration<intterations_max && diff>0.0001);
	
	if(itteration==intterations_max)
	{
		printf("The algorithm has not converge.\n");
	}
	return simplex[best];

}