All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.drools.planner.examples.cloudbalancing.persistence.CloudBalancingGenerator Maven / Gradle / Ivy

Go to download

Drools Planner optimizes automated planning by combining metaheuristic search algorithms with rule engine powered score calculation. This is the drools-planner-examples module which contains examples on how to use Drools Planner.

There is a newer version: 6.0.0.Alpha9
Show newest version
/*
 * Copyright 2010 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.drools.planner.examples.cloudbalancing.persistence;

import java.io.File;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.drools.planner.examples.cloudbalancing.domain.CloudAssignment;
import org.drools.planner.examples.cloudbalancing.domain.CloudBalance;
import org.drools.planner.examples.cloudbalancing.domain.CloudComputer;
import org.drools.planner.examples.cloudbalancing.domain.CloudProcess;
import org.drools.planner.examples.common.app.LoggingMain;
import org.drools.planner.examples.common.persistence.SolutionDao;

public class CloudBalancingGenerator extends LoggingMain {

    private static class Price {

        private int hardwareValue;
        private String description;
        private int cost;

        private Price(int hardwareValue, String description, int cost) {
            this.hardwareValue = hardwareValue;
            this.description = description;
            this.cost = cost;
        }

        public int getHardwareValue() {
            return hardwareValue;
        }

        public String getDescription() {
            return description;
        }

        public int getCost() {
            return cost;
        }
    }

    private static final Price[] CPU_POWER_PRICES = { // in gigahertz
            new Price(3, "single core 3ghz", 110),
            new Price(4, "dual core 2ghz", 140),
            new Price(6, "dual core 3ghz", 180),
            new Price(8, "quad core 2ghz", 270),
            new Price(12, "quad core 3ghz", 400),
            new Price(16, "quad core 4ghz", 1000),
            new Price(24, "eight core 3ghz", 3000),
    };
    private static final Price[] MEMORY_PRICES = { // in gigabyte RAM
            new Price(2, "2 gigabyte", 140),
            new Price(4, "4 gigabyte", 180),
            new Price(8, "8 gigabyte", 220),
            new Price(16, "16 gigabyte", 300),
            new Price(32, "32 gigabyte", 400),
            new Price(64, "64 gigabyte", 600),
            new Price(96, "96 gigabyte", 1000),
    };
    private static final Price[] NETWORK_BANDWIDTH_PRICES = { // in gigabyte per hour
            new Price(2, "2 gigabyte", 100),
            new Price(4, "4 gigabyte", 200),
            new Price(6, "6 gigabyte", 300),
            new Price(8, "8 gigabyte", 400),
            new Price(12, "12 gigabyte", 600),
            new Price(16, "16 gigabyte", 800),
            new Price(20, "20 gigabyte", 1000),
    };

    private static final int MAXIMUM_MINIMAL_CPU_POWER = 12; // in gigahertz
    private static final int MAXIMUM_MINIMAL_MEMORY = 32; // in gigabyte RAM
    private static final int MAXIMUM_MINIMAL_NETWORK_BANDWIDTH = 12; // in gigabyte per hour

    private static final File outputDir = new File("data/cloudbalancing/unsolved/");

    public static void main(String[] args) {
        new CloudBalancingGenerator().generate();
    }

    protected SolutionDao solutionDao;
    private Random random;

    public CloudBalancingGenerator() {
        checkConfiguration();
        solutionDao = new CloudBalancingDaoImpl();
    }

    public void generate() {
        writeCloudBalance(2, 6);
        writeCloudBalance(3, 9);
        writeCloudBalance(4, 12);
        writeCloudBalance(100, 300);
        writeCloudBalance(200, 600);
        writeCloudBalance(400, 1200);
        writeCloudBalance(800, 2400);
    }

    private void checkConfiguration() {
        if (CPU_POWER_PRICES.length != MEMORY_PRICES.length || CPU_POWER_PRICES.length != NETWORK_BANDWIDTH_PRICES.length) {
            throw new IllegalStateException("All price arrays must be equal in length.");
        }
    }

    private void writeCloudBalance(int cloudComputerListSize, int cloudProcessListSize) {
        File outputFile = determineOutputFile(cloudComputerListSize, cloudProcessListSize);
        CloudBalance cloudBalance = createCloudBalance(cloudComputerListSize, cloudProcessListSize);
        solutionDao.writeSolution(cloudBalance, outputFile);
    }

    private File determineOutputFile(int cloudComputerListSize, int cloudProcessListSize) {
        String cloudComputerListSizeString = Integer.toString(cloudComputerListSize);
        if (cloudComputerListSizeString.length() < 4) {
            cloudComputerListSizeString = "0000".substring(0, 4 - cloudComputerListSizeString.length()) + cloudComputerListSizeString;
        }
        String cloudProcessListSizeString = Integer.toString(cloudProcessListSize);
        if (cloudProcessListSizeString.length() < 4) {
            cloudProcessListSizeString = "0000".substring(0, 4 - cloudProcessListSizeString.length()) + cloudProcessListSizeString;
        }
        String outputFileName = "cb-" + cloudComputerListSizeString + "comp-" + cloudProcessListSizeString + "proc.xml";
        return new File(outputDir, outputFileName);
    }

    private CloudBalance createCloudBalance(int cloudComputerListSize, int cloudProcessListSize) {
        random = new Random(47);
        CloudBalance cloudBalance = new CloudBalance();
        cloudBalance.setId(0L);
        createCloudComputerList(cloudBalance,cloudComputerListSize);
        createCloudProcessList(cloudBalance, cloudProcessListSize);
        createCloudAssignmentList(cloudBalance);
        logger.info("CloudBalance {} with {} computers and {} processes.",
                cloudBalance.getCloudComputerList().size(), cloudBalance.getCloudProcessList().size());
        BigInteger possibleSolutionSize = BigInteger.valueOf(cloudBalance.getCloudComputerList().size()).pow(
                cloudBalance.getCloudAssignmentList().size());
        String flooredPossibleSolutionSize = "10^" + (possibleSolutionSize.toString().length() - 1);
        logger.info("CloudBalance with flooredPossibleSolutionSize ({}) and possibleSolutionSize({}).",
                flooredPossibleSolutionSize, possibleSolutionSize);
        return cloudBalance;
    }

    private void createCloudComputerList(CloudBalance cloudBalance, int cloudComputerListSize) {
        List cloudComputerList = new ArrayList(cloudComputerListSize);
        for (int i = 0; i < cloudComputerListSize; i++) {
            CloudComputer cloudComputer = new CloudComputer();
            cloudComputer.setId((long) i);
            int cpuPowerPricesIndex = random.nextInt(CPU_POWER_PRICES.length);
            cloudComputer.setCpuPower(CPU_POWER_PRICES[cpuPowerPricesIndex].getHardwareValue());
            int memoryPricesIndex = distortIndex(cpuPowerPricesIndex, MEMORY_PRICES.length);
            cloudComputer.setMemory(MEMORY_PRICES[memoryPricesIndex].getHardwareValue());
            int networkBandwidthPricesIndex = distortIndex(cpuPowerPricesIndex, NETWORK_BANDWIDTH_PRICES.length);
            cloudComputer.setNetworkBandwidth(NETWORK_BANDWIDTH_PRICES[networkBandwidthPricesIndex].getHardwareValue());
            int cost = CPU_POWER_PRICES[cpuPowerPricesIndex].getCost()
                    + MEMORY_PRICES[memoryPricesIndex].getCost()
                    + NETWORK_BANDWIDTH_PRICES[networkBandwidthPricesIndex].getCost();
            logger.debug("Created cloudComputer with cpuPowerPricesIndex ({}), memoryPricesIndex({}),"
                    + " networkBandwidthPricesIndex({}).",
                    new Object[]{cpuPowerPricesIndex, memoryPricesIndex, networkBandwidthPricesIndex});
            cloudComputer.setCost(cost);
            cloudComputerList.add(cloudComputer);
        }
        cloudBalance.setCloudComputerList(cloudComputerList);
    }

    private int distortIndex(int referenceIndex, int length) {
        int index = referenceIndex;
        double randomDouble = random.nextDouble();
        double loweringThreshold = 0.25;
        while (randomDouble < loweringThreshold && index >= 1) {
            index--;
            loweringThreshold *= 0.10;
        }
        double heighteningThreshold = 0.75;
        while (randomDouble >= heighteningThreshold && index <= (length - 2)) {
            index++;
            heighteningThreshold = (1.0 - ((1.0 - heighteningThreshold) * 0.10));
        }
        return index;
    }

    private void createCloudProcessList(CloudBalance cloudBalance, int cloudProcessListSize) {
        List cloudProcessList = new ArrayList(cloudProcessListSize);
        for (int i = 0; i < cloudProcessListSize; i++) {
            CloudProcess cloudProcess = new CloudProcess();
            cloudProcess.setId((long) i);
            int minimalCpuPower = generateRandom(MAXIMUM_MINIMAL_CPU_POWER);
            cloudProcess.setMinimalCpuPower(minimalCpuPower);
            int minimalMemory = generateRandom(MAXIMUM_MINIMAL_MEMORY);
            cloudProcess.setMinimalMemory(minimalMemory);
            int minimalNetworkBandwidth = generateRandom(MAXIMUM_MINIMAL_NETWORK_BANDWIDTH);
            cloudProcess.setMinimalNetworkBandwidth(minimalNetworkBandwidth);
            logger.debug("Created CloudProcess with minimalCpuPower ({}), minimalMemory({}),"
                    + " minimalNetworkBandwidth({}).",
                    new Object[]{minimalCpuPower, minimalMemory, minimalNetworkBandwidth});
            cloudProcessList.add(cloudProcess);
        }
        cloudBalance.setCloudProcessList(cloudProcessList);
    }

    private int generateRandom(int maximumValue) {
        double randomDouble = random.nextDouble();
        double parabolaBase = 2000.0;
        double parabolaRandomDouble = (Math.pow(parabolaBase, randomDouble) - 1.0) / (parabolaBase - 1.0);
        if (parabolaRandomDouble < 0.0 || parabolaRandomDouble >= 1.0) {
            throw new IllegalArgumentException("Invalid generated parabolaRandomDouble (" + parabolaRandomDouble + ")");
        }
        int value = ((int) Math.floor(parabolaRandomDouble * ((double) maximumValue))) + 1;
        if (value < 1 || value > maximumValue) {
            throw new IllegalArgumentException("Invalid generated value (" + value + ")");
        }
        return value;
    }

    private void createCloudAssignmentList(CloudBalance cloudBalance) {
        List cloudProcessList = cloudBalance.getCloudProcessList();
        List cloudAssignmentList = new ArrayList(cloudProcessList.size());
        long id = 0L;
        for (CloudProcess cloudProcess : cloudProcessList) {
            CloudAssignment cloudAssignment = new CloudAssignment();
            cloudAssignment.setId(id);
            cloudAssignment.setCloudProcess(cloudProcess);
            // Notice that we leave the PlanningVariable properties on null
            cloudAssignmentList.add(cloudAssignment);
            id++;
        }
        cloudBalance.setCloudAssignmentList(cloudAssignmentList);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy