瀏覽代碼

fixed ICE dependence; added cmake support

Johannes Ruehle 11 年之前
父節點
當前提交
bc9d3688c3
共有 6 個文件被更改,包括 61 次插入18 次删除
  1. 25 3
      AdaptiveDirectionRandomSearchOptimizer.cpp
  2. 2 2
      AdditionalIceUtils.cpp
  3. 7 9
      CMakeLists.txt
  4. 24 1
      CombinatorialOptimizer.cpp
  5. 1 1
      Opt_Namespace.h
  6. 2 2
      corefiles.cmake

+ 25 - 3
AdaptiveDirectionRandomSearchOptimizer.cpp

@@ -12,7 +12,7 @@
 #include <time.h>
 #include <iostream>
 
-#include "numbase.h" // ice random
+//#include "numbase.h" // ice random
 #include "optimization/AdditionalIceUtils.h"
 #include "core/optimization/blackbox/Definitions_core_opt.h"
 
@@ -190,7 +190,11 @@ void AdaptiveDirectionRandomSearchOptimizer::init()
   /*
     seed rand
   */
+#ifdef NICE_USE_ICE
   ice::Randomize();
+#else
+  srand(0);
+#endif
 
   /*
     check if bounds are active! bounds are needed
@@ -218,8 +222,11 @@ void AdaptiveDirectionRandomSearchOptimizer::init()
     {
       for(int l = 0; l < static_cast<int>(m_numberOfParallelPoints);l++)
       {
+#ifdef NICE_USE_ICE
         m_Parameters(k,l) = m_parameters(k,0) + m_scales(k,0) *2.0* (ice::RandomD() - 0.5);
-        
+#else
+        m_Parameters(k,l) = m_parameters(k,0) + m_scales(k,0) *2.0* (drand48() - 0.5);
+#endif
       }
     }
   }
@@ -232,7 +239,11 @@ void AdaptiveDirectionRandomSearchOptimizer::init()
     {
       for(int l = 0; l < static_cast<int>(m_numberOfParallelPoints);l++)
       {
+#ifdef NICE_USE_ICE
         m_Parameters(k,l) = m_advancedinitLowerBounds(k,0) +ice::RandomD() * (m_advancedinitUpperBounds(k,0) - m_advancedinitLowerBounds(k,0) ) ;
+#else
+        m_Parameters(k,l) = m_advancedinitLowerBounds(k,0) +drand48()* (m_advancedinitUpperBounds(k,0) - m_advancedinitLowerBounds(k,0) ) ;
+#endif
       }
     }
   }
@@ -267,8 +278,11 @@ void AdaptiveDirectionRandomSearchOptimizer::init()
       {
         for(int k = 0; k < static_cast<int>(m_numberOfParameters);k++)
         {
+#ifdef NICE_USE_ICE
           m_Parameters(k,u) = m_parameters(k,0) + m_scales(k,0) * 2.0*(ice::RandomD() - 0.5);
-            
+#else
+            m_Parameters(k,u) = m_parameters(k,0) + m_scales(k,0) * 2.0*(drand48() - 0.5);
+#endif
         }
         
         /*
@@ -355,7 +369,11 @@ OPTIMIZATION::matrix_type AdaptiveDirectionRandomSearchOptimizer::generatePoint(
   {
     if(m_scales(i,0) > 0.0 )
     {
+#ifdef NICE_USE_ICE
       newPoint(i,0) = m_scales(i,0) * 2.0*(ice::RandomD() - 0.5);
+#else
+        newPoint(i,0) = m_scales(i,0) * 2.0*(drand48() - 0.5);
+#endif
     }
   }
 
@@ -718,7 +736,11 @@ int AdaptiveDirectionRandomSearchOptimizer::optimize()
         /*
           generate random number [0,1]
         */
+#ifdef NICE_USE_ICE
         double choice = ice::RandomD();
+#else
+        double choice = drand48();
+#endif
         int u_chosen = 0;
 
         for(int u = 0; u < static_cast<int>(m_numberOfParallelPoints); u++)

+ 2 - 2
AdditionalIceUtils.cpp

@@ -1,5 +1,5 @@
 #include "optimization/AdditionalIceUtils.h"
-#include "vectort.h" // ICE
+//#include "vectort.h" // ICE
 // #include "ludecomp.h" //ICE
 // #include "mateigen.h" //ICE
 #include <cassert>
@@ -7,7 +7,7 @@
 #include <core/algebra/LUDecomposition.h>
 #include <core/algebra/EigValues.h>
 using namespace opt;
