PHP - Pasar codigo Hill Climbing en C a PHP HELP ME!!

 
Vista:

Pasar codigo Hill Climbing en C a PHP HELP ME!!

Publicado por Rafael (1 intervención) el 19/11/2009 17:26:08
HOLA, NECESITO URGENTE PUEDAN AYUDARME A TRADUCIR ESTE CODIGO EN C A PHP, ES DE UN ALGORITMO HILL CLIMBING ...POR FAVOR QUIEN SEPA QUE ME PUEDA AYUDAR...SE LOS AGRADECERE DE POR VIDA...

... ESTE ES EL CODIGO EN C QUE NECESITO PASAR A PHP

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* Problema de programacion lineal entera binaria */
/*
Max 3 x1 + 2 x2 - 5 x3 - 2 x4 + 3 x5
SA.
1 x1 + 1 x2 + 1 x3 + 2 x4 + 1 x5 <= 4
7 x1 + 3 x3 - 4 x4 + 3 x5 <= 8
11 x1 - 6 x2 + 3 x4 - 3 x5 >= 3

xi binarias
*/

/* Cantidad de variables */
const int SIZE = 5;

/* Cantidad de restricciones */
const int CSIZE = 3;

int focoeffs[SIZE];
int coeffs[CSIZE][SIZE];
int rhs[CSIZE];

void generarAleatorios() {
srand((unsigned)time( NULL ) );
// genera los focoeffs
for(int i = 0; i != SIZE; i++)
focoeffs[i] = rand()%10;

for(int i = 0; i != CSIZE ; i++)
for(int j = 0; j != SIZE; j++)
coeffs[i][j] = rand()%5;

//for(int i = 0; i!= CSIZE; i++)
//ops[CSIZE] = rand()%3;

for(int i = 0; i != CSIZE; i++)
rhs[i] = rand()%25;

printf("MAX: ");

printf("ST\n");
for(int b=0; b!=CSIZE; b++){
for(int a=0;a!=SIZE;a++)
printf("%d X%d ", coeffs[b][a], a+1);
printf(" = %d\n");
}
printf("\n\n\n");

}


/* Coeficientes de la funcion objetivo */
//int focoeffs[SIZE] = {3, 2, -5, -2, 3 };

/* Coeficientes de las restricciones */
// int coeffs[CSIZE][SIZE] = {{ 1, 1, 1, 2, 1},
// { 7, 0, 3, -4, 3},
// {11, -6, 0, 3, -3}};

/* Tipo de restriccion: 1: <=, 2: >= */
int ops[CSIZE] = { 1, 1, 1 };

/* Lados derechos */
// int rhs[CSIZE] = { 4, 8, 3 };

/* Mejor individuo actual */
int* best = new int[SIZE];

/* Verifica factibilidad */
int evalFactibility(int* vs) {
for (int i = 0; i < CSIZE; i++) {
int val = 0;
for (int j = 0; j < SIZE; j++) {
val += coeffs[i][j] * vs[j];
}
if (ops[i] == 1) {
if (!(val <= rhs[i])) {
return 0; //false;
}
}
else if (ops[i] == 2) {
if (!(val >= rhs[i])) {
return 0; //false;
}
}
}
return 1; //true;
}

/* Evalua un string */
int eval(int* vs) {
int val = 0;
for (int j = 0; j < SIZE; j++) {
val += focoeffs[j]*vs[j];
}
return val;
}

void error(char* msg) {
printf("%s\n", msg);
exit(0);
}

/* Setea el optimo actual por el individuo dado */
void setBest(int* vs) {
if (!evalFactibility(vs)) {
error("individuo no es factible");
}
best = vs;
}

/* Retorna el optimo actual */
int* getBest() {
return best;
}

/* Retorna la evaluacion del optimo actual */
int getBestEval() {
return eval(best);
}

/* string de variables binarias 0/1 */

/* retorna el vecindario */
int** getNeighborhood(int* vs) {
int** ret = new int*[SIZE];
for (int k = 0; k < SIZE; k++) {
ret[k] = new int[SIZE];
}
for (int i = 0; i < SIZE; i++) {
/* complemento simple */
for (int j = 0; j < SIZE; j++) {
ret[i][j] = vs[j];
}
ret[i][i] = 1 - ret[i][i];
}
return ret;
}

/* Crea un string aleatorio y factible */
int* getRandomString() {
int* vs = new int[SIZE];

vs[0] = 0;
vs[1] = 0;
vs[2] = 1;
vs[3] = 1;
vs[4] = 0;
srand((unsigned)time( NULL ) );
do {
for (int i = 0; i < SIZE; i++) {
vs[i] = rand()%2;
}
} while(!evalFactibility(vs));
return vs;
}

/* Imprime string */
void printString(int* vs) {
for (int i = 0; i < SIZE; i++) {
printf("%d ", vs[i]);
}
printf(": z=%d", eval(vs));
}

/* Algoritmo de busqueda local */
int* solve(int* ini) {
/* condicion de termino */
int endcond = 0; //false;
/* solucion inicial */
setBest(ini);
printf("solucion inicial: ");
printString(ini);
printf("\n");
/* cantidad de iteraciones */
int iter = 0;
do {
/* incrementa iteracion */
iter++;
printf("iteracion: %d\n",iter);
/* obtiene vecinos */
int** news= getNeighborhood(getBest());
/* determinar mejor vecino */
int bestn = 0;
int val = -100;
for (int i = 0; i < SIZE; i++) {
if (evalFactibility(news[i])) {
if (eval(news[i]) > val) {
val = eval(news[i]);
bestn = i;
}
}
}
/* determinar si es mejor que el optimo actual */
if (val > getBestEval()) {
printf("nuevo optimo: ");
printString(news[bestn]);
printf("\n");
setBest(news[bestn]);
}
else {
endcond = 1; //true;
}
} while(!endcond);
printf("fin\n");
return getBest();
}

int main(int argc, char* argv[]) {
generarAleatorios();
solve(getRandomString());
printf("Solucion encontrada: ");
printString(best);
printf("\n");
system ("pause");
return 0;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder