C++ 更新存储在地图中的矢量的矢量

C++ 更新存储在地图中的矢量的矢量,c++,stdvector,stdmap,C++,Stdvector,Stdmap,我在a.h中有以下设计: class A { virtual void updateCoefficients(std::string /*state*/, std::vector<std::vector<double>>& /*coefs*/, double /*reward*/) {} protected: std::map<std::string, std::v

我在
a.h
中有以下设计:

class A {
     virtual void updateCoefficients(std::string /*state*/, std::vector<std::vector<double>>& /*coefs*/,
                                    double /*reward*/) {}
protected: 
     std::map<std::string, std::vector<std::vector<double>>> heuristic;
}

class B : public A {
     virtual void updateCoefficients(std::string state, std::vector<std::vector<double>>& coefs,
                                   double reward) override;
}
当我从
类a
中定义的方法调用
更新效率时:

void A::foo(std::string state, std::string action, double const& reward) {
        if (heuristic.count(action)) {
            updateCoefficients(state, heuristic[action], reward);
        } else {
            std::vector<std::vector<double>> coefs(20, std::vector<double>(2, 0.0));
            heuristic[action] = coefs;
    } 
} 
void A::foo(std::string state,std::string action,双常量&奖励){
if(启发式计数(操作)){
更新效率(状态、启发式[行动]、奖励);
}否则{
std::vector coefs(20,std::vector(2,0.0));
启发式[行动]=系数;
} 
} 
问题在于映射图中的向量
coefs
没有被更新,但是每当调用
updateecofficients
时,所有向量都是0,就像它们初始化时一样。我做错了什么

其思想是将
类A
作为包含所有必须使用的方法和变量的基础,并在继承
类A
基础的子类中定义不同的
更新效率
方法

编辑

好的,整个代码都可以在这里找到。类
State
ActionState
THTS
中的信息是不相关的。我已经尝试了可能的修复方法,但map
启发式
始终充满零值。从其他类调用的方法是
离线启发式->学习(状态、操作、奖励)

编辑2


错误实际上在另一个地方,在代码逻辑中。使粘贴在上面的代码实际上是正确的。我会留下这个问题,以防有人遇到那样的问题。或者,如果这不是一件好事,请让我知道,这样我就删除了这个问题

启发式没有更新的原因是您需要调用一个::foo两次。第一次调用它将初始化为零,只有下一次调用将更新它们

因此,像这样更改代码将是一个解决方案:

void A::foo(std::string state, std::string action, double const& reward) {
      if (!heuristic.count(action)) {
            std::vector<std::vector<double>> coefs(20, std::vector<double>(2, 0.0));
            heuristic[action] = coefs;
      } 
      updateCoefficients(state, heuristic[action], reward);
} 

在我看来,您使用的是基类方法,将其设置为纯虚拟方法以避免错误<代码>虚拟无效更新效率(…)=0

按预期工作

// Example program
#include <iostream>
#include <string>
#include <vector>
#include <map>

class A {
     virtual void updateCoefficients(std::string /*state*/, std::vector<std::vector<double>>& /*coefs*/,
                                    double /*reward*/) = 0;
protected: 
     std::map<std::string, std::vector<std::vector<double>>> heuristic;
public:
     void foo(std::string state, std::string action, double const& reward);
     virtual ~A() {}

};

void A::foo(std::string state, std::string action, double const& reward) {
        if (heuristic.count(action)) {
            updateCoefficients(state, heuristic[action], reward);
            std::cout << "exist\n";
            std::cout << heuristic[action][0][0] << '\n';
            std::cout << heuristic[action][0][1] << '\n';
        } else {
            std::vector<std::vector<double>> coefs(20, std::vector<double>(2, 0.0));
            heuristic[action] = coefs;
            std::cout << "not exist\n";
    } 
} 

class B : public A {
     virtual void updateCoefficients(std::string state, std::vector<std::vector<double>>& coefs,
                                   double reward) override;
public:
    virtual ~B() {}
};

void B::updateCoefficients(std::string state, std::vector<std::vector<double>>& coefs,
                                   double reward) {
     for(unsigned i = 0; i < coefs.size(); i++) {
         coefs[i][0] += 1;
         coefs[i][1] += 2;
     }
}

int main()
{
  A* a = new B();
  a->foo("aaa", "bbb", 2.0);
  a->foo("aaa", "bbb", 2.0);
  a->foo("aaa", "bbb", 2.0);
  delete a;
  return 0;
}
//示例程序
#包括
#包括
#包括
#包括
甲级{
虚拟void更新效率(std::string/*state*/,std::vector&/*coefs*/,
双倍/*奖励*/)=0;
受保护的:
地图启发式;
公众:
void foo(标准::字符串状态、标准::字符串操作、双常量和奖励);
虚拟~A(){}
};
void A::foo(std::字符串状态、std::字符串操作、双常量和奖励){
if(启发式计数(操作)){
更新效率(状态、启发式[行动]、奖励);

std::难道你需要发布一个MCVE。我们不知道代码的其余部分发生了什么。@Đ或đeRelić我只能重复RichardHodges,你需要发布一个(、完整且可验证的示例)。整个代码都很难做到“最小”。使其成为MCVE是您的工作,而不是我们的工作。@Đ或đeRelić,不幸的是,我们没有在这里调试代码。如果您很难从命令行使用GDB,请尝试一些IDE(QTCreator是免费的,可以在没有QT的情况下工作,CLion也是不错的选择…).你应该给我们一些简单的例子,可以复制粘贴和运行没有任何变化-我们将提供帮助,最好是使用一些在线编译器,如IdeOne@Evgeniy我发现了错误,它在代码中的其他地方,与我附加的代码没有连接。调试帮助:)谢谢你的时间,很抱歉把这个问题变成了一个愚蠢的问题。是的,这是我犯的一个错误,但这不是<代码>启发式没有被更新的原因。感谢你指出我犯的另一个错误:)@Đ或đerrić,调试器将帮助你:)我已经更新了我的答案-在线尝试我真的讨厌使用GDB,所以我做了一个lot of"std::cout@ĐorđeRelić在这个答案中,启发式完全更新了。同样,更新代码工作得很好,foo就是问题所在。我尝试了你的修复,但没有起到任何作用。我更新了问题的标题和源文件的链接,以防我在一开始简化代码时出错。@ĐorđeRelić将调试打印添加到updateCoefficients方法,请确保调用必要的方法。检查所有使用refs的地方。我检查了。正如您在
GradualDesting::UpdateEfficients
中看到的,在多个位置上有
SystemUtils::log
,这是创建的日志:。旧值始终为0,即使它应该仅在第一次更新中。因此,逻辑是“旧值”值(0 | 1),最后一个值取决于向量中只有2个元素的向量中的索引。@Đ或đerrić
double offline heuristic::calculatesumofmulti钳(std::vector cocients,std::vector stateValues)
在这里使用ref&coef@Đ或đerrić在updateCoefficients方法中将日志添加到第二个for循环中
A* instance = new B();
instance->foo("aaa", "bbb", 123);
cout << "First call" << endl;

