{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Visualização das soluções do modelo de Vale do Sousa (90x1 anos)\n", "# Versão 08-09-2020" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# INPUT\n", "solutionFile = 'Paiva3lim3.sol'\n", "sufixo = 'paiva'\n", "info = 'Vale do Sousa (90 x 1ano)'\n", "pSaveFig = 1 # -1/0/1 - gravação das figuras no disco" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Ficheiro com vertices\n", "\n", "file = open('ValeSousaVert.txt')\n", "nlin = -1\n", "Units = {}\n", "xmin = 100000000000\n", "xmax = -100000000000\n", "ymin = 100000000000\n", "ymax = -100000000000\n", "\n", "\n", "NamUx = '000'\n", "LVert = []\n", "\n", "for line in file: \n", " nlin += 1\n", " if nlin == 0:\n", " continue\n", " lex = line.split() \n", " if nlin < 5 or nlin > 90475: \n", " print(lex) \n", " if NamUx == lex[0]:\n", " x = float(lex[2])\n", " y = float(lex[3]) \n", " LVert.append((x,y))\n", " if xmin > x : xmin = x \n", " if xmax < x : xmax = x \n", " if ymin > y : ymin = y \n", " if ymax < y : ymax = y \n", " elif line[:3] == '###':\n", " Units[NamUx] = LVert\n", " break\n", " else:\n", " if NamUx != '000':\n", " Units[NamUx] = LVert\n", " NamUx = lex[0]\n", " LVert = []\n", " x = float(lex[2])\n", " y = float(lex[3]) \n", " LVert.append((x,y))\n", " if xmin > x : xmin = x \n", " if xmax < x : xmax = x \n", " if ymin > y : ymin = y \n", " if ymax < y : ymax = y \n", "file.close()\n", "\n", "\n", "print('Numero das linhas no ficheiro: nlin=',nlin) \n", "print('xmin=', xmin, 'xmax=', xmax)\n", "print('ymin=', ymin, 'ymax=', ymax)\n", "print(len(Units))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Áreas das unidades de gestão (m2)\n", "\n", "file = open('UnitAreasVSousa.txt')\n", "nlin = -1\n", "Areas = {}\n", "\n", "for line in file: \n", " nlin += 1\n", " if nlin == 0:\n", " continue\n", " lex = line.split() \n", " if nlin < 5 or nlin > 90475: \n", " print(lex)\n", " Ar = float(lex[1])\n", " Areas[lex[0]] = Ar \n", " \n", "file.close()\n", "print(nlin, len(Areas))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Concelhos das unidades\n", "\n", "file = open('ug_concelhos.csv')\n", "\n", "nlin = -1\n", "Concel4Unid = {}\n", "UnPaivaNorte = []\n", "UnPaivaSul = []\n", "UnPenafiel = []\n", "UnParedes = []\n", "\n", "for line in file: \n", " nlin += 1\n", " if nlin == 0:\n", " continue\n", " lex = line.split(',')\n", " \n", " if nlin < 5 or nlin > 1310:\n", " print(lex)\n", " \n", " Concel4Unid[lex[0]] = lex[1]\n", " \n", " if lex[1] == \"PaivaNorte\":\n", " UnPaivaNorte.append(lex[0])\n", " elif lex[1] == \"PaivaSul\":\n", " UnPaivaSul.append(lex[0])\n", " elif lex[1] == \"Penafiel\":\n", " UnPenafiel.append(lex[0])\n", " elif lex[1] == \"Paredes\":\n", " UnParedes.append(lex[0])\n", " else:\n", " print('Unknown: ', line)\n", " \n", "file.close()\n", "print(nlin, len(Concel4Unid))\n", "#print(UnPaivaNorte)\n", "#print(UnPaivaSul)\n", "#print(UnParedes)\n", "#print(UnPenafiel)\n", "#print(Concel4Unid)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#%matplotlib notebook \n", "import matplotlib.pyplot as plt\n", "\n", "Cores = {'PaivaNorte':'fuchsia','PaivaSul':'salmon', 'Penafiel':'lime', 'Paredes':'royalblue'}\n", "\n", "fig = plt.figure(figsize=(10,10))\n", "ax = plt.gca()\n", "ax.set_facecolor('lightgray')\n", "\n", "K = sorted(list(Units.keys()))\n", "\n", "for key in K: \n", " xx = []\n", " yy = []\n", " for v in Units[key]:\n", " xx.append(v[0])\n", " yy.append(v[1])\n", " if int(key) >= 4000:\n", " clr = 'yellow' \n", " else:\n", " clr = Cores[Concel4Unid[key]]\n", " plt.fill(xx, yy, facecolor=clr, edgecolor='black', linewidth=1)\n", "\n", "#ax.xlim=(-25000, -15000)\n", "#ax.ylim=(150000, 1800000) \n", "\n", "plt.plot([-25000, -25010], [165000, 165010], lw=15, c=Cores['Paredes'], label='Paredes')\n", "plt.plot([-25000, -25010], [165000, 165010], lw=15, c=Cores['Penafiel'], label='Penafiel')\n", "plt.plot([-25000, -25010], [165000, 165010], lw=15, c=Cores['PaivaNorte'],label='PaivaNorte')\n", "plt.plot([-25000, -25010], [165000, 165010], lw=15, c=Cores['PaivaSul'], label='PaivaSul')\n", "plt.plot([-25000, -25010], [165000, 165010], lw=17, c='lightgray')\n", "plt.legend()\n", "plt.tight_layout()\n", " \n", "#plt.savefig('VSousaBlocos.svg',dpi=100)\n", "plt.show(plt)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Ficheiro das Prescrições - VS3\n", "\n", "file = open('PrescVS_1.txt')\n", "nlin = -1\n", "\n", "TabPreVS = {}\n", "\n", "Parx = 'xxx'\n", "Prex = 'xxx'\n", "\n", "for line in file: \n", " nlin += 1\n", " if nlin == 0:\n", " print(line)\n", " continue \n", " lex = line.split() \n", " if nlin < 5 or nlin > 143940: \n", " print(nlin,'lex=',lex)\n", " \n", " if lex[0] != Parx:\n", " Parx = lex[0]\n", " TabPreVS[Parx] = {}\n", " Prex = 'xxx'\n", " if lex[1] != Prex:\n", " Prex = lex[1]\n", " TabPreVS[Parx][Prex] = []\n", " for i in range(2,6):\n", " lex[i] = float(lex[i])\n", " lex[6] = int(lex[6]) \n", " TabPreVS[Parx][Prex].append(lex[2:])\n", " \n", "file.close()\n", "print('Número das linhas no ficheiro: nlin=',nlin) \n", "Un = sorted(list(TabPreVS.keys()))\n", "#print(Un)\n", "print('Número das Unidades na Tabela=', len(Un))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "Sol = []\n", "\n", "def sol_input(fname):\n", " \n", " print('File=', fname)\n", " file = open(fname)\n", "\n", " nlin = 0\n", " pr = 0\n", " for line in file: \n", " nlin += 1\n", " if pr==0 and line[:13]==\"Variable Name\":\n", " pr = 1\n", " continue\n", " if pr==1 and line[:19]==\"All other variables\": \n", " pr = 2\n", " break\n", " if pr==0:\n", " continue\n", " if line[:5] != 'Presc':\n", " continue\n", " if line[:8] == 'Presc800':\n", " continue \n", " lex = line.split()\n", " lexp = lex[0].split('_')\n", " \n", " if int(lexp[1][2:]) < 4000:\n", " buf = [lexp[1][2:], lexp[0][5:], lexp[2], lexp[3], lex[0], nlin] \n", " Sol.append(buf)\n", " file.close() \n", " print('Número das linhas no ficheiro: nlin=',nlin) \n", " print('Número das variáveias não nulas=', len(Sol)) " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Solution input\n", "Sol = []\n", "\n", "sol_input(solutionFile)\n", " \n", "Sol[:5], Sol[-5:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Solução + Prescrições = Madeira\n", "import numpy as np\n", "\n", "MAD = []\n", "for k in range(90): # para 90 periodos de 1 anos\n", " Ctt = ['Ctt',0,'X'] # V_thin, Parcelas... \n", " Cth = ['Cth',0,'X'] # V_harv, Parcelas... \n", " Ect = ['Ect',0,'X'] # V_thin, Parcelas...\n", " Ech = ['Ech',0,'X'] # V_harv, Parcelas... \n", " Pbt = ['Pbt',0,'X'] # V_thin, Parcelas... \n", " Pbh = ['Pbh',0,'X'] # V_harv, Parcelas... \n", " Qrt = ['Qrt',0,'X'] # V_thin, Parcelas...\n", " Qrh = ['Qrh',0,'X'] # V_harv, Parcelas... \n", " Sbt = ['Sbt',0,'X'] # V_thin, Parcelas... \n", " Sbx = ['Sbx',0,'X'] # V_thin+cork, Parcelas... \n", " Sbc = ['Sbc',0,'X'] # V_cork, Parcelas... \n", " period = [Ctt, Cth, Ect, Ech, Pbt, Pbh, Qrt, Qrh, Sbt, Sbx, Sbc]\n", " MAD.append(period)\n", "\n", "eps = 0.000001 \n", "for lin in Sol:\n", " ##print(lin)\n", " \n", " try: \n", " Q = TabPreVS[lin[0]][lin[1]] # Parc Pres\n", " except KeyError:\n", " print('KeyError: ', 'Pa=', lin[0], 'Presc=', lin[1], 'var=', lin[4])\n", " continue\n", " \n", " for st in Q:\n", " ##print('st:', st, st[5],st[4])\n", " if st[5] == 'Ct':\n", " if st[0] > eps and st[1] < eps: \n", " MAD[st[4]-1][0].append(lin[0])\n", " MAD[st[4]-1][0][1] += st[0] \n", " elif st[0] < eps and st[1] > eps: \n", " MAD[st[4]-1][1].append(lin[0])\n", " MAD[st[4]-1][1][1] += st[1] \n", " elif st[0] > eps and st[1] > eps: \n", " MAD[st[4]-1][1].append(lin[0])\n", " MAD[st[4]-1][0][1] += st[0]\n", " MAD[st[4]-1][1][1] += st[1]\n", " else:\n", " print('Aviso 11', st, lin[0], lin[1])\n", " elif st[5] == 'Ec':\n", " if st[0] > eps and st[1] < eps:\n", " MAD[st[4]-1][2].append(lin[0])\n", " MAD[st[4]-1][2][1] += st[0] \n", " elif st[0] < eps and st[1] > eps:\n", " MAD[st[4]-1][3].append(lin[0])\n", " MAD[st[4]-1][3][1] += st[1] \n", " elif st[0] < eps and st[1] < eps:\n", " pass \n", " else:\n", " print('Aviso 12', st, lin[0], lin[1]) \n", " elif st[5] == 'Pb': \n", " if st[0] > eps and st[1] < eps:\n", " MAD[st[4]-1][4].append(lin[0])\n", " MAD[st[4]-1][4][1] += st[0] \n", " elif st[0] < eps and st[1] > eps:\n", " MAD[st[4]-1][5].append(lin[0])\n", " MAD[st[4]-1][5][1] += st[1] \n", " elif st[0] > eps and st[1] > eps:\n", " MAD[st[4]-1][5].append(lin[0])\n", " MAD[st[4]-1][4][1] += st[0] \n", " MAD[st[4]-1][5][1] += st[1] \n", " else:\n", " print('Aviso 13', st, lin[0], lin[1])\n", " elif st[5] == 'Qr':\n", " if st[0] > eps and st[1] < eps:\n", " MAD[st[4]-1][6].append(lin[0])\n", " MAD[st[4]-1][6][1] += st[0] \n", " elif st[0] < eps and st[1] > eps:\n", " MAD[st[4]-1][7].append(lin[0])\n", " MAD[st[4]-1][7][1] += st[1] \n", " else: \n", " print('Aviso 14', st, lin[0], lin[1])\n", " elif st[5] == 'Sb':\n", " if st[0] > eps and st[3] < eps: \n", " MAD[st[4]-1][8].append(lin[0])\n", " MAD[st[4]-1][8][1] += st[0] \n", " elif st[0] < eps and st[3] > eps: \n", " MAD[st[4]-1][10].append(lin[0])\n", " MAD[st[4]-1][10][1] += st[3] \n", " elif st[0] > eps and st[3] > eps: \n", " MAD[st[4]-1][9].append(lin[0])\n", " MAD[st[4]-1][8][1] += st[0] \n", " MAD[st[4]-1][10][1] += st[3] \n", " else: \n", " print('Aviso 15', st, lin[0], lin[1])\n", " else:\n", " print('Aviso 10: ', st[5], st, lin[0], lin[1])\n", " break \n", "print('Ready')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "Clrs = ['gold', 'darkgoldenrod',\n", " 'lightcoral', 'red',\n", " 'lightgreen', 'green',\n", " 'cornflowerblue', 'blue',\n", " 'magenta', 'purple', 'indigo']\n", "\n", "K = sorted(list(Units.keys()))\n", "\n", "for period in range(0,20): # [2, 6, 20, 29]: \n", " \n", " if pSaveFig < 0:\n", " break\n", "\n", " fig = plt.figure(figsize=(10,10))\n", " ax = plt.gca()\n", " ax.set_facecolor('lightgray')\n", " \n", " plt.plot([-25000, -25001], [165000, 165001], lw=15, c=Clrs[0], label='Ct-thin')\n", " plt.plot([-25000, -25001], [165000, 165001], lw=15, c=Clrs[1], label='Ct-harv') \n", " plt.plot([-25000, -25001], [165000, 165001], lw=15, c=Clrs[3], label='Ec-harv')\n", " plt.plot([-25000, -25001], [165000, 165001], lw=15, c=Clrs[4], label='Pb-thin')\n", " plt.plot([-25000, -25001], [165000, 165001], lw=15, c=Clrs[5], label='Pb-harv')\n", " plt.plot([-25000, -25001], [165000, 165001], lw=15, c=Clrs[6], label='Qr-thin')\n", " plt.plot([-25000, -25001], [165000, 165001], lw=15, c=Clrs[7], label='Qr-harv')\n", " plt.plot([-25000, -25001], [165000, 165001], lw=15, c=Clrs[8], label='Sb-thin')\n", " plt.plot([-25000, -25001], [165000, 165001], lw=15, c=Clrs[9], label='Sb-thco')\n", " plt.plot([-25000, -25001], [165000, 165001], lw=15, c=Clrs[10], label='Sb-cork')\n", " plt.plot([-25000, -25001], [165000, 165001], lw=17, c='lightgray')\n", " plt.legend()\n", " \n", " for key in K:\n", " xx = []\n", " yy = []\n", " for v in Units[key]:\n", " xx.append(v[0])\n", " yy.append(v[1])\n", " ax.fill(xx, yy, facecolor='white', edgecolor='black', linewidth=1)\n", "\n", " MADp = MAD[period]\n", " nclr = 0\n", " for esp in MADp: \n", " for key in esp[3:] :\n", " xx = []\n", " yy = []\n", " for v in Units[key]:\n", " xx.append(v[0])\n", " yy.append(v[1])\n", " ax.fill(xx, yy, facecolor=Clrs[nclr], edgecolor='black', linewidth=1) # thin\n", " nclr += 1\n", " \n", " \n", " plt.text(-26000, 143000, info, fontsize=12)\n", " st = str(period+1)\n", " buf = 'Period = '+st\n", " plt.text(-26000, 166000, buf, fontsize=12) \n", " print(buf)\n", " \n", " #ax.xlim=((-20000, -15000))\n", " #ax.ylim=((150000, 170000))\n", " \n", " plt.tight_layout()\n", " if pSaveFig == 1:\n", " plt.savefig('VSousa-'+sufixo+'-'+st+'.png', dpi=100)\n", " plt.show(plt)\n", "print('Ready')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "#####################################################################################\n", "#####################################################################################" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Produção de Madeira\n", "\n", "\"\"\"\n", "MAD = []\n", "for k in range(9): # para 9 periodos de 10 anos\n", " Ctt = ['Ctt',0,'X'] # V_thin, Parcelas... 0\n", " Cth = ['Cth',0,'X'] # V_harv, Parcelas... 1 \n", " Ect = ['Ect',0,'X'] # V_thin, Parcelas... 2\n", " Ech = ['Ech',0,'X'] # V_harv, Parcelas... 3\n", " Pbt = ['Pbt',0,'X'] # V_thin, Parcelas... 4\n", " Pbh = ['Pbh',0,'X'] # V_harv, Parcelas... 5 \n", " Qrt = ['Qrt',0,'X'] # V_thin, Parcelas... 6\n", " Qrh = ['Qrh',0,'X'] # V_harv, Parcelas... 7\n", " Sbt = ['Sbt',0,'X'] # V_thin, Parcelas... 8\n", " Sbx = ['Sbx',0,'X'] # V_thin+cork, Parcelas... 9 \n", " Sbc = ['Sbc',0,'X'] # V_cork, Parcelas... 10 \n", " period = [Ctt, Cth, Ect, Ech, Pbt, Pbh, Qrt, Qrh, Sbt, Sbx, Sbc]\n", " MAD.append(period)\n", "\"\"\"\n", "#print(MAD)\n", "\n", "ProdMAD = {'Ctt':[],'Cth':[],'CtT':[], 'EcT':[], 'Pbt':[], 'Pbh':[], 'PbT':[], 'Qrt':[],\n", " 'Qrh':[], 'QrT':[], 'Sbt':[], 'Sbh':[], 'Sbc':[], 'Tot':[]}\n", "\n", "for k in range(90): \n", " ProdMAD['Ctt'].append(MAD[k][0][1])\n", " ProdMAD['Cth'].append(MAD[k][1][1])\n", " ProdMAD['CtT'].append(MAD[k][0][1] + MAD[k][1][1])\n", " s = MAD[k][0][1] + MAD[k][1][1]\n", " ProdMAD['EcT'].append(MAD[k][3][1])\n", " s += MAD[k][3][1] \n", " ProdMAD['Pbt'].append(MAD[k][4][1])\n", " ProdMAD['Pbh'].append(MAD[k][5][1])\n", " ProdMAD['PbT'].append(MAD[k][4][1] + MAD[k][5][1])\n", " s += MAD[k][4][1] + MAD[k][5][1] \n", " ProdMAD['Qrt'].append(MAD[k][6][1])\n", " ProdMAD['Qrh'].append(MAD[k][7][1])\n", " ProdMAD['QrT'].append(MAD[k][6][1] + MAD[k][7][1]) \n", " s += MAD[k][6][1] + MAD[k][7][1]\n", " ProdMAD['Sbt'].append(MAD[k][8][1])\n", " s += MAD[k][8][1]\n", " ProdMAD['Sbc'].append(MAD[k][10][1]) \n", " ProdMAD['Tot'].append(s)\n", "#print(ProdMAD)\n", "\n", "def tabprint(str):\n", " print('\\n{0:s}:'.format(str), end='')\n", " for k in range(90):\n", " if (k+1)%10==0:\n", " fim = '\\n '\n", " else:\n", " fim = ''\n", " print(' {0:10.2f}'.format(ProdMAD[str][k]),end=fim)\n", " \n", "print(info)\n", "print('Produção de Madeira por períodos\\n ',end='')\n", "for v in range(1,11): print(' {0:10d}'.format(v), end='') \n", "\n", "tabprint('Tot')\n", "tabprint('CtT')\n", "tabprint('EcT')\n", "tabprint('PbT')\n", "tabprint('QrT')\n", "tabprint('Sbt')\n", "tabprint('Sbc') \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "t = range(1,91) \n", "#digsize=(10,6))\n", "plt.plot(t, ProdMAD['Tot'], '-o', c='k', lw=4, label = 'Total')\n", "plt.plot(t, ProdMAD['CtT'], '-o', c=Clrs[1], label = 'Ct')\n", "plt.plot(t, ProdMAD['EcT'], '-o', c=Clrs[3], label = 'Ec')\n", "plt.plot(t, ProdMAD['PbT'], '-o', c=Clrs[5], label = 'Pb')\n", "plt.plot(t, ProdMAD['QrT'], '-o', c=Clrs[7], label = 'Qr')\n", "plt.plot(t, ProdMAD['Sbt'], '-o', c=Clrs[10], label = 'Sb')\n", "plt.xlabel('Period')\n", "plt.ylabel('Wood')\n", "plt.grid() \n", "plt.legend()\n", "if pSaveFig == 1:\n", " plt.savefig('VSousa-Mad-'+sufixo+'.png', dpi=100)\n", "plt.show()\n", "#-------------------------\n", "\n", "fig6 = plt.figure(figsize=(10,6))\n", "plt.stackplot(t, ProdMAD['CtT'], ProdMAD['EcT'], ProdMAD['PbT'], ProdMAD['QrT'], ProdMAD['Sbt'],\n", " labels = ['Ct', 'Ec', 'Pb', 'Qr', 'Sb'],\n", " colors= [Clrs[1],Clrs[3],Clrs[5],Clrs[7],Clrs[10]] )\n", "plt.grid() \n", "plt.legend(loc='upper left')\n", "plt.xlabel('Period')\n", "plt.ylabel('Wood')\n", "if pSaveFig == 1:\n", " plt.savefig('VSousa-Mad+'+sufixo+'.png', dpi=100)\n", "plt.show()\n", "#-------------------------\n", "\n", "fig7 = plt.figure(figsize=(10,4))\n", "plt.plot(t, ProdMAD['Sbc'], '-o', c=Clrs[10], label='Cork')\n", "plt.xlabel('Period')\n", "plt.ylabel('Cork')\n", "plt.legend()\n", "plt.grid()\n", "if pSaveFig == 1:\n", " plt.savefig('VSousa-Cork-'+sufixo+'.png', dpi=100)\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }