Index: vtcross/branches/nikhil/crossmodel2/src/cognitive_engines/CBR_CE/CBR_CE.h
===================================================================
--- vtcross/branches/nikhil/crossmodel2/src/cognitive_engines/CBR_CE/CBR_CE.h	(revision 554)
+++ vtcross/branches/nikhil/crossmodel2/src/cognitive_engines/CBR_CE/CBR_CE.h	(revision 562)
@@ -37,7 +37,10 @@
 
 
+//typedef float (*array)[radioInfo->numParameters][radioInfo->numUtilities];
+
 class CBR_CE : public CognitiveEngine
 {
     public:
+
         /*! Default constructor. */
         CBR_CE() : CognitiveEngine(){};
@@ -92,4 +95,6 @@
          * cognitive engine's functionality.
          */
+	void InitialSetting();
+
         void PerformUpdatePerformance();
         void PerformRequestOptimization();
@@ -97,6 +102,27 @@
         void PerformResetEngineCognitive();
         void PerformShutdownEngineCognitive();
+	void FineTune();
 
-        CBR *myCBR;
+        CBR *myCBR1;
+	CBR *myCBR2;
+/*
+    private:
+
+	float * Pweights;
+	float * Uweights;
+
+	float (*Status)[10];
+// this array stores the relationship between all parameters vs utilities.. 
+	float (*Trend)[10];
+
+	int * Slope;
+
+	float PoC;
+// this term represents percentage of change.. as in if this min PoC
+// is not detected its as good as no change..
+	int Ptune;
+// This is pointer to correspont to which parameter to tune.. 
+// AS OF NOW ONLY ONE AT A TIME PER CYCLE
+*/ 
 
 };
Index: vtcross/branches/nikhil/crossmodel2/src/cognitive_engines/CBR_CE/CBR_CE.cpp
===================================================================
--- vtcross/branches/nikhil/crossmodel2/src/cognitive_engines/CBR_CE/CBR_CE.cpp	(revision 554)
+++ vtcross/branches/nikhil/crossmodel2/src/cognitive_engines/CBR_CE/CBR_CE.cpp	(revision 562)
@@ -22,5 +22,11 @@
 #include <iostream>
 
+
 using namespace std;
