<commit_before>package be.iminds.iot.dianne.rl.agent.strategy;

import java.util.Map;

import be.iminds.iot.dianne.api.nn.NeuralNetwork;
import be.iminds.iot.dianne.api.rl.agent.ActionStrategy;
import be.iminds.iot.dianne.api.rl.agent.AgentProgress;
import be.iminds.iot.dianne.api.rl.environment.Environment;
import be.iminds.iot.dianne.tensor.Tensor;
import be.iminds.iot.dianne.tensor.TensorOps;

public class GaussianSamplingActionStrategy implements ActionStrategy {

	private NeuralNetwork policy;
	
	private int actionDims;
	private Tensor action;
	
	@Override
	public void setup(Map<String, String> config, Environment env, NeuralNetwork... nns) throws Exception {
		this.policy = nns[0];
		this.actionDims = env.actionDims()[0];
		this.action = new Tensor(this.actionDims);
	}

	@Override
	public AgentProgress processIteration(long i, Tensor state) throws Exception {
		Tensor actionParams = policy.forward(state);
		
		Tensor means = actionParams.narrow(0, 0, actionDims);
		Tensor stdevs = actionParams.narrow(0, actionDims, actionDims);
		
		action.randn();
		
		TensorOps.cmul(action, action, stdevs);
		TensorOps.add(action, action, means);
		
		return new AgentProgress(i, action);
	}

}<commit_msg>Added capping to gaussian sampling strategy<commit_after>package be.iminds.iot.dianne.rl.agent.strategy;

import java.util.Map;

import be.iminds.iot.dianne.api.nn.NeuralNetwork;
import be.iminds.iot.dianne.api.rl.agent.ActionStrategy;
import be.iminds.iot.dianne.api.rl.agent.AgentProgress;
import be.iminds.iot.dianne.api.rl.environment.Environment;
import be.iminds.iot.dianne.nn.util.DianneConfigHandler;
import be.iminds.iot.dianne.rl.agent.strategy.config.GaussianNoiseConfig;
import be.iminds.iot.dianne.tensor.Tensor;
import be.iminds.iot.dianne.tensor.TensorOps;

public class GaussianSamplingActionStrategy implements ActionStrategy {

	private NeuralNetwork policy;
	
	private GaussianNoiseConfig config;
	
	private int actionDims;
	private Tensor action;
	
	@Override
	public void setup(Map<String, String> config, Environment env, NeuralNetwork... nns) throws Exception {
		this.policy = nns[0];
		this.config = DianneConfigHandler.getConfig(config, GaussianNoiseConfig.class);
		this.actionDims = env.actionDims()[0];
		this.action = new Tensor(this.actionDims);
	}

	@Override
	public AgentProgress processIteration(long i, Tensor state) throws Exception {
		Tensor actionParams = policy.forward(state);
		
		Tensor means = actionParams.narrow(0, 0, actionDims);
		Tensor stdevs = actionParams.narrow(0, actionDims, actionDims);
		
		action.randn();
		
		TensorOps.cmul(action, action, stdevs);
		TensorOps.add(action, action, means);
		
		for(int a = 0; a < action.size(); a++) {
			float v = action.get(a);
			if(v < config.min)
				action.set(config.min, a);
			else if(v > config.max)
				action.set(config.max, a);
		}
		
		return new AgentProgress(i, action);
	}

}
{'fraction_non_alphanumeric': 0.11685625646328852, 'fraction_numerical': 0.003791795932437091, 'mean_word_length': 4.780876494023905, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '685837', 'n_tokens_mistral': 1060, 'n_tokens_neox': 974, 'n_words': 197}