2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.examples.adaptive.model.java;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.Random;
27 import org.onap.policy.apex.context.ContextException;
28 import org.onap.policy.apex.core.engine.executor.context.TaskSelectionExecutionContext;
29 import org.onap.policy.apex.examples.adaptive.concepts.AutoLearn;
32 * The Class AutoLearnPolicyDecideTaskSelectionLogic.
34 public class AutoLearnPolicyDecideTaskSelectionLogic {
35 // Recurring string constants
36 private static final String AUTO_LEARN_ALBUM = "AutoLearnAlbum";
37 private static final String AUTO_LEARN = "AutoLearn";
39 private static final Random RAND = new Random(System.currentTimeMillis());
40 private static final double WANT = 50.0;
46 * @param executor the executor
49 public boolean getTask(final TaskSelectionExecutionContext executor) {
50 String idString = executor.subject.getId();
51 executor.logger.debug(idString);
53 String inFieldsString = executor.inFields.toString();
54 executor.logger.debug(inFieldsString);
56 final List<String> tasks = executor.subject.getTaskNames();
60 executor.getContextAlbum(AUTO_LEARN_ALBUM).lockForWriting(AUTO_LEARN);
61 } catch (final ContextException e) {
62 executor.logger.error("Failed to acquire write lock on \"autoLearn\" context", e);
66 // Get the context object
67 AutoLearn autoLearn = (AutoLearn) executor.getContextAlbum(AUTO_LEARN_ALBUM).get(AUTO_LEARN);
68 if (autoLearn == null) {
69 autoLearn = new AutoLearn();
72 // Check the lists are initialized
73 if (!autoLearn.isInitialized()) {
77 final double now = (Double) (executor.inFields.get("MonitoredValue"));
78 final double diff = now - WANT;
79 final int option = getOption(diff, autoLearn);
80 learn(option, diff, autoLearn);
82 executor.getContextAlbum(AUTO_LEARN_ALBUM).put(AUTO_LEARN_ALBUM, autoLearn);
85 executor.getContextAlbum(AUTO_LEARN_ALBUM).unlockForWriting(AUTO_LEARN);
86 } catch (final ContextException e) {
87 executor.logger.error("Failed to acquire write lock on \"autoLearn\" context", e);
91 executor.subject.getTaskKey(tasks.get(option)).copyTo(executor.selectedTask);
98 * @param diff the diff
99 * @param autoLearn the auto learn
102 private int getOption(final double diff, final AutoLearn autoLearn) {
103 final Double[] avdiffs = autoLearn.getAvDiffs().toArray(new Double[autoLearn.getAvDiffs().size()]);
104 final int r = RAND.nextInt(size);
106 int closestdowni = -1;
107 double closestup = Double.MAX_VALUE;
108 double closestdown = Double.MIN_VALUE;
109 for (int i = 0; i < size; i++) {
110 if (Double.isNaN(avdiffs[i])) {
113 if (avdiffs[i] >= diff && avdiffs[i] <= closestup) {
114 closestup = avdiffs[i];
117 if (avdiffs[i] <= diff && avdiffs[i] >= closestdown) {
118 closestdown = avdiffs[i];
122 return calculateReturnValue(diff, r, closestupi, closestdowni, closestup, closestdown);
128 * @param option the option
129 * @param diff the diff
130 * @param autoLearn the auto learn
132 private void learn(final int option, final double diff, final AutoLearn autoLearn) {
133 final Double[] avdiffs = autoLearn.getAvDiffs().toArray(new Double[autoLearn.getAvDiffs().size()]);
134 final Long[] counts = autoLearn.getCounts().toArray(new Long[autoLearn.getCounts().size()]);
135 if (option < 0 || option >= avdiffs.length) {
136 throw new IllegalArgumentException("Error: option" + option);
139 if (Double.isNaN(avdiffs[option])) {
140 avdiffs[option] = diff;
142 avdiffs[option] = (avdiffs[option] * (counts[option] - 1) + diff) / counts[option];
144 autoLearn.setAvDiffs(Arrays.asList(avdiffs));
145 autoLearn.setCounts(Arrays.asList(counts));
149 * Calculate the return value of the learning.
151 * @param diff the difference
152 * @param random the random value
153 * @param closestupi closest to i upwards
154 * @param closestdowni closest to i downwards
155 * @param closestup closest up value
156 * @param closestdown closest down value
157 * @return the return value
159 private int calculateReturnValue(final double diff, final int random, int closestupi, int closestdowni,
160 double closestup, double closestdown) {
161 if (closestupi == -1 || closestdowni == -1) {
164 if (closestupi == closestdowni) {
167 if (Math.abs(closestdown - diff) > Math.abs(closestup - diff)) {