Changeset 1212 for trunk/Cbc/src/CbcHeuristicLocal.cpp
 Timestamp:
 Aug 21, 2009 12:19:13 PM (10 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/Cbc/src/CbcHeuristicLocal.cpp
r1211 r1212 37 37 // Get a copy of original matrix 38 38 assert(model.solver()); 39 matrix_ = *model.solver()>getMatrixByCol(); 39 if (model.solver()>getNumRows()) { 40 matrix_ = *model.solver()>getMatrixByCol(); 41 } 40 42 int numberColumns = model.solver()>getNumCols(); 41 43 used_ = new char[numberColumns]; … … 599 601 // Get a copy of original matrix 600 602 assert(model_>solver()); 601 matrix_ = *model_>solver()>getMatrixByCol(); 603 if (model_>solver()>getNumRows()) { 604 matrix_ = *model_>solver()>getMatrixByCol(); 605 } 602 606 delete [] used_; 603 607 int numberColumns = model>solver()>getNumCols(); … … 636 640 { 637 641 CbcHeuristicNaive other; 638 fprintf(fp,"0#include \"CbcHeuristic Naive.hpp\"\n");642 fprintf(fp,"0#include \"CbcHeuristicLocal.hpp\"\n"); 639 643 fprintf(fp,"3 CbcHeuristicNaive naive(*cbcModel);\n"); 640 644 CbcHeuristic::generateCpp(fp,"naive"); … … 876 880 model_ = model; 877 881 } 882 // Default Constructor 883 CbcHeuristicCrossover::CbcHeuristicCrossover() 884 :CbcHeuristic(), 885 numberSolutions_(0), 886 useNumber_(3) 887 { 888 setWhen(1); 889 } 890 891 // Constructor with model  assumed before cuts 892 893 CbcHeuristicCrossover::CbcHeuristicCrossover(CbcModel & model) 894 :CbcHeuristic(model), 895 numberSolutions_(0), 896 useNumber_(3) 897 { 898 setWhen(1); 899 for (int i=0;i<10;i++) 900 random_[i]=model.randomNumberGenerator()>randomDouble(); 901 } 902 903 // Destructor 904 CbcHeuristicCrossover::~CbcHeuristicCrossover () 905 { 906 } 907 908 // Clone 909 CbcHeuristic * 910 CbcHeuristicCrossover::clone() const 911 { 912 return new CbcHeuristicCrossover(*this); 913 } 914 // Create C++ lines to get to current state 915 void 916 CbcHeuristicCrossover::generateCpp( FILE * fp) 917 { 918 CbcHeuristicCrossover other; 919 fprintf(fp,"0#include \"CbcHeuristicLocal.hpp\"\n"); 920 fprintf(fp,"3 CbcHeuristicCrossover crossover(*cbcModel);\n"); 921 CbcHeuristic::generateCpp(fp,"crossover"); 922 if (useNumber_!=other.useNumber_) 923 fprintf(fp,"3 crossover.setNumberSolutions(%d);\n",useNumber_); 924 else 925 fprintf(fp,"4 crossover.setNumberSolutions(%d);\n",useNumber_); 926 fprintf(fp,"3 cbcModel>addHeuristic(&crossover);\n"); 927 } 928 929 // Copy constructor 930 CbcHeuristicCrossover::CbcHeuristicCrossover(const CbcHeuristicCrossover & rhs) 931 : 932 CbcHeuristic(rhs), 933 attempts_(rhs.attempts_), 934 numberSolutions_(rhs.numberSolutions_), 935 useNumber_(rhs.useNumber_) 936 { 937 memcpy(random_,rhs.random_,10*sizeof(double)); 938 } 939 940 // Assignment operator 941 CbcHeuristicCrossover & 942 CbcHeuristicCrossover::operator=( const CbcHeuristicCrossover& rhs) 943 { 944 if (this!=&rhs) { 945 CbcHeuristic::operator=(rhs); 946 useNumber_ = rhs.useNumber_; 947 attempts_ = rhs.attempts_; 948 numberSolutions_ = rhs.numberSolutions_; 949 memcpy(random_,rhs.random_,10*sizeof(double)); 950 } 951 return *this; 952 } 953 954 // Resets stuff if model changes 955 void 956 CbcHeuristicCrossover::resetModel(CbcModel * model) 957 { 958 CbcHeuristic::resetModel(model); 959 } 960 int 961 CbcHeuristicCrossover::solution(double & solutionValue, 962 double * betterSolution) 963 { 964 if (when_==0) 965 return 0; 966 numCouldRun_++; 967 bool useBest=(numberSolutions_!=model_>getSolutionCount()); 968 if (!useBest&&(when_%10)==1) 969 return 0; 970 numberSolutions_=model_>getSolutionCount(); 971 OsiSolverInterface * continuousSolver = model_>continuousSolver(); 972 int useNumber =CoinMin(model_>numberSavedSolutions(),useNumber_); 973 if (useNumber<2!continuousSolver) 974 return 0; 975 // Fix later 976 if (!useBest) 977 abort(); 978 numRuns_++; 979 double cutoff; 980 model_>solver()>getDblParam(OsiDualObjectiveLimit,cutoff); 981 double direction = model_>solver()>getObjSense(); 982 cutoff *= direction; 983 cutoff = CoinMin(cutoff,solutionValue); 984 OsiSolverInterface * solver = cloneBut(2); 985 // But reset bounds 986 solver>setColLower(continuousSolver>getColLower()); 987 solver>setColUpper(continuousSolver>getColUpper()); 988 int numberColumns = solver>getNumCols(); 989 // Fixed 990 double * fixed =new double [numberColumns]; 991 for (int i=0;i<numberColumns;i++) 992 fixed[i]=COIN_DBL_MAX; 993 int whichSolution[10]; 994 for (int i=0;i<useNumber;i++) 995 whichSolution[i]=i; 996 for (int i=0;i<useNumber;i++) { 997 int k = whichSolution[i]; 998 const double * solution = model_>savedSolution(k); 999 for (int j=0;j<numberColumns;j++) { 1000 if (solver>isInteger(j)) { 1001 if (fixed[j]==COIN_DBL_MAX) 1002 fixed[j]=floor(solution[j]+0.5); 1003 else if (fabs(fixed[j]solution[j])>1.0e7) 1004 fixed[j]=COIN_DBL_MAX; 1005 } 1006 } 1007 } 1008 const double * colLower = solver>getColLower(); 1009 for (int i=0;i<numberColumns;i++) { 1010 if (solver>isInteger(i)) { 1011 double value=fixed[i]; 1012 if (value!=COIN_DBL_MAX) { 1013 if (when_<10) { 1014 solver>setColLower(i,value); 1015 solver>setColUpper(i,value); 1016 } else if (value==colLower[i]) { 1017 solver>setColUpper(i,value); 1018 } 1019 } 1020 } 1021 } 1022 int returnCode = smallBranchAndBound(solver,numberNodes_,betterSolution, 1023 solutionValue, 1024 solutionValue,"CbcHeuristicCrossover"); 1025 if (returnCode<0) 1026 returnCode=0; // returned on size 1027 if ((returnCode&2)!=0) { 1028 // could add cut 1029 returnCode &= ~2; 1030 } 1031 1032 delete solver; 1033 return returnCode; 1034 } 1035 // update model 1036 void CbcHeuristicCrossover::setModel(CbcModel * model) 1037 { 1038 model_ = model; 1039 if (model) { 1040 for (int i=0;i<10;i++) 1041 random_[i]=model>randomNumberGenerator()>randomDouble(); 1042 } 1043 } 878 1044 879 1045
Note: See TracChangeset
for help on using the changeset viewer.