int main() { Verifier v; string fileName; cout << "Sudoku Verifier\n"; for (int i = 1; i <= NUM_FILES; i++) { cout << endl; // Construct file pathname fileName = string("testpuzzle") + (char)('0' + i) + ".txt"; // Read the solution file as input v.readGrid(fileName.c_str()); // Print the Sudoku grid v.printGrid(); // Verify whether or not the solution is correct if (v.verifySolution()) cout << "\nThis is a valid Sudoku solution\n"; else cout << "\nThis is not a valid Sudoku solution\n"; } system("pause"); return 0; }
int main(int argc, char **argv) { #if doMPI if ( MPI_SUCCESS != MPI_Init( & argc , & argv ) ) { std::cerr << "MPI_Init FAILED" << std::endl ; std::abort(); } #endif Verifier vf; vf.verify(argc, argv); #if doMPI #endif // MPI_Finalize(); return 0; }
void VNS::run() { const ProcessCount pCount = instance().processes().size(); // read configuration const uint32_t kMin = hasParam("kMin") ? param<uint32_t>("kMin") : 2; const uint32_t kMax = hasParam("kMax") ? param<uint32_t>("kMax") : 5; const uint32_t trials = hasParam("trials") ? param<uint32_t>("trials") : 5; const uint32_t maxInf = hasParam("maxInf") ? param<uint32_t>("maxInf") : 3; m_maxTrialsLS = param<uint64_t>("maxTrialsLS", defaultMaxTrialsLS()); m_maxSamplesLS = param<uint64_t>("maxSamplesLS", pCount); // initialize RNG m_rng.seed(seed()); // initialize shaker SmartShaker shaker(trials, maxInf); shaker.init(instance(), initial(), seed(), flag()); // initialize solution info m_current.reset(new SolutionInfo(instance(), initial())); // run local search to get starting solution localSearch(); // set best and publish to pool m_best = std::move(m_current); pool().push(m_best->objective(), m_best->solution()); // start VNS uint64_t it = 0; uint32_t k = kMin; uint64_t syncPeriod = 100; uint64_t diversifyPeriod = 100; uint64_t concentratePeriod = 100; #ifdef CHECK_VNS Verifier v; #endif while (!interrupted()) { ++it; // periodically sync with best result if (it % syncPeriod == 0) { SolutionPool::Entry entry; if (pool().best(entry)) { if (entry.obj() < m_best->objective()) { m_best.reset(new SolutionInfo(instance(), initial(), *(entry.ptr()))); k = kMin; } } } // periodically start from a good solution different from the best known one if (it % diversifyPeriod == 25) { SolutionPool::Entry hdEntry; if (pool().randomHighDiversity(hdEntry)) { m_current.reset(new SolutionInfo(instance(), initial(), *hdEntry.ptr())); } } else if (it % concentratePeriod == 75) { SolutionPool::Entry hqEntry; if (pool().randomHighQuality(hqEntry)) { m_current.reset(new SolutionInfo(instance(), initial(), *hqEntry.ptr())); } } else { // most of the times start from the best known solution m_current.reset(new SolutionInfo(*m_best)); } // make random jump shaker.shake(k, *m_current); // since shaking might take a long time, check for interruption if (interrupted()) { #ifdef TRACE_VNS std::cout << "VNS - Interrupted during shake @ iteration " << it << std::endl; #endif break; } #ifdef CHECK_VNS // verify shaking led to a feasible solution if (!v.verify(instance(), initial(), m_current->solution()).feasible()) { throw std::runtime_error("VNS - Shaking lead to an unfeasible solution"); } #endif // do a local search localSearch(); #ifdef CHECK_VNS // verify shaking led to a feasible solution if (!v.verify(instance(), initial(), m_current->solution()).feasible()) { throw std::runtime_error("VNS - Local search lead to an unfeasible solution"); } #endif // update best solution if improved if needed if (m_current->objective() < m_best->objective()) { m_best = std::move(m_current); #ifdef TRACE_VNS std::cout << "VNS - Improvement found @ iteration " << it << ": " << m_best->objective() << std::endl; #endif pool().push(m_best->objective(), m_best->solution()); // restart from small neighborhood k = kMin; } else { // move to another neighborhood k = kMin + (k - kMin + 1) % (kMax - kMin + 1); } } #ifdef TRACE_VNS std::cout << "VNS - Completed after " << it << " iterations" << std::endl; std::cout << "VNS - Failures per shake " << shaker.failuresPerShake() << std::endl; #endif signalCompletion(); }
int main(int argc, char** argv) { Galois::StatManager statManager; LonestarStart(argc, argv, name, desc, url); graph = new Graph(); { Mesh m; m.read(graph, filename.c_str(), detAlgo == nondet); Verifier v; if (!skipVerify && !v.verify(graph)) { std::cerr << "bad input mesh\n"; assert(0 && "Refinement failed"); abort(); } } std::cout << "configuration: " << std::distance(graph->begin(), graph->end()) << " total triangles, " << std::count_if(graph->begin(), graph->end(), is_bad(graph)) << " bad triangles\n"; Galois::Statistic("MeminfoPre1", GaloisRuntime::MM::pageAllocInfo()); Galois::preAlloc(15 * numThreads + GaloisRuntime::MM::pageAllocInfo() * 10); Galois::Statistic("MeminfoPre2", GaloisRuntime::MM::pageAllocInfo()); Galois::StatTimer T; T.start(); if (detAlgo == nondet) Galois::do_all_local(*graph, Preprocess()); else std::for_each(graph->begin(), graph->end(), Preprocess()); Galois::Statistic("MeminfoMid", GaloisRuntime::MM::pageAllocInfo()); Galois::StatTimer Trefine("refine"); Trefine.start(); using namespace GaloisRuntime::WorkList; typedef LocalQueues<dChunkedLIFO<256>, ChunkedLIFO<256> > BQ; typedef ChunkedAdaptor<false,32> CA; switch (detAlgo) { case nondet: Galois::for_each_local<CA>(wl, Process<>()); break; case detBase: Galois::for_each_det(wl.begin(), wl.end(), Process<>()); break; case detPrefix: Galois::for_each_det(wl.begin(), wl.end(), Process<detPrefix>(), Process<>()); break; case detDisjoint: Galois::for_each_det(wl.begin(), wl.end(), Process<detDisjoint>()); break; default: std::cerr << "Unknown algorithm" << detAlgo << "\n"; abort(); } Trefine.stop(); T.stop(); Galois::Statistic("MeminfoPost", GaloisRuntime::MM::pageAllocInfo()); if (!skipVerify) { int size = Galois::ParallelSTL::count_if(graph->begin(), graph->end(), is_bad(graph)); if (size != 0) { std::cerr << size << " bad triangles remaining.\n"; assert(0 && "Refinement failed"); abort(); } Verifier v; if (!v.verify(graph)) { std::cerr << "Refinement failed.\n"; assert(0 && "Refinement failed"); abort(); } std::cout << "Refinement OK\n"; } return 0; }
bool Generalization::EvaluateRisk( const vector<long>& rVecAttr, Verifier& rVerifier ) { try { AnonyException temp_exception; temp_exception.m_bShallContinue = true; stringstream temp_sstream; if (rVecAttr.size() > m_vecQiAttr.size()) { temp_sstream << "Generalization::EvaluateRisk Error: Incorrect number of attributes"; temp_exception.SetMsg( temp_sstream.str() ); throw( temp_exception ); } vector<long> vec_attr = rVecAttr; sort( vec_attr.begin(), vec_attr.end() ); long i, j; j = 0; for (i = 0; i < vec_attr.size(); ++i) { while (m_vecQiAttr[j].m_nAttr < vec_attr[i] && m_vecQiAttr[j].m_nAttr != vec_attr[i]) { ++j; if (j == m_vecQiAttr.size()) { temp_sstream << "Generalization::EvaluateRisk Error: Input attributes are not all quasi-identifiers"; temp_exception.SetMsg( temp_sstream.str() ); throw( temp_exception ); } } } const vector<Metadata>& r_meta = m_pMicrodata->GetVecMeta(); const vector<UnitValue>& r_micro = m_pMicrodata->GetVecTuple(); const long attr_num = r_meta.size(); const long tuple_num = r_micro.size() / attr_num; const long qi_num = m_vecQiAttr.size(); m_vecRisk.clear(); m_vecRisk.insert( m_vecRisk.end(), tuple_num, 0 ); list<EqvClass>::iterator eqv_pos; list<long>::const_iterator tpl_pos; if (rVecAttr.size() == m_vecQiAttr.size()) { vector<EqvClass*> vec_pt_eqv; vec_pt_eqv.push_back( NULL ); for (eqv_pos = m_lstEqvClass.begin(); eqv_pos != m_lstEqvClass.end(); ++eqv_pos) { vec_pt_eqv[0] = &(*eqv_pos); rVerifier.SetRisk( vec_pt_eqv, m_vecRisk ); } } else { pair< vector<UnitValue>, vector<EqvClass*> > key_pair; key_pair.first.insert( key_pair.first.end(), rVecAttr.size(), UnitValue() ); map<vector< UnitValue>, vector<EqvClass*> > emap; map<vector< UnitValue>, vector<EqvClass*> >::iterator emap_pos; vector<map< vector<UnitValue>, vector<EqvClass*> >::iterator> vec_pos; vec_pos.reserve( m_lstEqvClass.size() ); pair< map<vector<UnitValue>, vector<EqvClass*> >::iterator, bool> rslt_pair; map<UnitValue, long>::iterator sv_pos; for (eqv_pos = m_lstEqvClass.begin(); eqv_pos != m_lstEqvClass.end(); ++eqv_pos) { EqvClass& r_eqv = *eqv_pos; for (j = 0; j < rVecAttr.size(); ++j) { if (r_eqv.m_vecNodePos[rVecAttr[j]] != -1) { key_pair.first[j] = r_eqv.m_vecNodePos[rVecAttr[j]]; } else { key_pair.first[j] = r_micro[r_eqv.m_lstTplId.front() * attr_num + rVecAttr[j]]; } } emap_pos = emap.find( key_pair.first ); if (emap_pos == emap.end()) { rslt_pair = emap.insert( key_pair ); (*rslt_pair.first).second.push_back( &r_eqv ); vec_pos.push_back( rslt_pair.first ); } else { (*emap_pos).second.push_back( &r_eqv ); } } for (emap_pos = emap.begin(); emap_pos != emap.end(); ++emap_pos) { rVerifier.SetRisk( (*emap_pos).second, m_vecRisk ); } } for (i = 0; i < tuple_num; ++i) { if (m_vecStatus[i] != anony::NORMAL) { m_vecRisk[i] = 0; } } } catch( AnonyException e ) { m_vecRisk.clear(); cout << e.GetMsg() << endl; throw( e ); } return true; }
bool Generalization::Anonymize( Verifier& rVerifier ) { try { AnonyException temp_exception; temp_exception.m_bShallContinue = true; stringstream temp_sstream; if (m_pMicrodata == NULL) { temp_sstream << "Generalization::Anonymize Error: Pointer to the microdata is NULL"; temp_exception.SetMsg( temp_sstream.str() ); throw( temp_exception ); } if (m_eqvRootClass.m_lstTplId.size() == 0) { temp_sstream << "Generalization::Anonymize Error: Root equivalence class has not been initialized"; temp_exception.SetMsg( temp_sstream.str() ); throw( temp_exception ); } // Check whether the tuples in the "root" equivalence class need to be reloaded or not if (m_bRootDirty == true) { InitRootClass(); m_bRootDirty = false; } long i, j, k; long temp_cnt; temp_cnt = 0; // If the "root" equivalence class does not satisfy the privacy criterion, stop here if (rVerifier.Verify( m_eqvRootClass ) == false) { temp_sstream << "Generalization::Anonymize Error: No generalization satisfies the given requirement"; temp_exception.SetMsg( temp_sstream.str() ); throw( temp_exception ); } const vector<Metadata>& r_meta = m_pMicrodata->GetVecMeta(); const vector<UnitValue>& r_micro = m_pMicrodata->GetVecTuple(); const long attr_num = r_meta.size(); const long tuple_num = r_micro.size() / attr_num; const long qi_num = m_vecQiAttr.size(); // Initialize m_vecGenLevel m_vecGenLevel.clear(); m_vecGenLevel.insert( m_vecGenLevel.end(), attr_num, -1 ); for (i = 0; i < qi_num; ++i) { const vector<long>& r_vec_level = m_vecHrch[m_vecQiAttr[i].m_nAttr].GetVecLevel(); m_vecGenLevel[m_vecQiAttr[i].m_nAttr] = r_vec_level.size(); } // Starts from the coarsest generalization, i.e., a generalization based on the "root" equivalence class m_lstEqvClass.clear(); m_lstEqvClass.push_back( m_eqvRootClass ); list<EqvClass> temp_lst_eqvcls; list<EqvClass> done_lst_eqvcls; vector<AttrWghtPair> vec_attr_order = m_vecQiAttr; map<UnitValue, list<EqvClass>::iterator> map_eqvcls; map<UnitValue, list<EqvClass>::iterator>::iterator map_pos; pair<long, list<EqvClass>::iterator> temp_pair; long failure_cnt = 0; bool gen_failed; HrchNode temp_node; // Iteratively refine the generalization while (failure_cnt < qi_num) { // Sort the attributes according to their weights long cur_attr; sort( vec_attr_order.begin(), vec_attr_order.end(), CompareByWght() ); // Try to refine the attribute with the highest weight; if fail, try the attribute with the second highest weight, and so on for (i = 0; i < qi_num; ++i) { cur_attr = vec_attr_order[i].m_nAttr; if (m_vecGenLevel[cur_attr] == 0) { ++failure_cnt; // If we have reached the leaf level of attribute cur_attr, we are done with it vec_attr_order[i].m_fWght = 0; continue; } // Try to refine attribute cur_attr list<EqvClass>::iterator cls_pos; gen_failed = false; for (cls_pos = m_lstEqvClass.begin(); cls_pos != m_lstEqvClass.end(); ++cls_pos) // Check each equivalence class { // Refine the equivalence class along attribute cur_attr map_eqvcls.clear(); const EqvClass& r_eqvcls = *cls_pos; const vector<HrchNode> r_vec_hrch = m_vecHrch[cur_attr].GetVecNode(); long node_begin, node_end; if (r_eqvcls.m_vecNodePos[cur_attr] < 0) { node_begin = node_end = -1; } else { node_begin = r_vec_hrch[r_eqvcls.m_vecNodePos[cur_attr]].m_nChildLeft; node_end = r_vec_hrch[r_eqvcls.m_vecNodePos[cur_attr]].m_nChildRight; } list<long>::const_iterator tpl_pos; vector<HrchNode>::const_iterator node_pos; map<UnitValue, long>::iterator sen_pos; UnitValue eqvcls_key; for (tpl_pos = r_eqvcls.m_lstTplId.begin(); tpl_pos != r_eqvcls.m_lstTplId.end(); ++tpl_pos) { long tpl_no = *tpl_pos; if (node_begin == -1) { eqvcls_key = r_micro[tpl_no * attr_num + cur_attr]; } else { temp_node.m_uInterval.m_uLeft = temp_node.m_uInterval.m_uRight = r_micro[tpl_no * attr_num + cur_attr]; node_pos = lower_bound( r_vec_hrch.begin() + node_begin, r_vec_hrch.begin() + node_end + 1, temp_node ); eqvcls_key = long( distance( r_vec_hrch.begin(), node_pos ) ); if (eqvcls_key.l == node_end + 1) { temp_sstream << "Generalization::Anonymize Error: Value " << cur_attr << " of tuple " << tpl_no << " is not covered in level " << m_vecGenLevel[cur_attr] << " of the hierarchy"; temp_exception.SetMsg( temp_sstream.str() ); throw( temp_exception ); } } map_pos = map_eqvcls.find( eqvcls_key ); if (map_pos != map_eqvcls.end()) { EqvClass& r_target = *((*map_pos).second); r_target.m_lstTplId.push_back( tpl_no ); map<UnitValue, long>& r_sen_map = r_target.m_mapSenCnt; sen_pos = r_sen_map.find( r_micro[tpl_no * attr_num + m_nSenAttr] ); if (sen_pos == r_sen_map.end()) { r_sen_map.insert( pair<UnitValue, long>( r_micro[tpl_no * attr_num + m_nSenAttr], 1 ) ); } else { ++(*sen_pos).second; } } else { temp_lst_eqvcls.push_front( EqvClass() ); EqvClass& r_target = *temp_lst_eqvcls.begin(); r_target.m_lstTplId.push_back( tpl_no ); r_target.m_mapSenCnt.insert( pair<UnitValue, long>( r_micro[tpl_no * attr_num + m_nSenAttr], 1 ) ); r_target.m_vecNodePos = r_eqvcls.m_vecNodePos; if (node_begin == -1) { r_target.m_vecNodePos[cur_attr] = -1; } else { r_target.m_vecNodePos[cur_attr] = eqvcls_key.l; } map_eqvcls.insert( pair<UnitValue, list<EqvClass>::iterator>( eqvcls_key, temp_lst_eqvcls.begin() ) ); } } list<EqvClass>::iterator temp_cls_pos; for (temp_cls_pos = temp_lst_eqvcls.begin(); temp_cls_pos != temp_lst_eqvcls.end(); ++temp_cls_pos) { EqvClass& r_test = *temp_cls_pos; if (rVerifier.Verify( r_test ) == false) { gen_failed = true; break; } } if (gen_failed == true) { temp_lst_eqvcls.clear(); done_lst_eqvcls.clear(); break; } done_lst_eqvcls.splice( done_lst_eqvcls.end(), temp_lst_eqvcls ); } if (gen_failed == true) { ++failure_cnt; vec_attr_order[i].m_fWght = 0; } else { vec_attr_order[i].m_fWght /= 2; --m_vecGenLevel[cur_attr]; m_lstEqvClass.swap( done_lst_eqvcls ); temp_lst_eqvcls.clear(); done_lst_eqvcls.clear(); break; } } cout << "Current attribute: " << cur_attr << endl; } m_vecPtEqv.clear(); m_vecPtEqv.insert( m_vecPtEqv.begin(), m_pMicrodata->GetTupleNum(), NULL ); list<EqvClass>::iterator eqv_pos; list<long>::const_iterator tpl_pos; for (eqv_pos = m_lstEqvClass.begin(); eqv_pos != m_lstEqvClass.end(); ++eqv_pos) { EqvClass& r_eqv = *eqv_pos; for (tpl_pos = r_eqv.m_lstTplId.begin(); tpl_pos != r_eqv.m_lstTplId.end(); ++tpl_pos) { m_vecPtEqv[*tpl_pos] = &r_eqv; } } m_bGenExists = true; } catch( AnonyException e ) { m_lstEqvClass.clear(); m_vecGenLevel.clear(); m_vecPtEqv.clear(); cout << e.GetMsg() << endl; m_bGenExists = false; throw( e ); } return true; }