def main():
    solar_data = []
    solar_data += DataReader.get_daily_totals_for_file('Data/2005.csv')
    solar_data += DataReader.get_daily_totals_for_file('Data/2006.csv')
    solar_data += DataReader.get_daily_totals_for_file('Data/2007.csv')
    solar_data += DataReader.get_daily_totals_for_file('Data/2008.csv')
    solar_data += DataReader.get_daily_totals_for_file('Data/2009.csv')
    print("Imported Data")
    print("Running Simulation:")

    num_of_sims = 0
    with open('Output/rs.csv', 'w') as csvfile:
        writer = csv.writer(csvfile, delimiter=',')
        writer.writerow(['Smart Agents Wealth', 'Smart Agents Trades', 'Smart Agents Wealth (all)', 'Smart Agents Trades (all)','Controlled Agents Wealth', 'Controlled Agents Trades', 'Controlled Agents Wealth (all)', 'Controlled Agents Trades (all)', 'All Agents Wealth', 'All Agents Trades', 'All Agents Wealth (all)', 'All Agents Trades (all)', 'Smart Agents Price Correlation', 'Controlled Agents Price Correlation', 'All Agents Price Correlation'])
        for i in range(num_of_sims):
            print("Simulation:" + str(i + 1))
            market = Market(100, 10)
            for i in range(len(solar_data)):
                weather = solar_data[i]
                market.update(float(weather)/1000.0)
                price = market.price_history[-1]
                supply = market.asks[-1]
                demand = market.bids[-1]

            smart_agents = [agent for agent in market.agents if agent.use_brain == True]
            controlled_agents = market.agents[:10]
            all_agents = market.agents

            smart_wealth = sum([agent.wealth for agent in smart_agents if agent.wealth > 0])/len(smart_agents)
            smart_no_trades = sum([agent.no_trades for agent in smart_agents if agent.wealth > 0])/len(smart_agents)
            smart_wealth_a = sum([agent.wealth for agent in smart_agents])/len(smart_agents)
            smart_no_trades_a = sum([agent.no_trades for agent in smart_agents])/len(smart_agents)
                       
            controlled_wealth = sum([agent.wealth for agent in controlled_agents if agent.wealth > 0])/len(controlled_agents)
            controlled_no_trades = sum([agent.no_trades for agent in controlled_agents if agent.wealth > 0])/len(controlled_agents)
            controlled_wealth_a = sum([agent.wealth for agent in controlled_agents])/len(controlled_agents)
            controlled_no_trades_a = sum([agent.no_trades for agent in controlled_agents])/len(controlled_agents)

            all_wealth = sum([agent.wealth for agent in all_agents if agent.wealth > 0])/len(all_agents)
            all_no_trades = sum([agent.no_trades for agent in all_agents if agent.wealth > 0])/len(all_agents)
            all_wealth_a = sum([agent.wealth for agent in all_agents])/len(all_agents)
            all_no_trades_a = sum([agent.no_trades for agent in all_agents])/len(all_agents)

            smart_price_history = [agent.price_history for agent in smart_agents]
            smart_price_history = [sum(col) / float(len(col)) for col in zip(*smart_price_history)]
            smart_prediction_accuracy = str(numpy.corrcoef(smart_price_history,market.price_history)[0][1])

            controlled_price_history = [agent.price_history for agent in controlled_agents]
            controlled_price_history = [sum(col) / float(len(col)) for col in zip(*controlled_price_history)]
            controlled_prediction_accuracy = str(numpy.corrcoef(controlled_price_history,market.price_history)[0][1])

            all_price_history = [agent.price_history for agent in all_agents]
            all_price_history = [sum(col) / float(len(col)) for col in zip(*all_price_history)]
            all_prediction_accuracy = str(numpy.corrcoef(all_price_history,market.price_history)[0][1])

            writer.writerow([smart_wealth, smart_no_trades, smart_wealth_a, smart_no_trades_a, controlled_wealth, controlled_no_trades, controlled_wealth_a, controlled_no_trades_a,all_wealth, all_no_trades, all_wealth_a, all_no_trades_a,smart_prediction_accuracy,controlled_prediction_accuracy,all_prediction_accuracy])
        
    print("Creating Graphs:")
    market = Market(100, 10)
    for i in range(len(solar_data)):
        if int(i % (len(solar_data) / 100)) == 0:
            print(str(int(i / len(solar_data) * 100)) + "%")
        weather = solar_data[i]
        market.update(float(weather)/1000.0)
        price = market.price_history[-1]
        supply = market.asks[-1]
        demand = market.bids[-1]    
    
    solar_plot = pyplot.figure()
    a = solar_plot.add_subplot(111)
    a.plot(range(len(solar_data)), solar_data, label='Solar Radiance')
    a.legend()
    a.set_ylabel('kWh per m^2')
    a.set_xlabel('Day')
    solar_plot.savefig('Output/solar_radiance.png')

    aggregate_supply_demand_plot = pyplot.figure()
    b = aggregate_supply_demand_plot.add_subplot(111)
    b.plot(range(len(solar_data)), market.bids, label='Aggregate Demand')
    b.plot(range(len(solar_data)), market.asks, label='Aggregate Supply')
    b.legend()
    b.set_ylabel('Quantity')
    b.set_xlabel('Day')
    aggregate_supply_demand_plot.savefig('Output/supply_demand_history.png')

    price_history_plot = pyplot.figure()
    c = price_history_plot.add_subplot(111)
    c.plot(range(len(solar_data)), market.price_history, label='Average Trading Price')
    c.legend()
    c.set_ylabel('Price')
    c.set_xlabel('Day')
    c.set_ylim(12,22)
    price_history_plot.savefig('Output/price_history.png')

    #Plot average price expectation
    smart_agents = [agent for agent in market.agents if agent.use_brain == True]
    smart_price_history = [agent.price_history for agent in smart_agents]
    smart_price_history = [sum(col) / float(len(col)) for col in zip(*smart_price_history)]
    smart_price_prediction = pyplot.figure()
    d = smart_price_prediction.add_subplot(111)
    d.plot(range(len(solar_data)), market.price_history, label='Average Trading Price')
    d.plot(range(len(solar_data)), smart_price_history, label='Smart Agent Predicted Price')
    d.legend()
    d.set_ylabel('Price')
    d.set_xlabel('Day')
    d.set_ylim(12,22)
    smart_price_prediction.savefig('Output/smart_price_history.png')

    controlled_agents = market.agents[:10]
    controlled_price_history = [agent.price_history for agent in controlled_agents]
    controlled_price_history = [sum(col) / float(len(col)) for col in zip(*controlled_price_history)]
    controlled_price_prediction = pyplot.figure()
    e = controlled_price_prediction.add_subplot(111)
    e.axis('equal')
    e.scatter(market.price_history, controlled_price_history, label='Control Group Predicted Price', color = 'blue', alpha = 0.5, s=10)
    e.scatter(market.price_history, smart_price_history, label='Machine Learning Predicted Price', color = 'green', alpha = 0.5, s=10)
    
    e.legend()
    e.set_ylabel('Predicted Price')
    e.set_xlabel('Actual Price')
    e.set_ylim(12,20)
    e.set_xlim(12,20)
    controlled_price_prediction.savefig('Output/controlled_price_history.png')

    all_agents = market.agents
    all_price_history = [agent.price_history for agent in all_agents]
    all_price_history = [sum(col) / float(len(col)) for col in zip(*all_price_history)]
    all_price_prediction = pyplot.figure()
    f = all_price_prediction.add_subplot(111)
    f.plot(range(len(solar_data)), market.price_history, label='Average Trading Price')
    f.plot(range(len(solar_data)), all_price_history, label='All Group Predicted Price')
    f.legend()
    f.set_ylabel('Price')
    f.set_xlabel('Day')
    f.set_ylim(12,22)
    all_price_prediction.savefig('Output/all_price_history.png')
    
    #Sample Supply Demand Curve
    for agent in market.agents:
        agent.day_begin(5.0, market)

    buyers = [agent.price for agent in [agent for agent in market.agents if agent.demand > 0]]
    sellers = [agent.price for agent in [agent for agent in market.agents if agent.supply > 0]]
    buyers.sort(reverse=True)
    sellers.sort()
    while len(sellers) < len(buyers):
        buyers.pop()
    while len(buyers) < len(sellers):
        sellers.pop()

    supply_demand = pyplot.figure()
    g = supply_demand.add_subplot(111)
    g.plot(range(len(sellers)), sellers, label='Supply')
    g.plot(range(len(buyers)), buyers, label='Demand')
    g.legend()
    g.set_ylabel('Price')
    g.set_xlabel('Quantity')
    g.set_ylim(12,22)
    supply_demand.savefig('Output/supply_demand.png')

    print("Done")