DiSMEC++
null.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021, Aalto University, developed by Erik Schultheis
2 // All rights reserved.
3 //
4 // SPDX-License-Identifier: MIT
5 
6 #include "null.h"
7 #include "stats/collection.h"
8 
9 using namespace dismec::solvers;
10 
11 
12 namespace {
13  /*
14  stats::stat_id_t STAT_OBJECTIVE_VALUE{1};
15  stats::stat_id_t STAT_GRADIENT_NORM{2};
16  stats::stat_id_t STAT_GRADIENT{3};
17  stats::stat_id_t STAT_WEIGHT_VECTOR{5};
18  stats::stat_id_t STAT_ITER_TIME{8};
19  stats::stat_id_t STAT_PROGRESS{11};
20 */
22 }
23 
24 
25 
27 {
29  if(!m_CalcLoss) {
30  return {MinimizerStatus::SUCCESS, 0, 0, 0, 0, 0};
31  }
32 
33  m_Weights = init;
34  m_Gradient.resize(init.size());
35 
36  real_t f = objective.value(m_Weights);
37  objective.gradient(m_Weights, m_Gradient);
38  real_t gnorm = m_Gradient.norm();
39 
40  // OK, there is something wrong already!
41  if(!std::isfinite(f) || !std::isfinite(gnorm)) {
42  spdlog::error("Invalid optimization: initial value: {}, gradient norm: {}", f, gnorm);
43  return {MinimizerStatus::FAILED, 0, f, gnorm, f, gnorm};
44  }
45 
46  return {MinimizerStatus::SUCCESS, 0, f, gnorm, f, gnorm};
47 
48 }
49 
50 NullOptimizer::NullOptimizer(bool calc) : m_Weights(DenseRealVector(1)), m_CalcLoss(calc) {
51  declare_tag(TAG_ITERATION, "iteration");
52 }
Class that models an optimization objective.
Definition: objective.h:41
An integer-like type that represents categorical values.
Definition: opaque_int.h:24
MinimizationResult run(objective::Objective &objective, Eigen::Ref< DenseRealVector > init) override
Definition: null.cpp:26
NullOptimizer(bool calc)
Constructor, specifies whether to run any calculations at all.
Definition: null.cpp:50
DenseRealVector m_Gradient
Definition: null.h:32
void declare_tag(tag_id_t index, std::string name)
Declares a new tag. This function just forwards all its arguments to the internal StatisticsCollectio...
Definition: tracked.cpp:24
void set_tag(tag_id_t tag, long value)
Set value of tag. This function just forwards all its arguments to the internal StatisticsCollection.
Definition: tracked.h:116
constexpr const dismec::stats::tag_id_t TAG_ITERATION
Definition: null.cpp:21
@ FAILED
Some internal operation failed.
@ SUCCESS
The returned result is a minimum according to the stopping criterion of the algorithm.
types::DenseVector< real_t > DenseRealVector
Any dense, real values vector.
Definition: matrix_types.h:40
float real_t
The default type for floating point values.
Definition: config.h:17