Skip to content

Instantly share code, notes, and snippets.

@brlauuu
Created March 1, 2017 09:41
Show Gist options
  • Save brlauuu/e9a80c65dd2ecff7925c546adaeb9888 to your computer and use it in GitHub Desktop.
Save brlauuu/e9a80c65dd2ecff7925c546adaeb9888 to your computer and use it in GitHub Desktop.
#ifndef OFFLINE_HEURISTIC_H
#define OFFLINE_HEURISTIC_H
#include <map>
#include <string>
#include <vector>
class THTS;
#include "states.h"
//class State;
//class ActionState;
/******************************************************************
Offline Heuristic
******************************************************************/
class OfflineHeuristic {
public:
virtual ~OfflineHeuristic() {}
static OfflineHeuristic* fromString(std::string& desc, THTS* thts);
// Evaluate state
virtual double evaluateState(State /*state*/, ActionState /*actionState*/) {
assert(false);
return 0.0;
}
// Set parameters from command line
virtual bool setValueFromString(std::string& /*param*/,
std::string& /*value*/) {
return false;
}
virtual void setHeuristicFileLocationToStore(std::string loc) {
heuristicFileLocationToStore = loc;
}
virtual std::string getHeuristicFileLocationToStore() {
return heuristicFileLocationToStore;
}
virtual void setHeuristicFileLocationToLoad(std::string loc) {
heuristicFileLocationToLoad = loc;
}
virtual std::string getHeuristicFileLocationToLoad() {
return heuristicFileLocationToLoad;
}
virtual std::string getName() {
return name;
}
virtual std::string getDetails() {
return "";
}
virtual void setLearnHeuristic(bool val) {
learnHeuristic = val;
}
virtual bool learningHeuristic () {
return learnHeuristic;
}
virtual void learn(State state, ActionState actionState,
double const& reward);
virtual void parseHeuristicFromFile(std::string inputFile);
virtual void parseCoefficientsFromString(std::string coefsString, std::vector<double>& coefs);
virtual void parseStateToValues(std::string stateString);
virtual void updateCoefficients(std::string /*state*/, std::vector<std::vector<double>>& /*coefs*/,
double /*reward*/) = 0;
virtual double calculateSumOfMultipliers(std::vector<std::vector<double>> coefficients, std::vector<double> stateValues);
// Print out evaluated states
virtual void print(std::ostream& out, std::string const& indent = "");
THTS* thts;
protected:
OfflineHeuristic(THTS* _thts)
: thts(_thts),
learnHeuristic(false) {}
bool learnHeuristic;
std::string heuristicFileLocationToStore;
std::string heuristicFileLocationToLoad;
std::string name;
std::map<std::string, std::vector<std::vector<double>>> heuristic;
std::map<std::string, std::vector<double>> stateValues;
};
class GradualDescent : public OfflineHeuristic {
public:
enum DescentType {
ALL_AS_EQUAL, // Coefficients that are larger, get smaller step move -> conclusion that all weights are equally important
DEMOCRACY, // All coefficients have equal influence on the calculated reward (by getting equal share of step fixing)
BIAS // Weights that participate more in the final calculated reward are 'moved' faster to the goal reward
};
GradualDescent(THTS* _thts)
: OfflineHeuristic(_thts) {
name = "GradualDescent";
}
virtual ~GradualDescent() {}
// Set parameters from command line which are the option on if the heuristic is used or learned
virtual bool setValueFromString(std::string& param, std::string& value) override;
virtual void updateCoefficients(std::string state, std::vector<std::vector<double>>& coefs,
double reward) override;
virtual std::string getDetails() override {
return getDescentType();
}
// Evaluate state
// TODO
// virtual double evaluateState(State state, ActionState actionState) override;
void setStep(double _step);
double getStep() {
return step;
}
void setType(GradualDescent::DescentType _type) {
type = _type;
}
std::string getDescentType();
private:
double step;
GradualDescent::DescentType type;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment