Home /Modules/advantage/SLTradingSynthetic.sleela

SLTradingSynthetic.sleela

lib/advantage/trading/SLTradingSynthetic.sleela · 102 line(s) · sleela

<< lib/advantage/trading/SLTradingSynthetic.sleela
   SLeeLa Standard Library Definition.
   Definition: Synthetic bundling: turn one thing into many or many into one, with waste and
   fallback values for what does not sell. >>
#sleela 3.0
material;
artifact;
pure;
gasoline;
< Model 10 of the trading subject. Generalises refurbishment and aggregation and >
< covers the reverse. Digital: wrap and unwrap spreads, coin estate breakups. >
< Physical: assembled bespoke pieces. Up to 64 outputs. Check local law before >
< melting coins: some circulating coins are protected. >
class SLTradingSynthetic {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 39011;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-11-11";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-11-11";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 6;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }


  < ---- shared validation, created on first use ---- >
  SLTradingCheck chk;
  SLTradingCheck check() {
    if (chk == null) { chk = new SLTradingCheck(); }
    return chk;
  }

  << Turn one thing into many (break up a collection) or many into one
     (assemble parts). The n outputs are three parallel arrays: the value if it
     sells, the chance it sells, and the fallback value (for example melt) if
     it does not. Waste and the sales fee apply to what sells; the fallback is
     taken as received. A token wrap or unwrap is the one-output case. >>
  SLTradingResult evaluate(double inputCost, double fixedCosts,
                           double perOutputCost, double wasteRate,
                           double sellFeeRate, double holdingCostPerDay,
                           double holdDays, double[] saleValue,
                           double[] saleProbability, double[] fallbackValue,
                           int n) {
    SLTradingCheck c = check();
    if (n < 1 || n > c.maxComponents()) { return c.fail(c.countMessage()); }
    if (c.isPositive(inputCost) == false) { return c.fail(c.rangeMessage()); }
    if (c.isAmount(fixedCosts) == false) { return c.fail(c.rangeMessage()); }
    if (c.isAmount(perOutputCost) == false) { return c.fail(c.rangeMessage()); }
    if (c.isRate(wasteRate) == false) { return c.fail(c.rangeMessage()); }
    if (c.isRate(sellFeeRate) == false) { return c.fail(c.rangeMessage()); }
    if (c.isAmount(holdingCostPerDay) == false) { return c.fail(c.rangeMessage()); }
    if (c.isDays(holdDays) == false) { return c.fail(c.rangeMessage()); }

    double value = 0.0;
    int i = 0;
    while (i < n) {
      if (c.isAmount(saleValue[i]) == false) { return c.fail(c.rangeMessage()); }
      if (c.isProb(saleProbability[i]) == false) { return c.fail(c.rangeMessage()); }
      if (c.isAmount(fallbackValue[i]) == false) { return c.fail(c.rangeMessage()); }
      value = value + saleProbability[i] * saleValue[i]
                      * (1.0 - wasteRate) * (1.0 - sellFeeRate)
                    + (1.0 - saleProbability[i]) * fallbackValue[i];
      i = i + 1;
    }
    double cost = inputCost + fixedCosts + n * perOutputCost
                + holdingCostPerDay * holdDays;
    return c.finish(cost, value, holdDays);
  }
}