Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.itmo.degtiarenko;
- import java.io.PrintWriter;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import java.util.function.Function;
- public class HTask {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- PrintWriter out = new PrintWriter(System.out);
- Integer dim = in.nextInt();
- List<Double> minimumPoint = new ArrayList<>();
- for (int i = 0; i < dim; i++) {
- minimumPoint.add(findDimMinimumPoint(i, dim, in, out));
- }
- Double minValue = askFunctionValue(minimumPoint, in, out);
- System.out.println(String.format("minimum %f", minValue));
- in.close();
- out.close();
- }
- private static Double askFunctionValue(List<Double> point, Scanner in, PrintWriter out) {
- StringBuilder builder = new StringBuilder();
- for (Double val : point) {
- final double roundedVal = new BigDecimal(val)
- .setScale(10, RoundingMode.HALF_UP)
- .doubleValue();
- builder.append(roundedVal);
- builder.append(' ');
- }
- builder.append('\n');
- out.write(builder.toString());
- out.flush();
- return in.nextDouble();
- }
- private static Double findDimMinimumPoint(int dimension, int dimensionAmount,
- Scanner in, PrintWriter out)
- {
- List<Double> point = new ArrayList<>(dimensionAmount);
- Function<Double, Double> function = val -> {
- point.set(dimension, val);
- return askFunctionValue(point, in, out);
- };
- List<Double> possibleMins = new ArrayList<>();
- for (double s = 0.0; s < 1.0; s += 0.025) {
- point.clear();
- for (int i = 0; i < dimensionAmount; i++) {
- point.add(0.0);
- }
- possibleMins.add(findMinPointOnInterval(function, s, s + 0.025, 10e-6));
- }
- return possibleMins
- .stream()
- .mapToDouble(d -> d)
- .min()
- .getAsDouble();
- }
- private static double findMinPointOnInterval(Function<Double, Double> f, double left,
- double right, double eps)
- {
- while (right - left > eps) {
- double a = (left * 2 + right) / 3;
- double b = (left + right * 2) / 3;
- if (f.apply(a) < f.apply(b)) {
- right = b;
- } else {
- left = a;
- }
- }
- return (left + right) / 2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement