Skip to content

Commit

Permalink
Merge pull request #898 from lixun910/master
Browse files Browse the repository at this point in the history
1.10.0.2
  • Loading branch information
lixun910 committed Jun 6, 2017
2 parents 61d45de + aff6839 commit 1598253
Show file tree
Hide file tree
Showing 21 changed files with 242 additions and 108 deletions.
104 changes: 100 additions & 4 deletions Algorithms/maxp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@
#include "cluster.h"
#include "maxp.h"



using namespace std;

Maxp::Maxp(const GalElement* _w, const vector<vector<double> >& _z, int floor, vector<int> floor_variable, int initial, vector<int> seed)
Maxp::Maxp(const GalElement* _w, const vector<vector<double> >& _z, int floor, vector<vector<int> > floor_variable, int initial, vector<int> seed)
: w(_w), z(_z), LARGE(1000000), MAX_ATTEMPTS(100)
{
num_obs = floor_variable.size();
Expand All @@ -54,9 +52,41 @@ Maxp::Maxp(const GalElement* _w, const vector<vector<double> >& _z, int floor,
feasible = false;
else {
feasible = true;
double best_val = objective_function();
// deep copy
vector<vector<int> > current_regions = regions;
map<int, int> current_area2region = area2region;
vector<double> initial_wss;
int attemps = 0;

for (int i=0; i<initial; i++) {
init_solution();
if (p > 0) {
double val = objective_function();
initial_wss.push_back(val);

wxString str;
str << "initial solution";
str << i;
str << val;
str << best_val;
LOG_MSG(str.ToStdString());

if (val < best_val) {
current_regions = regions;
current_area2region = area2region;
best_val = val;
}
attemps += 1;
}
}

regions = current_regions;
p = regions.size();
area2region = current_area2region;

swap();
}

}

Maxp::~Maxp()
Expand Down Expand Up @@ -243,3 +273,69 @@ void Maxp::init_solution()

}
}

void Maxp::swap()
{
bool swapping = true;
int swap_iteration = 0;
int total_moves = 0;
int k = regions.size();

vector<int>::iterator iter;
vector<int> changed_regions(k, 1);
while (swapping) {
int moves_made = 0;
vector<int> regionIds;
for (int r=0; r<k; r++) {
if (changed_regions[r] >0) {
regionIds.push_back(r);
}
}
random_shuffle(regionIds.begin(), regionIds.end());
for (int r=0; r<k; r++) changed_regions[r] = 0;
swap_iteration += 1;
for (int i=0; i<regionIds.size(); i++) {
int seed = regionIds[i];
bool local_swapping = true;
int local_attempts = 0;
while (local_swapping) {
int local_moves = 0;
// get neighbors
vector<int> neighbors;
vector<int>& members = regions[seed];
for (int j=0; j<members.size(); j++) {
int member = members[j];
for (int k=0; k<w[member].Size(); k++) {
int candidate = w[member][k];
iter = find(members.begin(), members.end(), candidate);
if (iter != members.end()) continue;// not in members
iter = find(neighbors.begin(), neighbors.end(), candidate);
if (iter != neighbors.end()) continue; // not in neighbors
neighbors.push_back(candidate);
}
}
for (int j=0; j<neighbors.size(); j++) {
int nbr = neighbors[j];
vector<int> block = regions[ area2region[ nbr ] ]; // deep copy
//if (check_contiguity(block, neighbor)) {
// block.erase(neighbor);
//}
}
}
}
}
}

bool Maxp::is_component(const GalElement *w, const vector<int> &ids)
{
int components = 0;
map<int, int> masks;
for (int i=0; i<ids.size(); i++) masks[ids[i]] = 0;

vector<int> q;
for (int i=0; i<ids.size(); i++)
{
int node = ids[i];
}
return false;
}
22 changes: 21 additions & 1 deletion Algorithms/maxp.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@

using namespace std;

class qvector
{
public:
qvector();
~qvector();

bool find(int val);
void remove(int val);
void add(int val);

protected:
vector<int> vdata;
map<int, int> mdict;
vector<int>::iterator v_iter;
map<int, int>::iterator m_iter;
};

/*! A Max-p class */

