Comments (2)
I'm planning to add an example file for this soon. In the meantime, would the test file for the LP approach help you with your problem?
You can find it here:
https://github.com/Svalorzen/AI-Toolbox/blob/master/test/Factored/LinearProgrammingTests.cpp
from ai-toolbox.
Ok, I got it running. It seems to be working fine.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <array>
#include <cmath>
#include <chrono>
#include <thread>
#include <algorithm>
#include <AIToolbox/MDP/Algorithms/ValueIteration.hpp>
#include <AIToolbox/MDP/Policies/Policy.hpp>
#include <AIToolbox/MDP/IO.hpp>
#include <AIToolbox/MDP/SparseModel.hpp>
#include <AIToolbox/Factored/MDP/Environments/SysAdmin.hpp>
#include <AIToolbox/Utils/Core.hpp>
#include <AIToolbox/Factored/MDP/Algorithms/LinearProgramming.hpp>
#include <AIToolbox/Factored/MDP/Policies/QGreedyPolicy.hpp>
constexpr unsigned agents = 4;
constexpr double pFailBase = 0.1;
constexpr double pFailBonus = 0.2;
constexpr double pDeadBase = 0.3;
constexpr double pDeadBonus = 0.4;
constexpr double pLoad = 0.4;
constexpr double pDoneG = 0.4;
constexpr double pDoneF = 0.3;
int main() {
AIToolbox::Factored::MDP::CooperativeModel model =
AIToolbox::Factored::MDP::makeSysAdminUniRing(
agents,
pFailBase,
pFailBonus,
pDeadBase,
pDeadBonus,
pLoad,
pDoneG,
pDoneF);
// Create and setup the bases to use for the ValueFunction.
auto vf = AIToolbox::Factored::MDP::ValueFunction();
for (size_t s = 0; s < model.getS().size(); s += 2) {
for (size_t i = 0; i < 9; ++i) {
vf.values.bases.emplace_back(AIToolbox::Factored::BasisFunction{{s, s+1}, AIToolbox::Vector(9)});
vf.values.bases.back().values.setZero();
vf.values.bases.back().values[i] = 1.0;
}
}
AIToolbox::Factored::MDP::LinearProgramming solver;
AIToolbox::Factored::MDP::QFunction q;
std::tie(vf.weights, q) = solver(model, vf.values);
AIToolbox::Factored::MDP::QGreedyPolicy policy(
model.getS(),
model.getA(),
q);
// Start state is where all agents are Good
AIToolbox::Factored::State s(model.getS().size());
std::fill(std::begin(s), std::end(s), AIToolbox::Factored::MDP::SysAdminEnums::MachineStatus::Good);
AIToolbox::Factored::Action a(model.getA().size());
std::fill(std::begin(a), std::end(a), 0);
AIToolbox::Factored::State s1;
double r, totalReward = 0.0;
size_t t = 100;
while (true) {
// Print it!
std::cout << AIToolbox::Factored::MDP::printSysAdminRing(s) << std::endl;
// We give a time limit
if (t == 0) break;
// We sample an action for this state according to the optimal policy
a = policy.sampleAction(s);
// And we use the model to simulate what is going to happen next (in
// case of a "real world" scenario where the library is used this step
// would not exist as the world would automatically step to the next
// state. Here we simulate.
std::tie(s1, r) = model.sampleSR(s, a);
// Add into the total reward (we don't use this here, it's just as an
// example)
totalReward += r;
std::cout << "STEP: " << t << ", IMMEDIATE REWARD: " << r << ", TOTAL REWARD: " << totalReward << std::endl;
// Update the current state with the new one.
s = s1;
--t;
// Sleep 1 second so the user can see what is happening.
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return 0;
}
from ai-toolbox.
Related Issues (20)
- Python module problem HOT 26
- Using the Toolbox to solve the Tag problem HOT 4
- Save POMDP policy HOT 6
- Sparse Matrix's on POMDP model HOT 22
- Improve serialization for MDP and POMDP Sparse Models HOT 1
- Problem with make HOT 5
- Make cannot find .hpp file in Boost HOT 2
- Errors when compiling tutorials HOT 5
- Can't find Lpsolve when run cmake HOT 9
- Using AI-Toolbox with OMNeT++ HOT 2
- Make issue HOT 9
- Not able to find "compare" file HOT 3
- Trouble installing and running AIToolbox HOT 9
- about LP_Solve HOT 13
- error C3779: 'AIToolbox::IndexMapIterator<IdsIterator,Container>::operator *': a function that returns 'auto' cannot be used before it is defined HOT 11
- Better Project Setup Tutorial HOT 6
- C++: error: expected โ)โ before โelementsNโ HOT 10
- Problems when building the library HOT 4
- Sparse Matrix: Space Time Complexity while assigning and accessing an element. HOT 1
- Can't build because of lpslove HOT 16
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ai-toolbox.