package com.jdt.fedlearn.core.parameter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jdt.fedlearn.core.parameter.common.CategoryParameter;
import com.jdt.fedlearn.core.parameter.common.CommonParameter;
import com.jdt.fedlearn.core.parameter.common.NumberParameter;
import com.jdt.fedlearn.core.parameter.common.ParameterField;
import com.jdt.fedlearn.core.type.MetricType;
import com.jdt.fedlearn.core.type.ObjectiveType;
import com.jdt.fedlearn.core.type.ParameterType;
import com.jdt.fedlearn.common.entity.core.type.AlgorithmType;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestCommonParameter {
    @Test
    public void testConvertListToFgbParameter() {
        //TODO 构造一个
        String content = "{\"username\": \"nlp\", \"trainIdId\": 29, \"model\": \"FederatedGB\", \"commonParams\": [], \"algorithmParams\": [{\"field\": \"num_boost_round\", \"name\": \"\\u6811\\u7684\\u4e2a\\u6570\", \"value\": 1, \"describe\": [\"1\", \"100\"], \"defaultValue\": \"50\", \"type\": \"NUMS\"}, {\"field\": \"first_round_pred\", \"name\": \"\\u521d\\u59cb\\u5316\\u9884\\u6d4b\\u503c\", \"value\": 0, \"describe\": [\"0.0\", \"1.0\"], \"defaultValue\": \"0.0\", \"type\": \"NUMS\"}, {\"field\": \"maximize\", \"name\": \"maximize\", \"value\": \"true\", \"describe\": [\"true\", \"false\"], \"defaultValue\": \"true\", \"type\": \"STRING\"}, {\"field\": \"rowsample\", \"name\": \"\\u6837\\u672c\\u62bd\\u6837\\u6bd4\\u4f8b\", \"value\": 1, \"describe\": [\"0.1\", \"1.0\"], \"defaultValue\": \"1.0\", \"type\": \"NUMS\"}, {\"field\": \"colsample\", \"name\": \"\\u5217\\u62bd\\u6837\\u6bd4\\u4f8b\", \"value\": 1, \"describe\": [\"0.1\", \"1.0\"], \"defaultValue\": \"1.0\", \"type\": \"NUMS\"}, {\"field\": \"early_stopping_round\", \"name\": \"\\u65e9\\u505c\\u8f6e\\u6570\", \"value\": 10, \"describe\": [\"1\", \"20\"], \"defaultValue\": \"10\", \"type\": \"NUMS\"}, {\"field\": \"min_child_weight\", \"name\": \"min_child_weight\", \"value\": 1, \"describe\": [\"1\", \"10\"], \"defaultValue\": \"1\", \"type\": \"NUMS\"}, {\"field\": \"min_sample_split\", \"name\": \"min_sample_split\", \"value\": 10, \"describe\": [\"1\", \"20\"], \"defaultValue\": \"10\", \"type\": \"NUMS\"}, {\"field\": \"lambda\", \"name\": \"lambda\", \"value\": 1, \"describe\": [\"1\", \"20\"], \"defaultValue\": \"1\", \"type\": \"NUMS\"}, {\"field\": \"gamma\", \"name\": \"gamma\", \"value\": 0, \"describe\": [\"0\", \"1\"], \"defaultValue\": \"0\", \"type\": \"NUMS\"}, {\"field\": \"scale_pos_weight\", \"name\": \"scale_pos_weight\", \"value\": 1, \"describe\": [\"0\", \"1\"], \"defaultValue\": \"1\", \"type\": \"NUMS\"}, {\"field\": \"num_bin\", \"name\": \"\\u7279\\u5f81\\u5206\\u6876\\u4e2a\\u6570\", \"value\": 33, \"describe\": [\"33\", \"50\"], \"defaultValue\": \"33\", \"type\": \"NUMS\"}, {\"field\": \"eval_metric\", \"name\": \"eval_metric\", \"value\": [\"MAPE\",\"MAE\"], \"describe\": [\"mse\", \"mae\", \"rmse\", \"mape\"], \"defaultValue\": \"mape\", \"type\": \"STRING\"}, {\"field\": \"max_depth\", \"name\": \"max_depth\", \"value\": 7, \"describe\": [\"5\", \"20\"], \"defaultValue\": \"7\", \"type\": \"NUMS\"}, {\"field\": \"eta\", \"name\": \"learning rate\", \"value\": 0.3, \"describe\": [\"0.01\", \"1\"], \"defaultValue\": \"0.3\", \"type\": \"NUMS\"}, {\"field\": \"objective\", \"name\": \"objective\", \"value\": \"reg:square\", \"describe\": [\"reg:logistic\", \"reg:square\", \"count:poisson\", \"binary:logistic\"], \"defaultValue\": \"count:poisson\", \"type\": \"STRING\"}, {\"field\": \"cat_features\", \"name\": \"cat_features\", \"value\": \"1\", \"describe\": [], \"defaultValue\": \"\", \"type\": \"STRING\"}, {\"field\": \"randomized_response_probability\", \"name\": \"randomized_response_probability\", \"value\": 0, \"describe\": [\"0\", \"1\"], \"defaultValue\": \"0\", \"type\": \"NUMS\"}, {\"field\": \"differential_privacy_parameter\", \"name\": \"differential_privacy_parameter\", \"value\": 0, \"describe\": [\"0\", \"1\"], \"defaultValue\": \"0\", \"type\": \"NUMS\"}, {\"field\": \"label\", \"name\": \"\\u9884\\u6d4b\\u6807\\u7b7e\", \"value\": \"y\", \"describe\": [\"14\", \"15\", \"16\", \"17\", \"18\", \"19\", \"20\", \"21\", \"22\", \"23\", \"24\", \"25\", \"26\", \"uid\", \"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\", \"11\", \"12\", \"13\", \"27\", \"28\", \"29\", \"30\", \"31\", \"32\", \"33\", \"34\", \"35\", \"36\", \"37\", \"38\", \"39\", \"y\"], \"defaultValue\": \"y\", \"type\": \"STRING\"}]}";
//        String content = "{\"username\": \"nlp\", \"taskId\": 29, \"model\": \"FederatedGB\", \"commonParams\": [], \"algorithmParams\": [{\"field\": \"num_boost_round\", \"name\": \"\\u6811\\u7684\\u4e2a\\u6570\", \"value\": 1, \"describe\": [\"1\", \"100\"], \"defaultValue\": \"50\", \"type\": \"NUMS\"}, {\"field\": \"first_round_pred\", \"name\": \"\\u521d\\u59cb\\u5316\\u9884\\u6d4b\\u503c\", \"value\": 0, \"describe\": [\"0.0\", \"1.0\"], \"defaultValue\": \"0.0\", \"type\": \"NUMS\"}, {\"field\": \"maximize\", \"name\": \"maximize\", \"value\": \"true\", \"describe\": [\"true\", \"false\"], \"defaultValue\": \"true\", \"type\": \"STRING\"}, {\"field\": \"rowsample\", \"name\": \"\\u6837\\u672c\\u62bd\\u6837\\u6bd4\\u4f8b\", \"value\": 1, \"describe\": [\"0.1\", \"1.0\"], \"defaultValue\": \"1.0\", \"type\": \"NUMS\"}, {\"field\": \"colsample\", \"name\": \"\\u5217\\u62bd\\u6837\\u6bd4\\u4f8b\", \"value\": 1, \"describe\": [\"0.1\", \"1.0\"], \"defaultValue\": \"1.0\", \"type\": \"NUMS\"}, {\"field\": \"early_stopping_round\", \"name\": \"\\u65e9\\u505c\\u8f6e\\u6570\", \"value\": 10, \"describe\": [\"1\", \"20\"], \"defaultValue\": \"10\", \"type\": \"NUMS\"}, {\"field\": \"min_child_weight\", \"name\": \"min_child_weight\", \"value\": 1, \"describe\": [\"1\", \"10\"], \"defaultValue\": \"1\", \"type\": \"NUMS\"}, {\"field\": \"min_sample_split\", \"name\": \"min_sample_split\", \"value\": 10, \"describe\": [\"1\", \"20\"], \"defaultValue\": \"10\", \"type\": \"NUMS\"}, {\"field\": \"lambda\", \"name\": \"lambda\", \"value\": 1, \"describe\": [\"1\", \"20\"], \"defaultValue\": \"1\", \"type\": \"NUMS\"}, {\"field\": \"gamma\", \"name\": \"gamma\", \"value\": 0, \"describe\": [\"0\", \"1\"], \"defaultValue\": \"0\", \"type\": \"NUMS\"}, {\"field\": \"scale_pos_weight\", \"name\": \"scale_pos_weight\", \"value\": 1, \"describe\": [\"0\", \"1\"], \"defaultValue\": \"1\", \"type\": \"NUMS\"}, {\"field\": \"num_bin\", \"name\": \"\\u7279\\u5f81\\u5206\\u6876\\u4e2a\\u6570\", \"value\": 33, \"describe\": [\"33\", \"50\"], \"defaultValue\": \"33\", \"type\": \"NUMS\"}, {\"field\": \"eval_metric\", \"name\": \"eval_metric\", \"value\": [\"mape\",\"MAE\"], \"describe\": [\"mse\", \"mae\", \"rmse\", \"mape\"], \"defaultValue\": \"mape\", \"type\": \"STRING\"}, {\"field\": \"max_depth\", \"name\": \"max_depth\", \"value\": 7, \"describe\": [\"5\", \"20\"], \"defaultValue\": \"7\", \"type\": \"NUMS\"}, {\"field\": \"eta\", \"name\": \"learning rate\", \"value\": 0.3, \"describe\": [\"0.01\", \"1\"], \"defaultValue\": \"0.3\", \"type\": \"NUMS\"}, {\"field\": \"objective\", \"name\": \"objective\", \"value\": \"reg:square\", \"describe\": [\"reg:logistic\", \"reg:square\", \"count:poisson\", \"binary:logistic\"], \"defaultValue\": \"count:poisson\", \"type\": \"STRING\"}, {\"field\": \"cat_features\", \"name\": \"cat_features\", \"value\": \"1\", \"describe\": [], \"defaultValue\": \"\", \"type\": \"STRING\"}, {\"field\": \"randomized_response_probability\", \"name\": \"randomized_response_probability\", \"value\": 0, \"describe\": [\"0\", \"1\"], \"defaultValue\": \"0\", \"type\": \"NUMS\"}, {\"field\": \"differential_privacy_parameter\", \"name\": \"differential_privacy_parameter\", \"value\": 0, \"describe\": [\"0\", \"1\"], \"defaultValue\": \"0\", \"type\": \"NUMS\"}, {\"field\": \"label\", \"name\": \"\\u9884\\u6d4b\\u6807\\u7b7e\", \"value\": \"y\", \"describe\": [\"14\", \"15\", \"16\", \"17\", \"18\", \"19\", \"20\", \"21\", \"22\", \"23\", \"24\", \"25\", \"26\", \"uid\", \"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\", \"11\", \"12\", \"13\", \"27\", \"28\", \"29\", \"30\", \"31\", \"32\", \"33\", \"34\", \"35\", \"36\", \"37\", \"38\", \"39\", \"y\"], \"defaultValue\": \"y\", \"type\": \"STRING\"}]}";
        List<ParameterField> parameterFieldList = new ArrayList<>();
        ParameterField numBoostRound = new NumberParameter("num_boost_round", "树的轮数", 10, new String[]{"1", "100"}, ParameterType.NUMS);
        parameterFieldList.add(numBoostRound);
        ParameterField firstRoundPredict = new CategoryParameter("firstRoundPred", "初始化预测值", "AVG", new String[]{"ZERO", "AVG", "RANDOM"}, ParameterType.STRING);
        parameterFieldList.add(firstRoundPredict);
//        System.out.println(query.toString());
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("numBoostRound", 5000);
        FgbParameter a = (FgbParameter) CommonParameter.parseParameter(parameters, AlgorithmType.FederatedGB);
        System.out.println("a: " + a.toString());
    }


    @Test
    public void testConvertListToKernelParameter() {
        //TODO 构造一个
        String content = "{\"username\": \"nlp\", \"taskId\": 29, \"model\": \"FederatedGB\", \"commonParams\": [], \"algorithmParams\": [{\"field\": \"num_boost_round\", \"name\": \"\\u6811\\u7684\\u4e2a\\u6570\", \"value\": 1, \"describe\": [\"1\", \"100\"], \"defaultValue\": \"50\", \"type\": \"NUMS\"}, {\"field\": \"first_round_pred\", \"name\": \"\\u521d\\u59cb\\u5316\\u9884\\u6d4b\\u503c\", \"value\": 0, \"describe\": [\"0.0\", \"1.0\"], \"defaultValue\": \"0.0\", \"type\": \"NUMS\"}, {\"field\": \"maximize\", \"name\": \"maximize\", \"value\": \"true\", \"describe\": [\"true\", \"false\"], \"defaultValue\": \"true\", \"type\": \"STRING\"}, {\"field\": \"rowsample\", \"name\": \"\\u6837\\u672c\\u62bd\\u6837\\u6bd4\\u4f8b\", \"value\": 1, \"describe\": [\"0.1\", \"1.0\"], \"defaultValue\": \"1.0\", \"type\": \"NUMS\"}, {\"field\": \"colsample\", \"name\": \"\\u5217\\u62bd\\u6837\\u6bd4\\u4f8b\", \"value\": 1, \"describe\": [\"0.1\", \"1.0\"], \"defaultValue\": \"1.0\", \"type\": \"NUMS\"}, {\"field\": \"early_stopping_round\", \"name\": \"\\u65e9\\u505c\\u8f6e\\u6570\", \"value\": 10, \"describe\": [\"1\", \"20\"], \"defaultValue\": \"10\", \"type\": \"NUMS\"}, {\"field\": \"min_child_weight\", \"name\": \"min_child_weight\", \"value\": 1, \"describe\": [\"1\", \"10\"], \"defaultValue\": \"1\", \"type\": \"NUMS\"}, {\"field\": \"min_sample_split\", \"name\": \"min_sample_split\", \"value\": 10, \"describe\": [\"1\", \"20\"], \"defaultValue\": \"10\", \"type\": \"NUMS\"}, {\"field\": \"lambda\", \"name\": \"lambda\", \"value\": 1, \"describe\": [\"1\", \"20\"], \"defaultValue\": \"1\", \"type\": \"NUMS\"}, {\"field\": \"gamma\", \"name\": \"gamma\", \"value\": 0, \"describe\": [\"0\", \"1\"], \"defaultValue\": \"0\", \"type\": \"NUMS\"}, {\"field\": \"scale_pos_weight\", \"name\": \"scale_pos_weight\", \"value\": 1, \"describe\": [\"0\", \"1\"], \"defaultValue\": \"1\", \"type\": \"NUMS\"}, {\"field\": \"num_bin\", \"name\": \"\\u7279\\u5f81\\u5206\\u6876\\u4e2a\\u6570\", \"value\": 33, \"describe\": [\"33\", \"50\"], \"defaultValue\": \"33\", \"type\": \"NUMS\"}, {\"field\": \"eval_metric\", \"name\": \"eval_metric\", \"value\": [\"MAPE\",\"MAE\"], \"describe\": [\"mse\", \"mae\", \"rmse\", \"mape\"], \"defaultValue\": \"mape\", \"type\": \"STRING\"}, {\"field\": \"max_depth\", \"name\": \"max_depth\", \"value\": 7, \"describe\": [\"5\", \"20\"], \"defaultValue\": \"7\", \"type\": \"NUMS\"}, {\"field\": \"eta\", \"name\": \"learning rate\", \"value\": 0.3, \"describe\": [\"0.01\", \"1\"], \"defaultValue\": \"0.3\", \"type\": \"NUMS\"}, {\"field\": \"objective\", \"name\": \"objective\", \"value\": \"reg:square\", \"describe\": [\"reg:logistic\", \"reg:square\", \"count:poisson\", \"binary:logistic\"], \"defaultValue\": \"count:poisson\", \"type\": \"STRING\"}, {\"field\": \"cat_features\", \"name\": \"cat_features\", \"value\": \"1\", \"describe\": [], \"defaultValue\": \"\", \"type\": \"STRING\"}, {\"field\": \"randomized_response_probability\", \"name\": \"randomized_response_probability\", \"value\": 0, \"describe\": [\"0\", \"1\"], \"defaultValue\": \"0\", \"type\": \"NUMS\"}, {\"field\": \"differential_privacy_parameter\", \"name\": \"differential_privacy_parameter\", \"value\": 0, \"describe\": [\"0\", \"1\"], \"defaultValue\": \"0\", \"type\": \"NUMS\"}, {\"field\": \"label\", \"name\": \"\\u9884\\u6d4b\\u6807\\u7b7e\", \"value\": \"y\", \"describe\": [\"14\", \"15\", \"16\", \"17\", \"18\", \"19\", \"20\", \"21\", \"22\", \"23\", \"24\", \"25\", \"26\", \"uid\", \"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\", \"11\", \"12\", \"13\", \"27\", \"28\", \"29\", \"30\", \"31\", \"32\", \"33\", \"34\", \"35\", \"36\", \"37\", \"38\", \"39\", \"y\"], \"defaultValue\": \"y\", \"type\": \"STRING\"}]}";
//        String content = "{\"username\": \"nlp\", \"taskId\": 29, \"model\": \"FederatedGB\", \"commonParams\": [], \"algorithmParams\": [{\"field\": \"num_boost_round\", \"name\": \"\\u6811\\u7684\\u4e2a\\u6570\", \"value\": 1, \"describe\": [\"1\", \"100\"], \"defaultValue\": \"50\", \"type\": \"NUMS\"}, {\"field\": \"first_round_pred\", \"name\": \"\\u521d\\u59cb\\u5316\\u9884\\u6d4b\\u503c\", \"value\": 0, \"describe\": [\"0.0\", \"1.0\"], \"defaultValue\": \"0.0\", \"type\": \"NUMS\"}, {\"field\": \"maximize\", \"name\": \"maximize\", \"value\": \"true\", \"describe\": [\"true\", \"false\"], \"defaultValue\": \"true\", \"type\": \"STRING\"}, {\"field\": \"rowsample\", \"name\": \"\\u6837\\u672c\\u62bd\\u6837\\u6bd4\\u4f8b\", \"value\": 1, \"describe\": [\"0.1\", \"1.0\"], \"defaultValue\": \"1.0\", \"type\": \"NUMS\"}, {\"field\": \"colsample\", \"name\": \"\\u5217\\u62bd\\u6837\\u6bd4\\u4f8b\", \"value\": 1, \"describe\": [\"0.1\", \"1.0\"], \"defaultValue\": \"1.0\", \"type\": \"NUMS\"}, {\"field\": \"early_stopping_round\", \"name\": \"\\u65e9\\u505c\\u8f6e\\u6570\", \"value\": 10, \"describe\": [\"1\", \"20\"], \"defaultValue\": \"10\", \"type\": \"NUMS\"}, {\"field\": \"min_child_weight\", \"name\": \"min_child_weight\", \"value\": 1, \"describe\": [\"1\", \"10\"], \"defaultValue\": \"1\", \"type\": \"NUMS\"}, {\"field\": \"min_sample_split\", \"name\": \"min_sample_split\", \"value\": 10, \"describe\": [\"1\", \"20\"], \"defaultValue\": \"10\", \"type\": \"NUMS\"}, {\"field\": \"lambda\", \"name\": \"lambda\", \"value\": 1, \"describe\": [\"1\", \"20\"], \"defaultValue\": \"1\", \"type\": \"NUMS\"}, {\"field\": \"gamma\", \"name\": \"gamma\", \"value\": 0, \"describe\": [\"0\", \"1\"], \"defaultValue\": \"0\", \"type\": \"NUMS\"}, {\"field\": \"scale_pos_weight\", \"name\": \"scale_pos_weight\", \"value\": 1, \"describe\": [\"0\", \"1\"], \"defaultValue\": \"1\", \"type\": \"NUMS\"}, {\"field\": \"num_bin\", \"name\": \"\\u7279\\u5f81\\u5206\\u6876\\u4e2a\\u6570\", \"value\": 33, \"describe\": [\"33\", \"50\"], \"defaultValue\": \"33\", \"type\": \"NUMS\"}, {\"field\": \"eval_metric\", \"name\": \"eval_metric\", \"value\": [\"mape\",\"MAE\"], \"describe\": [\"mse\", \"mae\", \"rmse\", \"mape\"], \"defaultValue\": \"mape\", \"type\": \"STRING\"}, {\"field\": \"max_depth\", \"name\": \"max_depth\", \"value\": 7, \"describe\": [\"5\", \"20\"], \"defaultValue\": \"7\", \"type\": \"NUMS\"}, {\"field\": \"eta\", \"name\": \"learning rate\", \"value\": 0.3, \"describe\": [\"0.01\", \"1\"], \"defaultValue\": \"0.3\", \"type\": \"NUMS\"}, {\"field\": \"objective\", \"name\": \"objective\", \"value\": \"reg:square\", \"describe\": [\"reg:logistic\", \"reg:square\", \"count:poisson\", \"binary:logistic\"], \"defaultValue\": \"count:poisson\", \"type\": \"STRING\"}, {\"field\": \"cat_features\", \"name\": \"cat_features\", \"value\": \"1\", \"describe\": [], \"defaultValue\": \"\", \"type\": \"STRING\"}, {\"field\": \"randomized_response_probability\", \"name\": \"randomized_response_probability\", \"value\": 0, \"describe\": [\"0\", \"1\"], \"defaultValue\": \"0\", \"type\": \"NUMS\"}, {\"field\": \"differential_privacy_parameter\", \"name\": \"differential_privacy_parameter\", \"value\": 0, \"describe\": [\"0\", \"1\"], \"defaultValue\": \"0\", \"type\": \"NUMS\"}, {\"field\": \"label\", \"name\": \"\\u9884\\u6d4b\\u6807\\u7b7e\", \"value\": \"y\", \"describe\": [\"14\", \"15\", \"16\", \"17\", \"18\", \"19\", \"20\", \"21\", \"22\", \"23\", \"24\", \"25\", \"26\", \"uid\", \"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\", \"11\", \"12\", \"13\", \"27\", \"28\", \"29\", \"30\", \"31\", \"32\", \"33\", \"34\", \"35\", \"36\", \"37\", \"38\", \"39\", \"y\"], \"defaultValue\": \"y\", \"type\": \"STRING\"}]}";
        List<ParameterField> parameterFieldList = new ArrayList<>();
        ParameterField numBoostRound = new NumberParameter("batchSize", "随机样本采样个数", 100000, new String[]{"1000", "5000000"}, ParameterType.NUMS);
        parameterFieldList.add(numBoostRound);
        Map<String, Object> parameters = new HashMap<>();
        FederatedKernelParameter a = (FederatedKernelParameter) CommonParameter.parseParameter(parameters, AlgorithmType.FederatedKernel);
        //System.out.println("a: " + a.getBatchSize());
    }

    @Test
    public void testConstructList() {
        List<ParameterField> a = CommonParameter.constructList(AlgorithmType.MixGBoost);
        System.out.println("a: " + a.toString());
    }

    @Test
    public void testSerializeParameter() throws JsonProcessingException {
        FgbParameter parameter = new FgbParameter.Builder(3, new MetricType[]{MetricType.ACC}, ObjectiveType.regLogistic).build();

        ObjectMapper objectMapper = new ObjectMapper();
        String res = objectMapper.writeValueAsString(parameter);
        System.out.println(res);
    }
}