class Maxp
Expand All @@ -44,7 +61,7 @@ class Maxp
\param initial int number of initial solutions to generate
\param seed list ids of observations to form initial seeds. If len(ids) is less than the number of observations, the complementary ids are added to the end of seeds. Thus the specified seeds get priority in the solution
*/
Maxp(const GalElement* w, const vector<vector<double> >& z, int floor, vector<int> floor_variable, int initial, vector<int> seed);
Maxp(const GalElement* w, const vector<vector<double> >& z, int floor, vector<vector<int> > floor_variable, int initial, vector<int> seed);


//! A Deconstructor
Expand Down Expand Up @@ -152,6 +169,9 @@ class Maxp

double objective_function();

void swap();

bool is_component(const GalElement* w, const vector<int>& ids);
};

#endif
25 changes: 16 additions & 9 deletions DataViewer/DataSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,7 @@ IDataSource* IDataSource::CreateDataSource(wxString data_type_name,
IDataSource* IDataSource::CreateDataSource(wxString ds_json)
{
// '{"ds_type":"ds_shapefile", "ds_path": "/test.shp", "db_name":"test"..}'
std::string ds_json_str(ds_json.mb_str());

std::string ds_json_str(GET_ENCODED_FILENAME(ds_json));
json_spirit::Value v;

try {
Expand Down Expand Up @@ -369,15 +368,23 @@ void FileDataSource::ReadPtree(const ptree& pt,

wxString FileDataSource::GetJsonStr()
{
std::string ds_type_str(GetDataTypeNameByGdaDSType(ds_type).mb_str());
std::string ds_path_str(GET_ENCODED_FILENAME(file_repository_path));
//std::string ds_type_str(GetDataTypeNameByGdaDSType(ds_type).mb_str());
//std::string ds_path_str(GET_ENCODED_FILENAME(file_repository_path)); // utf-8 coded

json_spirit::Object ret_obj;
ret_obj.push_back(json_spirit::Pair("ds_type", ds_type_str));
ret_obj.push_back(json_spirit::Pair("ds_path", ds_path_str));
//json_spirit::Object ret_obj;
//ret_obj.push_back(json_spirit::Pair("ds_type", ds_type_str));
//ret_obj.push_back(json_spirit::Pair("ds_path", ds_path_str));

std::string json_str = json_spirit::write(ret_obj);
return wxString(json_str);
//std::string json_str1 = json_spirit::write(ret_obj);

wxString json_tmp = "{\"ds_type\":\"%s\", \"ds_path\":\"%s\"}";
wxString json_path = file_repository_path;
json_path.Replace("\\", "\\\\");

//wxString json_str = wxString::Format(json_tmp, GetDataTypeNameByGdaDSType(ds_type), json_path);
wxString json_str = "{\"ds_type\":\""+GetDataTypeNameByGdaDSType(ds_type)+"\", \"ds_path\":\""+json_path+"\"}";

return json_str;
}

void FileDataSource::WritePtree(ptree& pt,
Expand Down
2 changes: 1 addition & 1 deletion DialogTools/AutoUpdateDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ size_t write_to_file(void *ptr, size_t size, size_t nmemb, void* userdata)

string ReadUrlContent(const char* url)
{

wxLogMessage("AutoUpdate::ReadUrlContent()");
string response;

CURL* curl = curl_easy_init();
Expand Down
21 changes: 15 additions & 6 deletions DialogTools/ConnectDatasourceDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void RecentDatasource::Init(wxString json_str_)
}
else if (i->name_ == "ds_thumb") {
val = i->value_;
ds_thumb = val.get_str();
ds_thumb = wxString(val.get_str());
}
}
ds_names.push_back(ds_name);
Expand All @@ -173,7 +173,8 @@ void RecentDatasource::Save()
json_spirit::Object ds_obj;
std::string ds_name( GET_ENCODED_FILENAME(ds_names[i]));
std::string layer_name( GET_ENCODED_FILENAME(ds_layernames[i]));
std::string ds_conf( ds_confs[i].mb_str() );
//std::string ds_conf( ds_confs[i].mb_str() );
std::string ds_conf( GET_ENCODED_FILENAME(ds_confs[i]) );
std::string ds_thumb( GET_ENCODED_FILENAME(ds_thumbnails[i]) );
ds_obj.push_back( json_spirit::Pair("ds_name", ds_name) );
ds_obj.push_back( json_spirit::Pair("layer_name", layer_name) );
Expand Down Expand Up @@ -344,7 +345,7 @@ IDataSource* RecentDatasource::GetDatasource(wxString ds_name)
if (ds_names[i] == ds_name) {
wxString ds_conf = ds_confs[i];
try {
return IDataSource::CreateDataSource(ds_conf);
return IDataSource::CreateDataSource(ds_confs[i]);
} catch(GdaException& ex){
return NULL;
}
Expand Down Expand Up @@ -454,7 +455,13 @@ void ConnectDatasourceDlg::AddRecentItem(wxBoxSizer* sizer, wxScrolledWindow* sc
}
file_path_str = GenUtils::GetSamplesDir() + ds_thumb;

wxImage img(file_path_str);
wxImage img;
if (!wxFileExists(file_path_str)) {
ds_thumb = "no_map.png";
file_path_str = GenUtils::GetSamplesDir() + ds_thumb;

}
img.LoadFile(file_path_str);
if (!img.IsOk()) {
ds_thumb = "no_map.png";
file_path_str = GenUtils::GetSamplesDir() + ds_thumb;
Expand Down Expand Up @@ -1108,9 +1115,11 @@ void ConnectDatasourceDlg::OnSample(wxCommandEvent& event)
if (ds_name == "samples.sqlite") {
ds_name = GenUtils::GetSamplesDir() + ds_name;
ds_name.Replace("\\", "\\\\");
ds_json = wxString::Format("{\"ds_type\":\"SQLite\", \"ds_path\": \"%s\"}", ds_name);
//ds_json = wxString::Format("{\"ds_type\":\"SQLite\", \"ds_path\": \"%s\"}", ds_name);
ds_json = "{\"ds_type\":\"SQLite\", \"ds_path\": \""+ds_name+"\"}";
} else {
ds_json = wxString::Format("{\"ds_type\":\"GeoJSON\", \"ds_path\": \"%s\"}", ds_name);
//ds_json = wxString::Format("{\"ds_type\":\"GeoJSON\", \"ds_path\": \"%s\"}", ds_name);
ds_json = "{\"ds_type\":\"GeoJSON\", \"ds_path\": \""+ds_name+"\"}";
}

IDataSource* ds = IDataSource::CreateDataSource(ds_json);
Expand Down
19 changes: 17 additions & 2 deletions DialogTools/CreateGridDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,20 @@ void CreateGridDlg::OnCReferencefile2Click( wxCommandEvent& event )
wxLogMessage("In CreateGridDlg::OnCReferencefile2Click()");

try{
wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
while (node) {
wxWindow* win = node->GetData();
if (ConnectDatasourceDlg* w = dynamic_cast<ConnectDatasourceDlg*>(win)) {
if (w->GetType() == 0) {
w->Show(true);
w->Maximize(false);
w->Raise();
return;
}
}
node = node->GetNext();
}

ConnectDatasourceDlg dlg(this);
if (dlg.ShowModal() != wxID_OK)
return;
Expand Down Expand Up @@ -374,11 +388,12 @@ void CreateGridDlg::CreateGrid()

grids.push_back(new GdaPolygon(n_pts,pts));

delete pts;
delete[] pts;
}
}

ExportDataDlg dlg(this, grids, Shapefile::POLYGON);
ExportDataDlg dlg(NULL, grids, Shapefile::POLYGON);
dlg.Raise();
dlg.ShowModal();

m_nCount = nMaxCount;
Expand Down
2 changes: 1 addition & 1 deletion DialogTools/DatasourceDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ void DatasourceDlg::PromptDSLayers(IDataSource* datasource)
throw GdaException(msg.mb_str());
}

vector<string> table_names = OGRDataAdapter::GetInstance().GetLayerNames(ds_name.ToStdString(), ds_type);
vector<wxString> table_names = OGRDataAdapter::GetInstance().GetLayerNames(ds_name, ds_type);

int n_tables = table_names.size();

Expand Down
2 changes: 1 addition & 1 deletion DialogTools/ExportDataDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ ExportDataDlg::CreateOGRLayer(wxString& ds_name,
OGRDataAdapter& ogr_adapter = OGRDataAdapter::GetInstance();
OGRLayerProxy* new_layer =
ogr_adapter.ExportDataSource(ds_format.ToStdString(),
ds_name.ToStdString(),
ds_name,
layer_name.ToStdString(),
geom_type,
ogr_geometries,
Expand Down
Loading

0 comments on commit 1598253

Please sign in to comment.