summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGard Spreemann <gard.spreemann@epfl.ch>2018-05-14 17:25:40 +0200
committerGard Spreemann <gard.spreemann@epfl.ch>2018-05-14 17:25:40 +0200
commit0c6aafa07857042e92857fec6969d0caf89e187e (patch)
tree412ee4ec07f6e8d6567ac13851ff89ba9fc3e68e
parenta5454ea066a0b148c9f423fc8862e26aae79eefe (diff)
Support for finitization.gspr/finitize
-rw-r--r--geom_matching/wasserstein/CMakeLists.txt3
-rw-r--r--geom_matching/wasserstein/example/wasserstein_dist_finitize_dipha.cpp138
-rw-r--r--geom_matching/wasserstein/include/diagram_reader.h20
3 files changed, 161 insertions, 0 deletions
diff --git a/geom_matching/wasserstein/CMakeLists.txt b/geom_matching/wasserstein/CMakeLists.txt
index b59378c..fe5fd73 100644
--- a/geom_matching/wasserstein/CMakeLists.txt
+++ b/geom_matching/wasserstein/CMakeLists.txt
@@ -52,6 +52,9 @@ target_link_libraries(wasserstein_dist PUBLIC ${libraries})
add_executable(wasserstein_dist_dipha ${CMAKE_CURRENT_SOURCE_DIR}/example/wasserstein_dist_dipha.cpp ${WS_HEADERS})
target_link_libraries(wasserstein_dist_dipha PUBLIC ${libraries})
+add_executable(wasserstein_dist_finitize_dipha ${CMAKE_CURRENT_SOURCE_DIR}/example/wasserstein_dist_finitize_dipha.cpp ${WS_HEADERS})
+target_link_libraries(wasserstein_dist_finitize_dipha PUBLIC ${libraries})
+
# pure geometric version, arbitrary dimension
add_executable(wasserstein_dist_point_cloud ${CMAKE_CURRENT_SOURCE_DIR}/example/wasserstein_dist_point_cloud.cpp ${WS_HEADERS})
target_link_libraries(wasserstein_dist_point_cloud PUBLIC ${libraries})
diff --git a/geom_matching/wasserstein/example/wasserstein_dist_finitize_dipha.cpp b/geom_matching/wasserstein/example/wasserstein_dist_finitize_dipha.cpp
new file mode 100644
index 0000000..5269a36
--- /dev/null
+++ b/geom_matching/wasserstein/example/wasserstein_dist_finitize_dipha.cpp
@@ -0,0 +1,138 @@
+/*
+
+Copyright (c) 2015, M. Kerber, D. Morozov, A. Nigmetov
+Copyright (c) 2018, G. Spreemann
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
+You are under no obligation whatsoever to provide any bug fixes, patches, or
+upgrades to the features, functionality or performance of the source code
+(Enhancements) to anyone; however, if you choose to make your Enhancements
+available either publicly, or directly to copyright holder,
+without imposing a separate written license agreement for such Enhancements,
+then you hereby grant the following license: a non-exclusive, royalty-free
+perpetual license to install, use, modify, prepare derivative works, incorporate
+into other computer software, distribute, and sublicense such enhancements or
+derivative works thereof, in binary and source code form.
+
+ */
+
+#include <iostream>
+#include <locale>
+#include <iomanip>
+#include <vector>
+
+//#define LOG_AUCTION
+
+//#include "auction_runner_fr.h"
+//#include "auction_runner_fr.hpp"
+
+#include "wasserstein.h"
+
+// any container of pairs of doubles can be used,
+// we use vector in this example.
+
+int main(int argc, char* argv[])
+{
+ using PairVector = std::vector<std::pair<double, double>>;
+ PairVector diagramA, diagramB;
+
+ hera::AuctionParams<double> params;
+
+ if (argc < 4 ) {
+ std::cerr << "Usage: " << argv[0] << " file1 file2 ph_dim finitization [wasserstein_degree] [relative_error] [internal norm] [initial epsilon] [epsilon_factor] [max_bids_per_round] [gamma_threshold][log_filename_prefix]. By default power is 1.0, relative error is 0.01, internal norm is l_infinity, initall epsilon is chosen automatically, epsilon factor is 5.0, Jacobi variant is used (max bids per round is maximal), gamma_threshold = 0.0." << std::endl;
+ return 1;
+ }
+
+ unsigned int dim = std::stoul(argv[3]);
+
+ if (!hera::read_diagram_dipha<double, PairVector>(argv[1], dim, diagramA)) {
+ std::exit(1);
+ }
+
+ if (!hera::read_diagram_dipha<double, PairVector>(argv[2], dim, diagramB)) {
+ std::exit(1);
+ }
+
+ double finitization = std::atof(argv[4]);
+
+ hera::finitize(finitization, diagramA);
+ hera::finitize(finitization, diagramB);
+
+ params.wasserstein_power = (6 <= argc) ? atof(argv[5]) : 1.0;
+ if (params.wasserstein_power < 1.0) {
+ std::cerr << "The fourth argument (wasserstein_degree) was \"" << argv[5] << "\", must be a number >= 1.0. Cannot proceed. " << std::endl;
+ std::exit(1);
+ }
+
+ if (params.wasserstein_power == 1.0) {
+ hera::remove_duplicates<double>(diagramA, diagramB);
+ }
+
+ //default relative error: 1%
+ params.delta = (7 <= argc) ? atof(argv[6]) : 0.01;
+ if ( params.delta <= 0.0) {
+ std::cerr << "The 5th argument (relative error) was \"" << argv[6] << "\", must be a number > 0.0. Cannot proceed. " << std::endl;
+ std::exit(1);
+ }
+
+ // default for internal metric is l_infinity
+ params.internal_p = ( 8 <= argc ) ? atof(argv[7]) : hera::get_infinity<double>();
+ if (std::isinf(params.internal_p)) {
+ params.internal_p = hera::get_infinity<double>();
+ }
+
+
+ if (not hera::is_p_valid_norm<double>(params.internal_p)) {
+ std::cerr << "The 7th argument (internal norm) was \"" << argv[7] << "\", must be a number >= 1.0 or inf. Cannot proceed. " << std::endl;
+ std::exit(1);
+ }
+
+ // if you want to specify initial value for epsilon and the factor
+ // for epsilon-scaling
+ params.initial_epsilon= ( 9 <= argc ) ? atof(argv[8]) : 0.0 ;
+
+ if (params.initial_epsilon < 0.0) {
+ std::cerr << "The 8th argument (initial epsilon) was \"" << argv[8] << "\", must be a non-negative number. Cannot proceed." << std::endl;
+ std::exit(1);
+ }
+
+ params.epsilon_common_ratio = ( 10 <= argc ) ? atof(argv[9]) : 0.0 ;
+ if (params.epsilon_common_ratio <= 1.0 and params.epsilon_common_ratio != 0.0) {
+ std::cerr << "The 9th argument (epsilon factor) was \"" << argv[9] << "\", must be a number greater than 1. Cannot proceed." << std::endl;
+ std::exit(1);
+ }
+
+
+ params.max_bids_per_round = ( 11 <= argc ) ? atoi(argv[10]) : 0;
+ if (params.max_bids_per_round == 0)
+ params.max_bids_per_round = std::numeric_limits<size_t>::max();
+
+
+ params.gamma_threshold = (12 <= argc) ? atof(argv[11]) : 0.0;
+
+ std::string log_filename_prefix = ( 13 <= argc ) ? argv[12] : "";
+
+ params.max_num_phases = 800;
+
+#ifdef LOG_AUCTION
+ spdlog::set_level(spdlog::level::info);
+#endif
+
+ double res = hera::wasserstein_dist(diagramA, diagramB, params, log_filename_prefix);
+
+ std::cout << std::setprecision(15) << res << std::endl;
+
+ return 0;
+
+}
diff --git a/geom_matching/wasserstein/include/diagram_reader.h b/geom_matching/wasserstein/include/diagram_reader.h
index 84bf49c..55228b4 100644
--- a/geom_matching/wasserstein/include/diagram_reader.h
+++ b/geom_matching/wasserstein/include/diagram_reader.h
@@ -324,6 +324,26 @@ void remove_duplicates(ContType& dgm_A, ContType& dgm_B)
}
}
+template<class RealType = double>
+int finitize(RealType finitization, std::vector<std::pair<RealType, RealType> >& diagram)
+{
+ if (finitization == std::numeric_limits<double>::infinity())
+ return 0;
+
+ int altered = 0;
+
+ for (auto it = diagram.begin(); it != diagram.end(); ++it)
+ {
+ if (it->second > finitization)
+ {
+ it->second = finitization;
+ ++altered;
+ }
+ }
+
+ return altered;
+}
+
#ifdef WASSERSTEIN_PURE_GEOM