<jupyter_start><jupyter_text>Evaluate the obtained policy======================We have already seen how to evaluate the obtained policy in the previous section.In this section, we will dig into more details about evaluation. Take the stage-wise independent continuous problem we have introduced. We set the number of stages T=4 to intentionally make the problem a bit more complex. We choose optimality gap less than 1e-3 as our stopping criterion and turn off simulation to obtain the exact gap. As shown below, after ten iterations, evaluation of the obtained policy shows the optimality gap of 0.22%; after twenty iterations, the optimality gap turns to 0.00%, which is below the tolerance we set, so the algorithm stops. In the end, we obtain that the optimal value is 6.68 and a first stage solution is 9.08.<jupyter_code>from msppy.msp import MSLP
import numpy as np
from msppy.solver import SDDP
from msppy.evaluation import Evaluation, EvaluationTrue
nvic = MSLP(T=3, sense=-1, bound=100)
def f(random_state):
    return random_state.lognormal(mean=np.log(4),sigma=2)
for t in range(3):
    m = nvic[t]
    buy_now, buy_past = m.addStateVar(name='bought', obj=-1.0)
    if t != 0:
        sold = m.addVar(name='sold', obj=2)
        unsatisfied = m.addVar(name='unsatisfied')
        recycled = m.addVar(name='recycled', obj=0.5)
        m.addConstr(sold + unsatisfied == 0, uncertainty={'rhs':f})
        m.addConstr(sold + recycled == buy_past)
nvic.discretize(random_state=1, n_samples=100)
nvic_sddp = SDDP(nvic)
nvic_sddp.solve(max_iterations=30, freq_evaluations=10, n_simulations=-1, tol=1e-3)
nvic_sddp.db[-1]
nvic_sddp.first_stage_solution<jupyter_output>Academic license - for non-commercial use only
Academic license - for non-commercial use only
Academic license - for non-commercial use only<jupyter_text>Evaluate the policy on the true problem<jupyter_code>res_true = EvaluationTrue(nvic)
res_true.run(n_simulations=3000, percentile=95, 
    query=['sold','bought','unsatisfied','recycled'], query_stage_cost=True)
res_true.CI
res_true.stage_cost
res_true.solution['sold']
res_true.solution['bought']
res_true.solution['unsatisfied']
res_true.solution['recycled']<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10519187358916479, 'fraction_numerical': 0.02167042889390519, 'mean_word_length': 5.367816091954023, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '635990', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 761, 'n_tokens_neox': 700, 'n_words': 223, 'path': 'doc/source/evaluate.ipynb', 'repo_name': 'jucaleb4/msppy', 'size': '2215'}