com.anhth12.lambda.ml.param.DiscreteAround.java Source code

Java tutorial

Introduction

Here is the source code for com.anhth12.lambda.ml.param.DiscreteAround.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anhth12.lambda.ml.param;

import com.google.common.base.Preconditions;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * @author Tong Hoang Anh
 */
final class DiscreteAround implements HyperParamValues<Integer>, Serializable {

    private final int around;
    private final int step;

    public DiscreteAround(int around, int step) {
        Preconditions.checkArgument(step > 0);
        this.around = around;
        this.step = step;
    }

    @Override
    public List<Integer> getTrialValues(int num) {
        Preconditions.checkArgument(num > 0);
        if (num == 1) {
            return Collections.singletonList(around);
        }

        List<Integer> values = new ArrayList<>(num);
        int value = around - ((num - 1) * step / 2);

        for (int i = 0; i < num; i++) {
            values.add(value);
            value += step;
        }

        return values;
    }

    @Override
    public String toString() {
        return "DiscreteAround[..." + getTrialValues(3) + "...]";
    }

}