+
+// ZERO is consider +1 sign
+inline int signof(int nos) { return ( (nos < 0) ? -1:1 ); }
+//inline int signof(int nos) { return (nos==0)?0:(nos<0?-1:1); }
+
 
 
@@ -37,4 +43,8 @@
 {
     LOG("Cognitive Engine:: Generating solution.\n");
+
+	std::cout << "Ptune value: " << optList->Ptune << std::endl;
+//	std::cout << "slope values: " << Slope[0] << std::endl;
+//	std::cout << "slope values: " << Slope[1] << std::endl;
 
     string searchNames[radioInfo->numUtilities];
@@ -81,5 +91,5 @@
 
     /* CBR specific call */
-    uint32_t rc = myCBR->Search(searchNames, searchOps, searchVals,
+    uint32_t rc = myCBR1->Search(searchNames, searchOps, searchVals,
             radioInfo->numUtilities, returnValues);
 
@@ -87,5 +97,17 @@
         /* Adapt the returned parameters to meet the objective */
         LOG("Cognitive Engine:: Found\n");
-
+	
+	FineTune();
+/*
+        No need or random adaptation since it will get updated 
+        using update function call
+        if(returnValues[numberColumns-1] < 0) {
+            returnValues[2] = returnValues[2] - 15;
+            returnValues[3] = returnValues[3] - 2;
+        } else {
+            returnValues[2] = returnValues[2] + 15;
+            returnValues[3] = returnValues[3] + 2;
+        }
+*/
     } else if(rc == 31337) {
         LOG("Cognitive Engine:: Not Found.\n");
@@ -93,28 +115,45 @@
         /* Currently this is hard coded and implementation specific! */
 
+     int Pt = optList->Ptune; 
+ // helps to cycle from on parameter to another.. 
+
 	for (int i = 0; i < radioInfo->numParameters; i++) {
-            returnValues[i] = currentParameters[i].value + pList[i].step;
-            if (returnValues[i] > pList[i].max) {
+	    returnValues[i] = currentParameters[i].value;
+	}
+    
+        returnValues[Pt] = currentParameters[Pt].value + pList[Pt].step*(optList->Slope[Pt]);
+		    
+////////////////////   LIMITER   /////////////////////////////
+            if (returnValues[Pt] > pList[Pt].max) {
 	        std::cout << "SORRY CANT EXCEED MAX VALUE" << std::endl;
-	        returnValues[i] = returnValues[i] - pList[i].step;
+	        returnValues[Pt] = returnValues[Pt] - pList[Pt].step;
+		optList->Ptune += 1;
             }
-	    if (returnValues[i] < pList[i].min) {
+	    if (returnValues[Pt] < pList[Pt].min) {
 		std::cout << "SORRY CANT GO BELOW MIN VALUE" << std::endl;
-	        returnValues[i] = returnValues[i] + pList[i].step ;		 
+	        returnValues[Pt] = returnValues[Pt] + pList[Pt].step ;
+		optList->Ptune += 1;		 
 	    }
-	}
-
-	for (int i = 0; i < radioInfo->numUtilities; i++)
+
+	    if (optList->Ptune > radioInfo->numParameters) {
+		optList->Ptune = 0;		
+	    }
+/////////////////////////////////////////////////////////////
+	
+
+	for (int i = 0; i < radioInfo->numUtilities; i++) {
 	    returnValues[radioInfo->numParameters + i] = uList[i].value;
-
-	for (int i = 0; i < radioInfo->numObservables; i++)
+	}
+
+	for (int i = 0; i < radioInfo->numObservables; i++) {
 	    returnValues[radioInfo->numParameters + radioInfo->numUtilities + i] = oList[i].value;
+	}
 
     size_t returnValueIndex = 0;
-    for(size_t i = 0; i < radioInfo->numUtilities; i++) {
+    for(size_t i = 0; i < radioInfo->numParameters; i++) {
         pList[i].value = returnValues[returnValueIndex];
         returnValueIndex++;
     }
-    for(size_t i = 0; i < radioInfo->numParameters; i++) {
+    for(size_t i = 0; i < radioInfo->numUtilities; i++) {
         uList[i].value = returnValues[returnValueIndex];
         returnValueIndex++;
@@ -143,10 +182,9 @@
 
     // Add row to CBR. 
-    myCBR->AddRow(allNames, returnValues, returnValueIndex + 1);
-
+    myCBR1->AddRow(allNames, returnValues, returnValueIndex + 1);
+    myCBR2->AddRow(allNames, returnValues, returnValueIndex + 1);	
 	} else {
         LOG("Cognitive Engine:: Search return an invalid value.\n");
     }
-
 
     return pList;
@@ -162,5 +200,5 @@
 
     uint32_t whereLen = radioInfo->numParameters;
-    uint32_t setLen = radioInfo->numUtilities + radioInfo->numObservables;
+    uint32_t setLen = radioInfo->numUtilities + radioInfo->numObservables + 1;
 
 
@@ -170,8 +208,53 @@
     string setList[setLen];
 
+/*
+    size_t columnObsIndex = 0;
+    for (size_t i = 0; i < radioInfo->numObservables; i++){
+        obsList[columnObsIndex] = observables[i].name;
+        columnObsIndex++;
+    }  
+    obsList[columnObsIndex] = "utility";
+
+    size_t columnIndex = 0;
+    for (size_t i = 0; i < radioInfo->numParameters; i++){
+        nameList[columnIndex] = parameters[i].name;
+        columnIndex++;
+    }   
+    for (size_t i = 0; i < radioInfo->numUtilities; i++){
+        nameList[columnIndex] = uList[i].name;
+        columnIndex++;
+    }   
+
+    size_t obsValueIndex = 0;
+    for(size_t i = 0; i < radioInfo->numObservables; i++) {
+        obsVals[obsValueIndex] = observables[i].value;
+        obsValueIndex++;
+    }
+
+///  Calculate Utility 
+   float newUtilityValue = 0;
+
+    for(size_t i = 0; i < radioInfo->numUtilities; i++) {
+        newUtilityValue = newUtilityValue + (uList[i].target - observables[i].value);
+    }
+    obsVals[obsValueIndex] = newUtilityValue;
+
+    size_t returnValueIndex = 0;
+    for(size_t i = 0; i < radioInfo->numParameters; i++) {
+        valList[returnValueIndex] = parameters[i].value;
+        returnValueIndex++;
+    }
+    for(size_t i = 0; i < radioInfo->numUtilities; i++) {
+        valList[returnValueIndex] = uList[i].target;
+        returnValueIndex++;
+    }
+*/
+	float TotalWeight = 0;
+	int Penalty = 10;
 
     for(size_t i = 0; i < radioInfo->numParameters; i++) {
         whereList[i] = parameters[i].name;
         whereValue[i] = parameters[i].value;
+	TotalWeight += parameters[i].value*(optList->Pweights[i]);
     }
 
@@ -179,5 +262,9 @@
     for(size_t i = 0; i < radioInfo->numUtilities; i++) {
 	setList[returnValueIndex] = utilities[i].name;
-	setValue[returnValueIndex] = utilities[i].value;   
+	setValue[returnValueIndex] = utilities[i].value;
+	if ((utilities[i].value > uList[i].target && uList[i].goal == "max") || (utilities[i].value < uList[i].target && uList[i].goal == "min")) 
+		TotalWeight += abs(utilities[i].value - uList[i].target)*(optList->Uweights[i]);
+	else
+                TotalWeight += abs(utilities[i].value - uList[i].target)*(optList->Uweights[i])*Penalty;   
         returnValueIndex++;
     }
@@ -187,17 +274,155 @@
 	returnValueIndex++;
     }
-
-
-myCBR->Update(whereList,setList,whereValue,setValue,whereLen,setLen);
-
-}
-
+	setList[returnValueIndex] = "TotalWeight";
+	setValue[returnValueIndex] = TotalWeight; 
+	
+myCBR1->Update(whereList,setList,whereValue,setValue,whereLen,setLen);
+
+// MAIN CODE HERE //
+
+int searchOps[radioInfo->numParameters];
+string searchNames[radioInfo->numParameters];
+float searchVals[radioInfo->numParameters];
+float returnValues[numberColumns];
+
+
+for (int L = 0; L < radioInfo->numParameters ; L++) { 
+   searchOps[L] = 0;
+   searchNames[L] = parameters[L].name;
+   searchVals[L] = parameters[L].value;
+}
+
+
+for (int lp = 0; lp < radioInfo->numParameters; lp++ ) {
+
+   searchOps[lp] = (optList->Slope[lp] == -1)?2:4;   
+
+   uint32_t rc = myCBR1->Search(searchNames, searchOps, searchVals,radioInfo->numParameters, returnValues);
+  if(rc == 0) {
+	std::cout << "case found for learning for parameter : " << lp << std::endl; 
+
+///////////
+	
+		std::cout << "CASE STUDY" << std::endl;
+		std::cout << "returnvalue ber " << returnValues[radioInfo->numParameters] << std::endl;
+		std::cout << "BER current value " << utilities[0].value << std::endl;
+		
+	
+
+/////////
+
+    for (int ul = 0; ul < radioInfo->numUtilities; ul++) {
+	int rl = radioInfo->numParameters + ul;
+	if( abs(1 - returnValues[rl]/utilities[ul].value) > optList->PoC ) {
+	    if(optList->Trend[lp][ul] == 0.0) {
+
+		if ((returnValues[rl] > utilities[ul].value) && optList->Status[lp][ul] >= 0 ) {
+		    optList->Status[lp][ul] += 1;
+		} 
+		else if ( ((returnValues[rl]) < utilities[ul].value) && optList->Status[lp][ul] <= 0) {
+		    optList->Status[lp][ul] -= 1;
+ 		}		
+		else {
+		    optList->Status[lp][ul] = 0;
+		}
+	    
+		if (abs(optList->Status[lp][ul]) > 2) {
+		    optList->Trend[lp][ul] = signof(optList->Status[lp][ul]);
+			std::cout << "TREND GETTING UPDATED" << std::endl;
+		}
+            }
+	    else if ( optList->Trend[lp][ul] == 1 && (returnValues[rl] > utilities[ul].value) && optList->Status[lp][ul] >= 0 ) {
+		    optList->Status[lp][ul] += 1;
+	    }
+	    else if ( optList->Trend[lp][ul] == -1 && (returnValues[rl] > utilities[ul].value) && optList->Status[lp][ul] <= 0) {
+		    optList->Status[lp][ul] -= 1;
+            }
+	    else {
+		    optList->Status[lp][ul] = 0;
+		    optList->Trend[lp][ul] = 0;	
+	    }
+
+
+
+	}
+    }
+  }
+  else if (rc == 31337) {   
+	 std::cout << "No case found for parameter: " << lp << std::endl; 
+  }
+   searchOps[lp] = 0;
+
+
+}
+
+
+// updating slope matrix..
+// AS OF NOW VALID ONLY FOR ONE UTILITY 
+    for (int i = 0; i < radioInfo->numParameters; i++) {
+	if ( (uList[0].goal == "min" && optList->Trend[i][0] < 0) || (uList[0].goal == "max" && optList->Trend[i][0] > 0) ) {
+	   optList->Slope[i] = -1;
+	}
+	else if ( (uList[0].goal == "min" && optList->Trend[i][0] >= 0) || (uList[0].goal == "max" && optList->Trend[i][0] <= 0) ) {  
+	   optList->Slope[i] = 1;	
+	}
+    }
+
+
+   std::cout << "Status1 " << optList->Status[0][0] << std::endl;
+   std::cout << "Status2 " << optList->Status[1][0] << std::endl;
+   std::cout << "Slope1 " << optList->Slope[0] << std::endl;
+   std::cout << "Slope2 " << optList->Slope[1] << std::endl;
+   std::cout << "Trend1 " << optList->Trend[0][0] << std::endl;
+   std::cout << "Trend2 " << optList->Trend[1][0] << std::endl;
+ /// end of MAIN CODE //
+}
 
 void
 CBR_CE::BuildCognitiveEngine()
 {
-    string filename = "ex1";
-    string filename2 = "ex2";
-    string tablename = "data";
+    string filename = "exdb";
+    string tablename1 = "data1";
+    string tablename2 = "data2";
+
+	//// NEW SET OF VARIABLES ///
+
+//	float * Pweights = new float[radioInfo->numParameters];
+	optList->Uweights = new float[radioInfo->numUtilities];
+	optList->Pweights = new float[radioInfo->numParameters];
+
+///// we have to assume 10 utilities maxxx!!!! VERY important
+
+	optList->Status = new float[radioInfo->numParameters][10];
+	optList->Trend = new float[radioInfo->numParameters][10];
+	optList->PoC = 0.01;
+	optList->Slope = new int[radioInfo->numParameters];	
+	optList->Ptune = 0;   // starting with most tunable parameter.. (widest range..)
+
+
+////////////initializing here since initialsetting giving wierd errors/////
+
+        for (int x = 0; x < radioInfo->numParameters; x++) {
+                std::cout << x << std::endl;
+                optList->Pweights[x] = 1/(pList[x].step);
+                optList->Slope[x] = 1;
+        }
+
+
+
+        for (size_t i = 0; i < radioInfo->numUtilities; i++) {
+                optList->Uweights[i] = 1;
+        }
+
+
+        for (size_t i = 0; i < radioInfo->numParameters; i++) {
+                for (size_t k = 0; k < radioInfo->numUtilities; k++) {
+                        optList->Status[i][k] = 0;
+                        optList->Trend[i][k] = 0;
+                }
+        }
+////////////////////////////////////////////////////////////////////////
+
+
+
 
     uint32_t numberColumns = radioInfo->numParameters + radioInfo->numUtilities \
@@ -208,12 +433,5 @@
 
     string cols[numberColumns];
-    string Plist[numberParameters];
-    string Ulist[numberUtilities];
-    float Utarget[numberUtilities];
-    string Ugoal[numberUtilities];
-
     size_t columnIndex = 0;
-    size_t Pindex = 0;              // new
-    size_t Uindex = 0;              // new
 
 
@@ -221,15 +439,8 @@
         cols[columnIndex] = pList[i].name;
         columnIndex++;
-        Plist[Pindex] = pList[i].name;   // new
-        Pindex++;                        // new
     }
     for (size_t i = 0; i < radioInfo->numUtilities; i++){
         cols[columnIndex] = uList[i].name;
         columnIndex++;
-        Ulist[Uindex] = uList[i].name;     // new
-	Utarget[Uindex] = uList[i].target; // new
-	Ugoal[Uindex] = uList[i].goal;	   // new
-        Uindex++;			   // new
-
     }      
     for (size_t i = 0; i < radioInfo->numObservables; i++){   
@@ -239,5 +450,8 @@
     cols[columnIndex] = "TotalWeight";	
 
-    myCBR = new CBR(filename, tablename, cols, numberColumns);
+    myCBR1 = new CBR(filename, tablename1, cols, numberColumns);	
+
+    myCBR2 = new CBR(filename, tablename2, cols, numberColumns);	
+
 
 }
@@ -399,4 +613,31 @@
 }
 
+void CBR_CE::InitialSetting()
+{
+
+	std::cout << "inside initilsetting method" << std::endl; 
+
+	for (int x = 0; x < radioInfo->numParameters; x++) {
+		std::cout << x << std::endl;
+		optList->Pweights[x] = 1/(pList[x].step);
+		optList->Slope[x] = 0;
+	}
+
+
+
+	for (size_t i = 0; i < radioInfo->numUtilities; i++) { 
+		optList->Uweights[i] = 1;
+	}
+
+
+	for (size_t i = 0; i < radioInfo->numParameters; i++) {
+		for (size_t k = 0; k < radioInfo->numUtilities; k++) {
+			optList->Status[i][k] = 0;
+			optList->Trend[i][k] = 0;
+		}
+	}
+
+}
+
 
 void
@@ -412,3 +653,10 @@
     Shutdown();
 }
- 
+
+void
+CBR_CE::FineTune()
+{
+
+	std::cout << "UNDER DEVELOPMENT" << std::endl;
+
+} 
