Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Price Optimization Notebook #88

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

FengShi0705
Copy link

Description of changes:
Adding price optimization notebook.

We prepare to publish an AWS blog post where we used Amazon Braket quantum annealer to solve a price optimization problem. We are adding this notebook to complement the blog post as an asset.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@FengShi0705 FengShi0705 requested a review from a team as a code owner October 25, 2021 15:58
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Copy link
Contributor

@christianbmadsen christianbmadsen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the reference to the blog post in the example notebook please.

Copy link
Contributor

@christianbmadsen christianbmadsen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cell 26 takes a long time to run and submits a substantial amount of tasks against DWave. I recommend that you comment out as default similar to cell 9 here:https://github.com/aws/amazon-braket-examples/blob/main/examples/braket_features/Using_the_tensor_network_simulator_TN1.ipynb

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 3, 2021

import matplotlib.pyplot as plt

We are already importing this, can we remove these imports from every cell again?

@amazon-braket amazon-braket deleted a comment from review-notebook-app bot Dec 3, 2021
@virajvchaudhari
Copy link
Contributor

virajvchaudhari commented Dec 3, 2021

Hi @FengShi0705, Thanks for submitting the notebook. I just have some questions/comments hoping you can address.

  1. Can we handle the required imports in the top most cell and not repeat them in the below cells. You can refer other notebooks for this.
  2. Can you remove the comment in the last cell #b=300, if that's not required?
  3. Can we have a proper spacing after assigning the variable and the working code. You can refer to this notebook to understand how we are aligning the spacing. https://github.com/aws/amazon-braket-examples/blob/main/examples/quantum_annealing/Dwave_Factoring/Dwave_factoring.ipynb
  4. As Christian pointed out the cell 26 takes a long time to run, may be we can comment out that cell and add a note that will allow users to understand that it will take long time to run and whether they want to execute it. You can refer this notebook: https://github.com/aws/amazon-braket-examples/blob/main/examples/braket_features/Using_the_tensor_network_simulator_TN1.ipynb
  5. Can we also change some variable names instead of a b, that would give more context about the variables?

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 4, 2021

View / edit / reply to this conversation on ReviewNB

licedric commented on 2021-12-04T01:00:36Z
----------------------------------------------------------------

Optimization or optimisation? Settle on a spelling. I suggest "optimization" since that's what we use in our other notebooks.

"We showcase how to formulate this problem as a quadratic unconstrained binary optimization problem (QUBO) and use D-Wave Systems Inc. Advantage quantum annealer on Amazon Braket to find close-to-optimal solutions."

->

"We show how to formulate this problem as a quadratic unconstrained binary optimization problem (QUBO) and use the D-Wave Advantage quantum annealer on Amazon Braket to find close-to-optimal solutions."


FengShi0705 commented on 2021-12-21T21:05:36Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 4, 2021

View / edit / reply to this conversation on ReviewNB

licedric commented on 2021-12-04T01:00:37Z
----------------------------------------------------------------

"Table of content" -> "Table of contents"


FengShi0705 commented on 2021-12-21T21:05:47Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 4, 2021

View / edit / reply to this conversation on ReviewNB

licedric commented on 2021-12-04T01:00:38Z
----------------------------------------------------------------

Incomplete sentence


FengShi0705 commented on 2021-12-21T21:06:12Z
----------------------------------------------------------------

Done, complete it now

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 4, 2021

View / edit / reply to this conversation on ReviewNB

licedric commented on 2021-12-04T01:00:38Z
----------------------------------------------------------------

Maximizing the revenue subject to what? What can we change? I suggest rewriting the problem as something like

max_{p} R, R = sum d_t(p_t) p_t

so it's clear that we're optimizing with respect to p.

soly -> solely. Also add period at the end of the sentence.


FengShi0705 commented on 2021-12-21T21:06:57Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 4, 2021

View / edit / reply to this conversation on ReviewNB

licedric commented on 2021-12-04T01:00:39Z
----------------------------------------------------------------

"b is the constant" -> "b is a constant"


FengShi0705 commented on 2021-12-21T21:07:13Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 4, 2021