-using namespace ice;
+//using namespace ice;
 
 matrix_type & MatrixAddVal ( matrix_type & mat, double val )
 {

+ 7 - 9
CMakeLists.txt

@@ -4,21 +4,19 @@
 #library name (name is appended to "nice_" to form the target library name)
 set(the_library "optimization")
 
-#define variable nice_<libname>_HDR and nice_<libname>_SRC for library header and source files (don't include progs and test source files here)
-include( corefiles.cmake)
-
-#define variable nice_<libname>_PROGFILES_HDR and nice_<libname>_PROGFILES_SRC for program header and source files (don't include library and test source files here)
-include( progfiles.cmake)
-
-#define variable nice_<libname>_TESTFILES_HDR and nice_<libname>_TESTFILES_SRC for unit test header and source files (don't include library and progs source files here)
-include( testfiles.cmake)	
-
 #add linkage dependencies to other libraries here
 set("nice_${the_library}_LINKING_DEPENDENCIES" "nice_core")
 
 #####################################################
 message(STATUS "adding library ${the_library}")
 
+# switch back to build the library, programs and tests from source files defined in the 
+# corefiles.cmake, progfiles.cmake and testfiles.cmake. Why not using the recursively collected
+# source files? Because of the testfile tests/MyCostFunction.cpp, which would be recognized as a unit test,
+# but only is an include by unit test TestGradientDescent
+set(NICE_SOURCEFILES_FIND_GLOBALLYRECURSIVE 0)
+nice_get_source_files()
+
 nice_build_library()
 
 nice_add_progs()

+ 24 - 1
CombinatorialOptimizer.cpp

@@ -6,10 +6,13 @@
 //////////////////////////////////////////////////////////////////////
 
 #include "optimization/CombinatorialOptimizer.h"
+#include "core/basics/Exception.h"
 #include <stdlib.h>
 #include <time.h>
 
+#ifdef NICE_USE_ICE
 #include "numbase.h" // ice random
+#endif
 
 #include <iostream>
 
@@ -86,7 +89,11 @@ void CombinatorialOptimizer::init()
   /*
     seed rand
   */
+#ifdef NICE_USE_ICE
   ice::Randomize();
+#else
+  srand(0);
+#endif
 
   /*
     (re)set m_Tk
@@ -149,7 +156,11 @@ bool CombinatorialOptimizer::accepptPoint(double oldValue, double newValue)
     /*
       generate uniform distrubuted random number
     */
+#ifdef NICE_USE_ICE
     double lambda = ice::RandomD();
+#else
+    double lambda = drand48();
+#endif
     
     if(lambda <= exp(-(newValue - oldValue)/((m_Tk < 1.0e-20)? 1.0e-20 :m_Tk)))
     {
@@ -211,7 +222,11 @@ bool CombinatorialOptimizer::accepptPoint(double oldValue, double newValue)
     /*
       generate uniform distrubuted random number
     */
+#ifdef NICE_USE_ICE
     double lambda = ice::RandomD();
+#else
+    double lambda = drand48();
+#endif
     
     if(lambda <= exp(-(newValue - oldValue)/((m_Tk < 1.0e-20)? 1.0e-20 :m_Tk)))
     {
@@ -239,11 +254,19 @@ matrix_type CombinatorialOptimizer::generatePoint()
       if(m_fast == true)
       {
         //newPoint(i,0) = m_parameters(i,0) + m_stepLength * m_Tk * ice::GaussRandom(1.0) ;
-        newPoint(i,0) = m_parameters(i,0) + m_scales(i,0) * m_Tk * ice::GaussRandom(1.0) ;
+#ifdef NICE_USE_ICE
+          newPoint(i,0) = m_parameters(i,0) + m_scales(i,0) * m_Tk * ice::GaussRandom(1.0) ;
+#else
+      fthrow ( NICE::Exception, "CombinatorialOptimizer::generatePoint(): this functions needs the ICE library (NICE_USE_ICE)" );
+#endif
       }
       else
       {
+#ifdef NICE_USE_ICE
         newPoint(i,0) = m_parameters(i,0) + m_scales(i,0) * ice::GaussRandom(1.0) ;
+#else
+      fthrow ( NICE::Exception, "CombinatorialOptimizer::generatePoint(): this functions needs the ICE library (NICE_USE_ICE)" );
+#endif
       }
     }
   }

+ 1 - 1
Opt_Namespace.h

@@ -10,7 +10,7 @@
 
 //#include <matrix.h>
 //#include <image.h>
-#include <MatrixO.h>
+//#include <MatrixO.h>
 //using namespace ice;
 
 namespace OPTIMIZATION

+ 2 - 2
corefiles.cmake

@@ -12,7 +12,7 @@ SET(nice_optimization_SRC
 ./EmptyLog.cpp
 ./Plotter.cpp
 ./GoldenCutLineSearcher.cpp
-#./AdaptiveDirectionRandomSearchOptimizer.cpp
+./AdaptiveDirectionRandomSearchOptimizer.cpp
 ./CostFunction_ndim_2ndOrder.cpp
 ./FileLog.cpp
 ./ArmijoLineSearcher.cpp
@@ -20,7 +20,7 @@ SET(nice_optimization_SRC
 ./MatrixIterativeOptimizer.cpp
 ./LineSearcher.cpp
 ./GradientDescentOptimizer.cpp
-#./CombinatorialOptimizer.cpp 
+./CombinatorialOptimizer.cpp 
 ./tests/MyCostFunction.cpp
 )