for (auto h : instance->heuristic)
    for (auto b : h.second)
        for (auto c : b)
            cout << c << " ";
cout << endl;               
instance->foo("aaa", "bbb", 123);
cout << "Second call" << endl;

for (auto h : instance->heuristic)
    for (auto b : h.second)
        for (auto c : b)
            cout << c << " ";
cout << endl;               
First call
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
Updated
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 
// Example program
#include <iostream>
#include <string>
#include <vector>
#include <map>

class A {
     virtual void updateCoefficients(std::string /*state*/, std::vector<std::vector<double>>& /*coefs*/,
                                    double /*reward*/) = 0;
protected: 
     std::map<std::string, std::vector<std::vector<double>>> heuristic;
public:
     void foo(std::string state, std::string action, double const& reward);
     virtual ~A() {}

};

void A::foo(std::string state, std::string action, double const& reward) {
        if (heuristic.count(action)) {
            updateCoefficients(state, heuristic[action], reward);
            std::cout << "exist\n";
            std::cout << heuristic[action][0][0] << '\n';
            std::cout << heuristic[action][0][1] << '\n';
        } else {
            std::vector<std::vector<double>> coefs(20, std::vector<double>(2, 0.0));
            heuristic[action] = coefs;
            std::cout << "not exist\n";
    } 
} 

class B : public A {
     virtual void updateCoefficients(std::string state, std::vector<std::vector<double>>& coefs,
                                   double reward) override;
public:
    virtual ~B() {}
};

void B::updateCoefficients(std::string state, std::vector<std::vector<double>>& coefs,
                                   double reward) {
     for(unsigned i = 0; i < coefs.size(); i++) {
         coefs[i][0] += 1;
         coefs[i][1] += 2;
     }
}

int main()
{
  A* a = new B();
  a->foo("aaa", "bbb", 2.0);
  a->foo("aaa", "bbb", 2.0);
  a->foo("aaa", "bbb", 2.0);
  delete a;
  return 0;
}