Anisotropic
Anisotropic

Reputation: 645

Data Structure for storing a tuple key: list of parameters relationship

In c++

I'm running an optimization algorithm to find a set of parameters for a given combination of temperature and humidity and I want a structure for efficient look-up and iteration time for further computation.

Example: I calculate all 5-parameters for a device across the range of Temperature[0k-300k] x Humidity[x-xxx]. I want to store these in memory and when I call a function at a given temperature and humidity, it should be able to quickly load the relevant 5 parameters using the tuple as a key.

I'm thinking of a multimap such as std::multimap<double, std::vector<double>>, but I'm iffy about the baggage that comes with vector.

Edit:

Ok an example will be like this (for one variable):

for x in range(Temperature){
      parameterList[x] = (deviceClass.geneticAlgo(loss = 1, x));

deviceClass.setParameters(parameterList);

Then for that class I want:

double later_calc(temperature,humidity, x...){
       return deviceSimulation(parameterList[(temperature, humidity, x...)]);
}

I want the later_calc function to quickly access the structure in the class accessed by the input of temperature, humidity, etc.

Upvotes: 1

Views: 206

Answers (1)

davidhigh
davidhigh

Reputation: 15498

It seems to me you want to use a std::map with either a std::tuple as key or a custom class which holds the parameters, and a mapped type which stores the experiment result (here it is double, but you can also easily use a custom Result class):

struct ParameterType
{
    int temperature;
    int humidity;
    int x;

    bool operator<(ParameterType const& rhs) const
    {
        return std::tie(temperature, humidity, x) < std::tie(rhs.temperature, rhs.humidity, rhs.x);
    }
};

using ResultType = double;

int main()
{
     std::map<ParameterType, ResultType> params;

     params[{263, 10, 1}] = 1.0;

     std::cout<< params[{263, 10, 1}] <<std::endl; 
}

DEMO

You should use a multimap if you want to repeat experiments, i.e. if you have multiple results for the same parameter set. (But you could also use a map with a std::vector<ResultType>).

Note that I've used integer values as a key, whereas in practice one is tempted to use floating point numbers. Those, however, might lead to problems in key comparison due to rounding errors. So better fix the decimal digits and decode real inputs as 263.53 as 26353.

Upvotes: 1

Related Questions