void NEHCalc::NEH() { int NumberofAddedTasks = 0, Tempi = 0, TempCmax = 0; for (std::vector<SingleTask*>::iterator itr = ListofSortedTasks.begin(); itr!= (ListofSortedTasks.end() -1); itr++) { if (((*itr)->GetLength() == (*(itr+1))->GetLength()) && ((*itr)->GetNumberofTask() > (*(itr+1))->GetNumberofTask())) { std::swap(*(itr), *(itr + 1)); } } std::vector<SingleTask*>::iterator itr = ListofSortedTasks.begin(); UpdateR(*(ListofSortedTasks.begin()), 0, NumberofAddedTasks); UpdateQ(*(ListofSortedTasks.begin()), 0, NumberofAddedTasks++); ListofOrderedTasks.push_back(*(itr)); ListofSortedTasks.erase(itr); while (!ListofSortedTasks.empty()) { TempCmax = CountCmax((*(ListofSortedTasks.begin())), 0, NumberofAddedTasks); Tempi = 0; for (int i = 1; i <= NumberofAddedTasks; i++) { //std::cout << TempCmax <<" " << CountCmax((*(ListofSortedTasks.begin())),i,NumberofAddedTasks) << std::endl; //if ((*ListofSortedTasks.begin())->GetNumberofTask() == 20) //{ // std::cout << TempCmax << " " << CountCmax((*(ListofSortedTasks.begin())), i, NumberofAddedTasks)<<std::endl; //} if (TempCmax > CountCmax((*(ListofSortedTasks.begin())), i, NumberofAddedTasks)) { TempCmax = CountCmax((*(ListofSortedTasks.begin())), i, NumberofAddedTasks); Tempi = i; } } //std::cout << Tempi; UpdateR(*(ListofSortedTasks.begin()), Tempi, NumberofAddedTasks); UpdateQ(*(ListofSortedTasks.begin()), Tempi, NumberofAddedTasks); //ShowRData(); //std::cout << std::endl; //ShowQData(); //std::cout << std::endl; ListofOrderedTasks.insert(ListofOrderedTasks.begin() + Tempi, *(ListofSortedTasks.begin())); ListofSortedTasks.erase(ListofSortedTasks.begin()); /*for (std::vector<SingleTask*>::iterator itr = ListofOrderedTasks.begin(); itr!= ListofOrderedTasks.end(); itr++) { std::cout << (*itr)->GetNumberofTask() << " "; } std::cout << std::endl;*/ NumberofAddedTasks++; } std::cout << "Cmax is: " << TableofR[_NumberofMachines-1][_NumberofTasks-1] << std::endl; //UpdateR((*(ListofSortedTasks.begin()+1)), 0, 0); //UpdateQ((*(ListofSortedTasks.begin()+1)), 0, 0); //ListofOrderedTasks.push_back((*(ListofSortedTasks.begin()+1))); //UpdateR((*(ListofSortedTasks.begin()+3)), 1, 1); //UpdateQ((*(ListofSortedTasks.begin()+3)), 1, 1); //ListofOrderedTasks.push_back((*(ListofSortedTasks.begin()+3))); //std:: cout << CountCmax((*(ListofSortedTasks.begin())), 2, 2) << std::endl; //UpdateR((*(ListofSortedTasks.begin())), 0, 1); /*UpdateQ((*(ListofSortedTasks.begin())), 0, 1); ListofOrderedTasks.push_back((*(ListofSortedTasks.begin()))); std::swap(*(ListofOrderedTasks.begin()),*(ListofOrderedTasks.begin()+1)); UpdateR((*(ListofSortedTasks.begin()+1)), 1, 2); UpdateQ((*(ListofSortedTasks.begin()+1)), 1, 2); ListofOrderedTasks.push_back((*(ListofSortedTasks.begin())));*/ //UpdateQ((*(ListofSortedTasks.begin()+3)), 1, 1); //UpdateR((*(ListofSortedTasks.begin()+3)), 1, 1 ); //ListofOrderedTasks.push_back((*(ListofSortedTasks.begin()+3))); //UpdateR((*(ListofSortedTasks.begin()+2)), 1, 2 );*/ //ListofOrderedTasks.push_back((*(ListofSortedTasks.begin()+2))); //std::swap(*(ListofOrderedTasks.begin()+1),*(ListofOrderedTasks.begin()+2)); //UpdateR((*(ListofSortedTasks.begin()+1)), 1, 3 ); //UpdateR((*(ListofSortedTasks.begin()+2)), 2 ); //UpdateR((*(ListofSortedTasks.begin()+3)), 3 ); //ListofOrderedTasks.push_back((*ListofSortedTasks.begin())); //ListofSortedTasks.pop_front(); }
void ADMMCut::MakeLayers() { // Initial Cutting Edge Setting InitState(); debug_ = false; int cut_count = 0; vector<double> cut_energy; vector<double> res_energy; do { /* Recreate dual graph at the beginning of each cut */ SetStartingPoints(cut_count); CreateA(); ptr_stiff_->CalculateD(D_, x_, 0, 0, cut_count); ///* for rendering */ //if (cut_count == 0) //{ // WriteWeight(); // WriteStiffness("offset1.txt", "rotation1.txt"); // getchar(); //} //if (cut_count == 4) //{ // WriteStiffness("offset2.txt", "rotation2.txt"); // getchar(); //} //if (cut_count == 6) //{ // WriteStiffness("offset3.txt", "rotation3.txt"); // getchar(); //} /* set x for intial cut setting */ SetBoundary(); /* * energy specify: * cut energy : | A * x | * defomation energy : norm(K D - F) */ ptr_stiff_->CreateGlobalK(x_); ptr_stiff_->CreateF(x_); SpMat K_init = *(ptr_stiff_->WeightedK()); VX F_init = *(ptr_stiff_->WeightedF()); double icut_energy = 0; VX V_Cut = A_ * x_; for (int i = 0; i < Md_; i++) { icut_energy += abs(V_Cut[i]); } double ideform_energy = (K_init * D_ - F_init).norm(); cout << "****************************************" << endl; cout << "ADMMCut Round : " << cut_count << endl; cout << "Initial cut energy before entering ADMM: " << icut_energy << endl; cout << "Initial Lagrangian energy before entering ADMM: " << ideform_energy << endl; cout << "---------------------------------" << endl; int rew_count = 0; VX x_prev; VX D_prev; /* Output energy list for reweighting process in a single graph cut problem, energy.size() = number of reweighting process performed. cut energy = |A_ * x_| = sum_{e_ij} w_ij * |x_i - x_j| res energy = (K(x)D - F(x)).norm() */ cut_energy.clear(); res_energy.clear(); cut_energy.push_back(icut_energy); res_energy.push_back(ideform_energy); do { /* Reweighting loop for cut */ int ADMM_count = 0; x_prev = x_; CreateC(cut_count, rew_count); do { cout << "ADMMCut Round: " << cut_count << ", reweight iteration:" << rew_count << ", ADMM " << ADMM_count << " iteration." << endl; /*-------------------ADMM loop-------------------*/ CalculateX(); D_prev = D_; CalculateD(); UpdateLambda(); /*-------------------Residual Calculation-------------------*/ SpMat Q_prev; SpMat Q_new; CalculateQ(D_prev, Q_prev); CalculateQ(D_, Q_new); /* Update K reweighted by new x */ ptr_stiff_->CreateGlobalK(x_); ptr_stiff_->CreateF(x_); SpMat K_new = *(ptr_stiff_->WeightedK()); VX F_new = *(ptr_stiff_->WeightedF()); dual_res_ = penalty_ * (D_prev - D_).transpose() * K_new.transpose() * Q_prev + lambda_.transpose() * (Q_prev - Q_new); primal_res_ = K_new * D_ - F_new; /*-------------------Screenplay-------------------*/ double new_cut_energy = x_.dot(H1_ * x_); cout << "new quadratic func value record: " << new_cut_energy << endl; cout << "dual_residual : " << dual_res_.norm() << endl; cout << "primal_residual(KD-F) : " << primal_res_.norm() << endl; cout << "---------------------" << endl; ADMM_count++; } while (!TerminationCriteria(ADMM_count)); /* One reweighting process ended! */ /* Output energy and residual */ double energy = 0; VX V_Cut = A_ * x_; for (int i = 0; i < Md_; i++) { energy += ptr_dualgraph_->Weight(i) * abs(V_Cut[i]); } /*-------------------Screenplay-------------------*/ double res_tmp = primal_res_.norm(); cout << "Cut " << cut_count << " Reweight " << rew_count << " completed." << endl; cout << "Cut Energy :" << energy << endl; cout << "Res Energy :" << res_tmp << endl; cout << "<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-" << endl; cut_energy.push_back(energy); res_energy.push_back(res_tmp); /* write x distribution to a file */ string str_x = "Cut_" + to_string(cut_count) + "_Rew_" + to_string(rew_count) + "_x"; Statistics tmp_x(str_x, x_); tmp_x.GenerateVectorFile(); rew_count++; } while (!UpdateR(x_prev, rew_count)); /* Output reweighting energy history for last cut process */ string str_eC = "Cut_" + to_string(cut_count) + "_Cut_Energy"; Statistics s_eC(str_eC, cut_energy); s_eC.GenerateStdVecFile(); string str_eR = "Cut_" + to_string(cut_count) + "_Res_Energy"; Statistics s_eR(str_eR, res_energy); s_eR.GenerateStdVecFile(); /* Update New Cut information to Rendering (layer_label_) */ UpdateCut(); fprintf(stdout, "ADMMCut No.%d process is Finished!\n", cut_count); cut_count++; } while (!CheckLabel(cut_count)); fprintf(stdout, "All done!\n"); }