View / edit / reply to this conversation on ReviewNB

licedric commented on 2021-12-04T01:00:40Z
----------------------------------------------------------------

Still too much output. Just show a subset. Ditto for the rest of the notebook


FengShi0705 commented on 2021-12-21T21:07:38Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 4, 2021

View / edit / reply to this conversation on ReviewNB

licedric commented on 2021-12-04T01:00:40Z
----------------------------------------------------------------

Line #4.    price_levels=[5, 8, 10, 12, 13, 16, 19] # the option of price at each day

Can you clarify this comment? Something like "possible prices for each day"


FengShi0705 commented on 2021-12-21T21:08:01Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 4, 2021

View / edit / reply to this conversation on ReviewNB

licedric commented on 2021-12-04T01:00:41Z
----------------------------------------------------------------

Line #45.    print("dataset x:")

This is huge. Just print out a few entries of x and y.


FengShi0705 commented on 2021-12-21T21:08:29Z
----------------------------------------------------------------

yes, done, reduced the size of output printing

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 4, 2021

View / edit / reply to this conversation on ReviewNB

licedric commented on 2021-12-04T01:00:42Z
----------------------------------------------------------------

"By using the created training dataset, we simply using sklearn to fit a linear demand model." ->

"We use sklearn to fit a linear demand model to the training set."

"round the model coefficients to integer" -> "round the model coefficients to integers"

But where does this rounding take place?


FengShi0705 commented on 2021-12-21T21:10:30Z
----------------------------------------------------------------

Done.

Good point, the rounding is a legacy procedure for previous version. There is no need for rounding to take place, so the wording around "rounding" is now removed .

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 4, 2021

View / edit / reply to this conversation on ReviewNB

licedric commented on 2021-12-04T01:00:42Z
----------------------------------------------------------------

"Where" -> "where"


FengShi0705 commented on 2021-12-21T21:10:44Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 4, 2021

View / edit / reply to this conversation on ReviewNB

licedric commented on 2021-12-04T01:00:43Z
----------------------------------------------------------------

Maximize shouldn't be in math font.

"most recent n day's" -> "most recent n days'"

"random choose" -> "randomly choose"


FengShi0705 commented on 2021-12-21T21:11:15Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 4, 2021

View / edit / reply to this conversation on ReviewNB

licedric commented on 2021-12-04T01:00:44Z
----------------------------------------------------------------

Where do we assume that the price each day is only drawn from a set of fixed m options? This should be called out earlier explicitly. Also add a reference if this model is taken from the literature.

"the objective function which is the total revenue R in the next n days" -> "the objective function, i.e. the total revenue R, for the next n days"

"subject to" should not be in math font


FengShi0705 commented on 2021-12-21T21:13:38Z
----------------------------------------------------------------

Done.

Good point, A sentence is now added to explain the discrete price choices. According to certain business requirements, the possible prices are usually defined to be a set of discrete price values (e.g. $5.99, $9.99, $14.99).

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 4, 2021

View / edit / reply to this conversation on ReviewNB

licedric commented on 2021-12-04T01:00:44Z
----------------------------------------------------------------

Explain why you're using a 1d list to represent the 2d matrix x.


FengShi0705 commented on 2021-12-21T21:14:18Z
----------------------------------------------------------------

Done.

Now added a comment in the code: # here, we use a list to represent the sequence of prices from day T-n to day T+n-1.

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 4, 2021

View / edit / reply to this conversation on ReviewNB

licedric commented on 2021-12-04T01:00:45Z
----------------------------------------------------------------

"price to represent revenue" -> "the price to represent the revenue"


FengShi0705 commented on 2021-12-21T21:15:04Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 4, 2021

View / edit / reply to this conversation on ReviewNB

licedric commented on 2021-12-04T01:00:46Z
----------------------------------------------------------------

Line #10.    def get_demands_rev(a,b,hist_p, p):

What does this function do? Add it as a comment.


FengShi0705 commented on 2021-12-21T21:15:49Z
----------------------------------------------------------------

Done. comment added

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 4, 2021

View / edit / reply to this conversation on ReviewNB

