Coder Social home page Coder Social logo

anderson-optimization / gams-parser Goto Github PK

View Code? Open in Web Editor NEW
7.0 4.0 1.0 319 KB

Python based gams syntax parser for determining sets, variables, equations, and models from gams files.

Python 42.53% GAMS 55.08% Dockerfile 0.90% Makefile 1.23% C++ 0.14% PHP 0.13%
gams optimization parsing

gams-parser's Introduction

GAMS Parser

This python library uses lark to parse the algebraic modeling language GAMS.

Package Overview

Goals

  1. Gather rich data on structure of model in a gams file.

  2. Inject custom defintions and data into gams files based upon a DSL.

  3. Format and write datasets into common GAMS formats

  4. [Future direction] Gather defined data from model using python api.

    • This is probably outside the scope of this library as it requires GAMS as a dependency.

Contents

Parser

GAMS to AST Model

This accomplishes goal 1.

Create structure model from GAMS code.

SHORT PARAGRAPH ON THIS FUNCTIONALITY

HOW TO USE HERE

Examples

EXAMPLES HERE

Injector

AO GAMS DSL

This accomplishes goal 2.

Inject GAMS code into gams file using AO context item.

SHORT PARAGRAPH ON THIS FUNCTIONALITY

HOW TO USE HERE

Examples

List and map items

Template file

* Project
set project /
**ao list project
/;

set asset /
**ao list asset
new_solar
new_battery
/;

set gen(asset) /
**ao list asset "type startswith asset:gen"
**ao list asset "type startswith asset:battery"
new_solar
new_battery
/;


set project2asset(project,asset) /
**ao map project asset
/;

set project2data(project,data) /
**ao map project data
/;

Output

* Project
set project /
SiteAProject
/;

* Asset
set asset /
SiteA_Parcel, ArcelormittalClevelandInc_GENC
new_solar
new_battery
/;

set gen(asset) /
ArcelormittalClevelandInc_GENC
new_solar
new_battery
/;

set project2asset(project,asset) /
SiteAProject.SiteA_Parcel
SiteAProject.ArcelormittalClevelandInc_GENC
SiteAProject.new_battery
SiteAProject.new_solar
/;

set project2data(project,data) /
SiteAProject.SiteALoad
SiteAProject.SiteA_TOURates
/;

Params

Template

parameter param_financial(project,financial_field) /
**ao param project 'step.financial.parameter.simplefinance'
/;

parameter param_solar(project,solar_field) /
**ao param project 'step.solar.parameter.solar'
/;

** SOC min/max????
parameter param_battery(project,battery_field) /
**ao param project 'step.battery.parameter.batterycapitalcost'
**ao param project 'step.battery.parameter.batterycharacter'
**ao param project 'step.battery.parameter.batterysize'
/;

Output

parameter param_financial(project,financial_field) /
SiteAProject.stateTaxRate 0.05
SiteAProject.inflationRate 0.03
SiteAProject.period 25
SiteAProject.federalTaxRate 0.25
SiteAProject.omEscalationRate 0.02
SiteAProject.discountRate 0.08
/;

parameter param_solar(project,solar_field) /
SiteAProject.degradationRate 2
SiteAProject.capacityCost 1500
SiteAProject.omCost 2
SiteAProject.capacityPower 5
/;

** SOC min/max????
parameter param_battery(project,battery_field) /
SiteAProject.energyCosts 1500
SiteAProject.capacityCosts 1500
SiteAProject.dischargeEfficiency 0.95
SiteAProject.chargeEfficiency 0.95
SiteAProject.duration 2
/;

Tariff Structure

Template

set product /energy,demand/;
set period /period1*period10/;
set tier /tier1*tier10/;

parameter product_rate(supply,product,period,tier) "Rate of energy/demand cost ($/kWh and $/kW)";
parameter product_adj(supply,product,period,tier) "Rate of adjustment ($/kWh and $/kW)";

set weekday_schedule(supply,product,month,hour,period);
set weekend_schedule(supply,product,month,hour,period);


** Tariff definition

parameter product_rate(supply,product,period,tier) /
**ao tariff product_rate
/;
parameter product_adj(supply,product,period,tier) /
**ao tariff product_adj
/;

set weekend_schedule(supply,product,month,hour,period) /
**ao tariff weekend_schedule
/;

set weekday_schedule(supply,product,month,hour,period) /
**ao tariff weekend_schedule
/;

Output

set product /energy,demand/;
set period /period1*period10/;
set tier /tier1*tier10/;

parameter product_rate(supply,product,period,tier) "Rate of energy/demand cost ($/kWh and $/kW)";
parameter product_adj(supply,product,period,tier) "Rate of adjustment ($/kWh and $/kW)";

set weekday_schedule(supply,product,month,hour,period);
set weekend_schedule(supply,product,month,hour,period);


** Tariff definition

parameter product_rate(supply,product,period,tier) /
supply1.demand.period1.tier1 0
supply1.demand.period2.tier1 19.8
supply1.demand.period3.tier1 17
supply1.energy.period1.tier1 0.0584
supply1.energy.period2.tier1 0.0321
supply1.energy.period3.tier1 0.04
supply1.energy.period4.tier1 0.032
/;
parameter product_adj(supply,product,period,tier) /
supply1.demand.period1.tier1 0
supply1.demand.period2.tier1 0
supply1.demand.period3.tier1 0
supply1.energy.period1.tier1 0
supply1.energy.period2.tier1 0
supply1.energy.period3.tier1 0
supply1.energy.period4.tier1 0
/;

set weekend_schedule(supply,product,month,hour,period) /
supply1.demand.m1.h1.period1
supply1.demand.m1.h2.period1
supply1.demand.m1.h3.period1
supply1.demand.m1.h4.period1
supply1.demand.m1.h5.period1
supply1.demand.m1.h6.period1
supply1.demand.m1.h7.period1
supply1.demand.m1.h8.period1
supply1.demand.m1.h9.period1
supply1.demand.m1.h10.period1
supply1.demand.m1.h11.period1
supply1.demand.m1.h12.period1
supply1.demand.m1.h13.period1
supply1.demand.m1.h14.period1
supply1.demand.m1.h15.period1
supply1.demand.m1.h16.period1
supply1.demand.m1.h17.period1
* etc...
/;

Writer

Dataframe to CSV

This writes pandas dataframes to csv in ways that GAMS can easily digest.

SHORT PARAGRAPH ON THIS FUNCTIONALITY

HOW TO USE HERE

EXAMPLES

Grammar

  • The current grammar is focused on capturing all GAMS syntax as long as it is valid and less concerned about catching invalid syntax.
  • The current grammar is built to gather data related to symbol definitions. As such, other parts of the grammar may be not accurate.

Difficulties

  • Gams operates on two passes through the input files. Compiler statements are done before execution and are used to directly include gams code into a parent file or add/remove specific statements.
  • Lark parses the gams file in one pass and is a somewhat hybrid of compiler/execution syntax. This leads to set values possibly containing only include statements, whereas GAMS would check to ensure these are actual set values at execution time as well.

Gams Syntax

https://www.gams.com/latest/docs/UG_SetDefinition.html

ISSUES

The expression grammar doesn't obey order of operations for math. use this calculater as reference for parsing structure. https://github.com/lark-parser/lark/blob/master/examples/calc.py

Tests

The test/gams folder contains sample gams syntax that can be used to test a grammar file to ensure it captures GAMS fairly complex syntax.

make test
make test FLAGS="--nocapture --nologcapture"

gams-parser's People

Contributors

ericjanderson4 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

pengfei-cheng

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.