[PYTHON] I tried to solve the virtual machine placement optimization problem (simple version) with blueqat

Last time, I checked the operation of VQE using blueqat. This time, I will try to solve the virtual machine optimization problem (simplified version) with VQE of blueqat.

What is a virtual machine placement optimization problem?

In a nutshell, it is a problem that requires ** "What kind of placement of virtual machines can be reasonably packed in a virtualization platform?" **. I'm calling it that way now, but is there an official name ...?

Background to thinking about this issue

My daily work is to use VMware products to provide virtual machines in an on-premise environment and to operate and maintain the entire platform. The platform is a medium-scale platform of about 700VM. What I think about my business is, "Isn't it possible to free up a little more resources (especially CPU and memory) by devising the layout of virtual machines?"

I think that every infrastructure administrator is the same, but when providing a new virtual machine, isn't it particularly conscious of the following?

--Does the physical machine to be equipped with the virtual machine have spare resources?

This perspective is very important because overloading a single physical machine can affect the performance of the virtual machine. Also, as a platform administrator, when new requirements for creating a virtual machine come out, it is easy because you only have to check the remaining capacity of the current platform. But what about the following perspectives?

--Is the layout so that the resources of physical machines can be effectively used throughout the platform?

Thinking about placement from this perspective can be a daunting task. What is meant by "utilizing" here is whether virtual machines can be packed and placed without strain. Therefore, it is necessary to judge whether it is optimal as a whole, including the virtual machine currently running. Of course, this is not the case when load distribution functions such as DRS are enabled, but in my experience, many legacy companies do not rely on such functions and are managed by humans. And these optimizations as a whole can be considered not only for on-premise environment managers but also for cloud operators by replacing virtual machines with instances, etc., which is a very important element for operation management of huge infrastructure.

Assuming that there are 1000 virtual machines and 100 physical machines, the total number of combinations is 100000 (1000 x 100). Well, this is quite the case with classic computers, but if you become a cloud operator, this number will not be enough ...

Prerequisites for solving a problem

If you suddenly pack too many things, you will get a flat tire, so gradually complicate the problem. This time, we will solve the problem under the following conditions.

--One physical machine --The only resource to be evaluated for deciding the placement is the CPU

I'm sorry to impose various conditions, but this time let me go with this.

Specific problem setting

Consider a state in which a virtual machine with the following required specifications is installed in a physical machine with 6 CPU cores (upper limit).

--VM0: 2 cores --VM1: 4 cores --VM2: 5 cores --VM3: 8 cores

When not considering overcommitment, what is the combination of virtual machines that can make the best use of the performance of physical machines? This time, we will not place virtual machines that exceed the CPU limit (6 cores) of physical machines. In the case of this problem setting, it is recommended that only VM0 and VM2 (2 cores + 4 cores = 6 cores) be placed.

Hamiltonian

This time Hamiltonian is as follows

H = -A\sum _{\alpha \in \rm{VM}}w_{\alpha}x_{\alpha} + B_{1}(W_{limit}-\sum _{\alpha \in \rm{VM}}w_{\alpha}x_{\alpha})^{2}

It is expressed by.

