Untitled

 avatar
unknown
c_cpp
3 years ago
1.0 kB
4
Indexable
#include <conio.h>
#include <stdio.h>
#include "hill_climb.h"

void hillclimb()
{
	int n = 10;
	int stuff = 1;
	pcg32_random_t rng;
	pcg32_srandom_r(&rng, time(NULL) ^ (intptr_t)&printf, (intptr_t)&rng);

	double x;
	double s = 0;
	double r;
	double w;
	double best = s;
	double neg = 1;
	double neg2 = 1;

	double sugar = 1;

	while (stuff<50000)
	{
		x = ((double)pcg32_boundedrand_r(&rng, 1000000000)) / (500000000 / sugar);
		if (stuff % 2 == 0) x = -x;

		r = s + x;

		for (int i = 0; i < n; i++)
		{
			x = ((double)pcg32_boundedrand_r(&rng, 1000000000)) / (500000000 / sugar);
			if (i % 2 == 0) x = -x;

			w = s + x;

			if (fvg1(w) < 0) neg = -1;
			else neg = 1;
			if (fvg1(r) < 0) neg2 = -1;
			else neg2 = 1;
			if (neg*fvg1(w) < neg2*fvg1(r)) r = w;
		}
		s = r;

		if (fvg1(s) < 0) neg = -1;
		else neg = 1;
		if (fvg1(best) < 0) neg2 = -1;
		else neg2 = 1;
		if (neg*fvg1(s)< neg2*fvg1(best)) best = s;

		stuff++;
	}
	printf("%f", best);

}
Editor is loading...