licedric commented on 2021-12-04T01:00:46Z
----------------------------------------------------------------

Line #16.            d_i = get_demand(

This code is not Pythonic. Try something like

d = [get_demand(coeff=a, b=b, prices=all_p[i+1:i+1+t]) for i in range(t)]

rev = np.dot(d, p)


FengShi0705 commented on 2021-12-21T21:16:24Z
----------------------------------------------------------------

Done as suggested

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 4, 2021

View / edit / reply to this conversation on ReviewNB

licedric commented on 2021-12-04T01:00:47Z
----------------------------------------------------------------

The uncertainty of the predicted demand d_t can be represented by its estimated variance." Use comma at the end

"Where beta" -> "where beta"

"regularized parameters" -> "regularization parameter"

"esitmation" -> "estimation"

Where does the formula for the estimated variance come from?


FengShi0705 commented on 2021-12-21T21:17:03Z
----------------------------------------------------------------

Done. and a reference to the formula is added

@review-notebook-app
Copy link

review-notebook-app bot commented Dec 4, 2021

View / edit / reply to this conversation on ReviewNB

licedric commented on 2021-12-04T01:00:48Z
----------------------------------------------------------------

"As we mentioned above that price can only take one option per day, this means that one and only one of the binary variables in a day must be 1

and others must be 0."

->

"Since the price can only take one value per day, exactly one of the binary variables in a day must be 1, and the others must be 0."

"large enough coefficients" -> "large enough coefficient"


FengShi0705 commented on 2021-12-21T21:17:33Z
----------------------------------------------------------------

Done

Copy link
Author

Done.

Good point, the rounding is a legacy procedure for previous version. There is no need for rounding to take place, so the wording around "rounding" is now removed .


View entire conversation on ReviewNB

Copy link
Author

Done


View entire conversation on ReviewNB

1 similar comment
Copy link
Author

Done


View entire conversation on ReviewNB

Copy link
Author

Done.

Good point, A sentence is now added to explain the discrete price choices. According to certain business requirements, the possible prices are usually defined to be a set of discrete price values (e.g. $5.99, $9.99, $14.99).


View entire conversation on ReviewNB

Copy link
Author

Done.

Now added comments in the code: # here, we use a list to represent the sequence of prices from day T-n to day T+n-1.


View entire conversation on ReviewNB

Copy link
Author

Done


View entire conversation on ReviewNB

Copy link
Author

Done. comment added


View entire conversation on ReviewNB

Copy link
Author

Done as suggested


View entire conversation on ReviewNB

Copy link
Author

Done. and a reference to the formula is added


View entire conversation on ReviewNB

Copy link
Author

Done


View entire conversation on ReviewNB

Copy link
Author

Perfect suggestion. Done


View entire conversation on ReviewNB

Copy link
Author

Done. reduced the printing size. The rows and columns are now explained in the text. Each row represents an optimized solution. The columns starting with X_ shows the value of the binary decision variables $x_{t,k}$ and the energy column shows the negative value of objective, i.e. −𝐻


View entire conversation on ReviewNB

Copy link
Author

Done


View entire conversation on ReviewNB

2 similar comments
Copy link
Author

Done


View entire conversation on ReviewNB

Copy link
Author

Done


View entire conversation on ReviewNB

Copy link
Author

Really great point. We missed the covariance in this formula.

Now we made following changes:

  1. we added covariance in this formula
  2. in the code, we now added a function to calculate the covariance

View entire conversation on ReviewNB

Copy link
Author

Done, also added covariance calculation in this function now


View entire conversation on ReviewNB

Copy link
Author

Done. Added explanation sentence This small but non-zero value is due to floating point precision.


View entire conversation on ReviewNB

Copy link
Author

Done


View entire conversation on ReviewNB

1 similar comment
Copy link
Author

Done


View entire conversation on ReviewNB

Copy link
Author

Done, added wikipedian link


View entire conversation on ReviewNB

Copy link

@JordanAWS JordanAWS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for taking the time to create this demo notebook for Braket! Fantastic description of the use case. I've added a number of edit requests below, mostly style things, and also some code updates now that we have the option to do default s3 buckets.

