
#import "Drill.h"
 
@implementation Drill

- init
{
   [super init];
   L = 11300;
   dh = 8.5;

   Yp = 12;
   visc0600 = 60;
   visc0300 = 36;
   n = 3.32*log(visc0600/visc0300);
   K = visc0300/pow(511,n);

   return self;
} 


- setGenes
{
    double myPi;
 
    numOfGenes = 5;
    popUpList = [[PopUpList alloc] init];
    [[popUpList addItem:"Dirll Pipe ID"] setTag:0];
    [[popUpList addItem:"Drill Pipe OD"] setTag:1];
    [[popUpList addItem:"Mud Weight"] setTag:2];
    [[popUpList addItem:"Circulating Rate"] setTag:3];
    [[popUpList addItem:"Plastic Viscosity"] setTag:4];
    geneMap[0] = 16;
    geneMap[1] = 16;
    geneMap[2] = 16;
    geneMap[3] = 16;
    geneMap[4] = 16;
    myPi = 3.141592653589793;
    scaleFactor[0] = (double)8.0/pow((double)2.0,(double)geneMap[0]);    
    scaleFactor[1] = (double)10.0/pow((double)2.0,(double)geneMap[1]);    
    scaleFactor[2] = (double)5.0/pow((double)2.0,(double)geneMap[2]);    
    scaleFactor[3] = (double)1000.0/pow((double)2.0,(double)geneMap[3]);    
    scaleFactor[4] = (double)40.0/pow((double)2.0,(double)geneMap[4]);    
    xOffset[0] = 1.0;    
    xOffset[1] = 2.0;    
    xOffset[2] = 10.0;    
    xOffset[3] = 50.0;    
    xOffset[4] = 10.0;    
    return self;
}
  
 
- (double)getFitnessOfMember:(int)genomeNum
{
    double revenue;

    [super getFitnessOfMember:genomeNum];
    d = xVal[genomeNum][0]+xOffset[0];
    dp = xVal[genomeNum][1]+xOffset[1];
    Mw = xVal[genomeNum][2]+xOffset[2];
    Q = xVal[genomeNum][3]+xOffset[3];
    Pv = xVal[genomeNum][4]+xOffset[4];

    V = Q/(2.448*d*d);
    Vfm = V*60;
    Vc = pow(5.82e4*K/Mw,1/(2*n))*pow((1.6/d)*((3*n+1)/(4*n)),n/(2*n));

    if(V < Vc*.85){
        PdsL = pow((1.6*Vfm/d)*(3*n+1)/(4*n),n)*K*L/(300*d);
        Pds = PdsL;
    }
    else if(V > Vc*1.15){
        PdsT = 2.27e7*pow(Mw,0.8)*pow(Vfm,1.8)*pow(Pv,0.2)*L/pow(d,1.2);
        Pds = PdsT;
    }
    else{
        PdsL = pow((1.6*Vfm/d)*(3*n+1)/(4*n),n)*K*L/(300*d);
        PdsT = 2.27e7*pow(Mw,0.8)*pow(Vfm,1.8)*pow(Pv,0.2)*L/pow(d,1.2);
        if(PdsL > PdsT){
            Pds = PdsL;
        }
        else{
            Pds = PdsT;
        }
    }
    revenue = pow(Q,2.5)/10 - pow(Pds,1.5)/pow(500,1.5);
    if(d > dp){
         revenue = revenue/3;
    }
    genePool[genomeNum].fitness = revenue;
    return genePool[genomeNum].fitness;   
}


- dumpSolution:console forGenome:(int)genomeNum
{
    int i;
    id popUpListMatrix;
    char buffer[MAXPATHLEN+1];

    [console replaceSel:"\n>>> Best Drill Solution Achieved: \n"];
    popUpListMatrix = [popUpList itemList];
    for(i=0;i<numOfGenes;i++){
        sprintf(buffer,"\n%d. %s = %f ", i+1, [[popUpListMatrix cellAt:i :0] title], xVal[genomeNum][i]+xOffset[i]);
        [console replaceSel:buffer];
    }
    return self;
}

 
@end
