boost::shared_ptr space_from_json()

in binding-cpp/gym_binding.cpp [51:90]


boost::shared_ptr<Space> space_from_json(const Json::Value& j)
{
	boost::shared_ptr<Space> r(new Space);
	Json::Value v = j["info"];
	std::string type = require(v, "name");
	if (type=="Discrete") {
		r->type = Space::DISCRETE;
		r->discreet_n = v["n"].asInt(); // will throw runtime_error if cannot be converted to int

	} else if (type=="Box") {
		r->type = Space::BOX;
		Json::Value shape = v["shape"];
		Json::Value low   = v["low"];
		Json::Value high  = v["high"];
		if (!shape.isArray() || !low.isArray() || !high.isArray())
			throw std::runtime_error("cannot parse box space (1)");
		int l1 = low.size();
		int l2 = high.size();
		int ls = shape.size();
		int sz = 1;
		for (int s=0; s<ls; ++s) {
			int e = shape[s].asInt();
			r->box_shape.push_back(e);
			sz *= e;
		}
		if (sz != l1 || l1 != l2)
			throw std::runtime_error("cannot parse box space (2)");
		r->box_low.resize(sz);
		r->box_high.resize(sz);
		for (int i=0; i<sz; ++i) {
			r->box_low[i]  = low[i].asFloat();
			r->box_high[i] = high[i].asFloat();
		}

	} else {
		throw std::runtime_error("unknown space type '" + type + "'");
	}

	return r;
}