$ x_ {\ alpha} $ indicates whether the $ \ alpha $ th virtual machine is installed in the physical machine (1: installed, 0: not installed). And $ W_ {\ rm {limit}} $ represents the CPU limit of the physical machine (6 cores in this case), and $ w_ {\ alpha} $ represents the CPU resources required by the $ \ alpha $ th virtual machine. I am. Also, $ A $ and $ B_1 $ are parameters that express the weight of each term. If you are familiar with it, this problem is ** [Knapsack problem](https://ja.wikipedia.org/wiki/%E3%83%8A%E3%83%83%E3% Very similar to 83% 97% E3% 82% B5% E3% 83% 83% E3% 82% AF% E5% 95% 8F% E9% A1% 8C) **. However, unlike the normal knapsack problem, the "value" and "volume" are the same (CPU in this case).

Execution environment

Around this time, I wanted to do simple development on the iPad Pro, so I ran it on the Google Colaboratory.

Source code

First, install blueqat. Not required if you are using a local Jupyter notebook and have already installed it.

pip install blueqat

Library import

from blueqat.pauli import qubo_bit as q
from blueqat.vqe import Vqe, QaoaAnsatz
import numpy as np

Virtual machine definition class

class VirtualMachine():
    def __init__(self, number, cost):
        self.__number = number
        self.__cost = cost

    @property
    def cost(self):
        return self.__cost

Hamiltonian construction function

def create_Hamiltonian(CpuLimit, vms, params):
    # first term of Hamiltonian
    h1 = 0.0
    for i in range(len(vms)):
        h1 -= vms[i].cost * q(i)

    # second term of Hamiltonian
    h2 = 0.0
    vmtotalCpu = 0.0
    for j in range(len(vms)):
        vmtotalCpu += vms[j].cost * q(j)

    h2 = (CpuLimit - vmtotalCpu)**2

    return params[0] * h1 + params[1] * h2

Main processing


#Physical Machine CPUlimit
CpuLimit = 6

#create Virtual Machine
vms = []
vms.append(VirtualMachine(number=0, cost=2))
vms.append(VirtualMachine(number=1, cost=4))
vms.append(VirtualMachine(number=2, cost=5))
vms.append(VirtualMachine(number=3, cost=8))

#Hyper Parameter(A=100)
Params =[1.0, 100.0]

#Create Hamiltonian
h = create_Hamiltonian(CpuLimit, vms, Params)
ansatz = QaoaAnsatz(h, 20)
runner = Vqe(ansatz)
result = runner.run()

print("mode:")
print(result.most_common(10))

Execution result

mode:
(((1, 1, 0, 0), 0.7896332746515127), ((1, 0, 1, 0), 0.08187420835800963), ((0, 0, 1, 0), 0.0748323470871601), ((0, 1, 0, 0), 0.04708791984791466), ((0, 0, 0, 1), 0.005451806960418528), ((1, 0, 0, 1), 0.0005107132984061434), ((1, 1, 1, 0), 0.00015463766513252268), ((0, 1, 1, 0), 0.00014643503666132072), ((0, 0, 1, 1), 0.0001069655788835076), ((0, 0, 0, 0), 8.715493427846994e-05))

As for how to read the output, it is ((((solution combination 1, appearance probability of combination 1), (solution combination 2, appearance probability of combination 2), ...)). (The output order is the order with the highest probability of appearance) (1,1,0,0) indicates that VM0 and VM1 are placed, and the probability of appearance is 78%. It was a good result. However, in reality, the probability of occurrence of a solution changes each time it is executed, and the solution that is most likely to appear may also fluctuate. why···? This execution result is executed about 3 or 4 times and the best one is posted. If anyone knows the cause, please let me know.

Next schedule

Next, I would like to optimize when there are multiple physical machines. I wonder if I can do it well

Recommended Posts

I tried to solve the virtual machine placement optimization problem (simple version) with blueqat
I tried to solve the problem with Python Vol.1
I tried to solve a combination optimization problem with Qiskit
I tried to solve the soma cube with python
The 15th offline real-time I tried to solve the problem of how to write with python
How to write offline real time I tried to solve the problem of F02 with Python
I tried to solve the ant book beginner's edition with python
I wanted to solve the ABC164 A ~ D problem with Python
I tried to solve the shift scheduling problem by various methods
I tried to solve TSP with QAOA
I tried to express sadness and joy with the stable marriage problem.
I tried to solve the 2020 version of 100 language processing [Chapter 3: Regular expressions 25-29]
I tried to visualize the model with the low-code machine learning library "PyCaret"
I tried to solve the E qualification problem collection [Chapter 1, 5th question]
I tried to solve the inverted pendulum problem (Cart Pole) by Q-learning.
I tried to save the data with discord
LeetCode I tried to summarize the simple ones
I tried to implement the traveling salesman problem
I tried to solve the 2020 version of 100 language processing knocks [Chapter 1: Preparatory movement 00-04]
I tried to solve the 2020 version of 100 language processing knocks [Chapter 1: Preparatory movement 05-09]
I tried to learn the sin function with chainer
I tried to move machine learning (ObjectDetection) with TouchDesigner
Try to solve the internship assignment problem with Python
I tried to touch the CSV file with Python
I tried to step through Bayesian optimization. (With examples)
I tried to compress the image using machine learning
I tried to solve AOJ's number theory with Python
[Keras] I tried to solve a donut-type region classification problem by machine learning [Study]
I tried to analyze the whole novel "Weathering with You" ☔️
I wanted to solve the Panasonic Programming Contest 2020 with Python
I tried to find the average of the sequence with TensorFlow
I tried to notify the train delay information with LINE Notify
I tried to simulate ad optimization using the bandit algorithm.
[Machine learning] I tried to summarize the theory of Adaboost
I tried to divide the file into folders with Python
I tried to describe the traffic in real time with WebSocket
I tried to automate the watering of the planter with Raspberry Pi
I tried to make Othello AI with tensorflow without understanding the theory of machine learning ~ Introduction ~
I tried machine learning with liblinear
I tried to solve 100 language processing knock 2020 version [Chapter 2: UNIX commands 10 to 14]
Try to solve the N Queens problem with SA of PyQUBO
I tried to process the image in "sketch style" with OpenCV
I tried to display the video playback time (OpenCV: Python version)
I tried to get started with Bitcoin Systre on the weekend
I tried to process the image in "pencil style" with OpenCV
I tried to expand the size of the logical volume with LVM
I tried to solve 100 language processing knock 2020 version [Chapter 2: UNIX commands 15 to 19]
I tried to improve the efficiency of daily work with Python
I tried to solve the first question of the University of Tokyo 2019 math entrance exam with python sympy
I tried "Implementing a genetic algorithm (GA) in python to solve the traveling salesman problem (TSP)"
I tried to move the ball
I tried to estimate the interval.
I tried to make Othello AI with tensorflow without understanding the theory of machine learning ~ Implementation ~
Try to solve the function minimization problem using particle swarm optimization
(Machine learning) I tried to understand the EM algorithm in a mixed Gaussian distribution carefully with implementation.
I want to solve the problem of memory leak when outputting a large number of images with Matplotlib
I tried to make Othello AI with tensorflow without understanding the theory of machine learning ~ Battle Edition ~
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
Solve a simple traveling salesman problem using a Boltzmann machine with simulated annealing
How to write offline real time I tried to solve E11 with python