"outputs": [
{
"name": "stdout",
"output_type": "stream",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clear the output of this cell, since it's not informative for a reader.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that people can run this notebook in their local environment, let's add the ocean sdk and plugins to the pip install:
pip install sklearn dwave-ocean-sdk amazon-braket-ocean-plugin

"source": [
"# Using quantum annealing on Amazon Braket for price optimization\n",
"\n",
"Combinatorial Optimization is one of the most important fields in optimization. Practical applications can be found in virtually every industry. Prominent examples include supply chain optimization in transport and logistics, portfolio management in finance, and the optimization of clinical trials in healthcare, among many others. It is also one of the most active research topics in operation research and computer science. However, many practical combinatorial optimization problems are NP-hard and require massive computation costs to find solution of good quality. \n",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few suggestions to make the start of the paragraph flow more smoothly, moving up a sentence from later in the paragraph:
"Combinatorial Optimization is an active topic of research at the forefront of computer science and operations research, with practical applications across virtually every industry."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an informational link for NP-hard in case a reader isn't familiar with the term. e.g. https://en.wikipedia.org/wiki/NP-hardness

"execution_count": 8,
"id": "a668a23a",
"metadata": {},
"outputs": [

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The format of this all_p output isn't very informative to the reader. Recommend changing the display format to eliminate the Binary and Num prefixes and separating out p_data and p into separate cells, describing what the expressions mean.

{
"data": {
"text/plain": [
"'(((Binary(X_048)*Num(19.000000)+Binary(X_047)*Num(16.000000)+Binary(X_046)*Num(13.000000)+Binary(X...m(12.000000)+Binary(X_009)*Num(10.000000)+Binary(X_007)*Num(5.000000)+Binary(X_008)*Num(8.000000)))'"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, this display format is hard on the eyes. Same suggestion as above.

{
"data": {
"text/plain": [
"'((((Binary(X_048)*Num(19.000000)+Binary(X_047)*Num(16.000000)+Binary(X_046)*Num(13.000000)+Binary(...X_001)*Num(8.000000))*Num(-0.000535)+Num(0.016381))+Num(1.000000))*Num(100.000000))*Num(-1.000000))'"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Display format.

"output_type": "execute_result"
}
],
"source": [

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May want to say here that this cell takes a few minutes to run.

]
}
],
"source": [

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whitespace seems to have gotten messed up somehow. Please paste in this, which should work instantly with the standard Command/CTRL -> / for commenting/uncommenting multi lines.

# beta_options=[1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9]
# results = {beta_option:[] for beta_option in beta_options}
# beta_samples = beta_options*6

# for beta_i in beta_samples:
#     max_revenue, prediction_variance, energy, opt_demand, opt_prices, rev_std = optimize(
#        a = a,
#        b = b,
#        data_x = data_x,
#        selected_hist_prices = p_data,
#        price_levels = price_levels,
#        Lp = Lp,
#        Ld = Ld,
#        sigma = sigma ,
#        beta=beta_i,
#        vol_bound=None,
#    )
#     print('---')
#     print(f"beta_sample:{beta_i}")
#     print(f"max_revenue:{max_revenue}")
#     print(f"prediction_variance:{prediction_variance}")
#     print(f"energy:{energy}")
#     print(f"opt_demand:{opt_demand}")
#     print(f"opt_prices:{opt_prices}")
#     print(f"rev_std: {rev_std}")
#     print(f"penalty_term: {max_revenue-beta_i*prediction_variance+energy}")
#     results[beta_i].append([energy, max_revenue, prediction_variance, rev_std])

:return: variance
"""
n_samples, t = data_x.shape
ones = np.ones((n_samples, 1), dtype=np.float)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to builtin float

:return: variance
"""
n_samples, t = data_x.shape
ones = np.ones((n_samples, 1), dtype=np.float)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to built-in float

" :return: variance\n",
" \"\"\"\n",
" n_samples, t = data_x.shape\n",
" ones = np.ones((n_samples, 1), dtype=np.float)\n",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to built-in float

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants