Code Monkey home page Code Monkey logo

Comments (2)

Svalorzen avatar Svalorzen commented on July 22, 2024

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.

njunhao avatar njunhao commented on July 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.