Commit f0bce9ab authored by Valentin Reis's avatar Valentin Reis
Browse files

[ci] CI/Packaging refactor, lint passes.

parent fd0839e5
Pipeline #10307 passed with stages
in 35 seconds
......@@ -3,33 +3,16 @@ stages:
- lib
- docs
variables:
NIX_PATH: "nixpkgs=./."
ormolu:
stage: source
tags:
- kvm
- nix
script:
- |
nix-shell --pure -E '
let pkgs = (import ./.);
in pkgs.mkShell {
buildInputs = [pkgs.fd pkgs.ormolu];
shellHook =
"export LOCALE_ARCHIVE=${pkgs.glibcLocales}/lib/locale/locale-archive \n" +
"export LANG=en_US.UTF-8";
}
' --run bash <<< '
RETURN=0
cd src
for F in $(fd -e hs); do
ormolu -o -XTypeApplications -m check $F
if [ $? -ne 0 ]; then
echo "[!] $F does not pass ormolu haskell format check." >&2
RETURN=1
fi
done
if [ $RETURN -ne 0 ]; then exit 1; fi
'
- nix-shell -p gnumake --run "make ormolu"
hlint:
stage: source
......@@ -37,47 +20,30 @@ hlint:
- kvm
- nix
script:
- nix-shell --pure -p '(import ./.).hlint' --run bash <<< 'hlint src/ --hint=./.hlint.yaml'
- nix-shell -p gnumake --run "make hlint"
dhall-format:
shellcheck:
stage: source
tags:
- kvm
- nix
script:
- |
nix-shell --pure -p '(import ./.).fd' '(import ./.).haskellPackages.dhall' --run bash <<< '
RETURN=0
for F in $(fd -e dhall); do
dhall format < $F | cmp -s $F -
if [ $? -ne 0 ]; then
echo "[!] $F does not pass dhall-format format check." >&2
RETURN=1
fi
done
if [ $RETURN -ne 0 ]; then exit 1; fi
'
- nix-shell -p gnumake --run "make shellcheck"
nix-build:
stage: lib
dhall-format:
stage: source
tags:
- kvm
- nix
script: nix-build -A hbandit --no-build-output
script:
- nix-shell -p gnumake --run "make dhall-format"
check-vendored-documentation:
stage: docs
nix-build:
stage: lib
tags:
- kvm
- nix
script:
- |
nix-shell nix/default.nix -A hack --run bash --arg useGhcide false <<< '
echo "check: is vendored documentation up to date?"
cp -r doc/ doc2
./shake doc
diff -r doc doc2
'
script: nix-build -A hbandit --no-build-output
readthedocs:
stage: docs
......
......@@ -90,18 +90,31 @@ shellcheck:
done
'
.PHONY: hlint
dhall-format:
@nix-shell --pure -p hlint --run bash <<< '
hlint src/ --hint=./.hlint.yaml
'
.PHONY: dhall-format
dhall-format:
@nix-shell --pure -p fd haskellPackages.dhall --run bash <<< '
RETURN=0
for F in $$(fd -e dhall); do
dhall format --inplace $$F
dhall format < $$F | cmp -s $$F -
if [ $$? -ne 0 ]; then
echo "[!] $$F does not pass dhall-format format check. Formatting.." >&2
dhall format --inplace $$F
RETURN=1
fi
done
if [ $$RETURN -ne 0 ]; then exit 1; fi
'
.PHONY: ormolu
ormolu:
@nix-shell --pure -E '
let pkgs = (import ./. {});
let pkgs = import <nixpkgs> {};
in pkgs.mkShell {
buildInputs = [pkgs.fd pkgs.ormolu];
shellHook =
......@@ -109,9 +122,16 @@ ormolu:
"export LANG=en_US.UTF-8";
}
' --run bash <<< '
for F in $$(fd -e hs); do
ormolu -o -XTypeApplications -o -XPatternSynonyms -m inplace $$F
RETURN=0
for F in $$(fd -E src/HBandit/Tutorial.hs -e hs); do
ormolu -o -XTypeApplications -o -XPatternSynonyms -m check $$F
if [ $$? -ne 0 ]; then
echo "[!] $$F does not pass ormolu format check. Formatting.." >&2
ormolu -o -XTypeApplications -o -XPatternSynonyms -m inplace $$F
RETURN=1
fi
done
if [ $$RETURN -ne 0 ]; then exit 1; fi
'
.PHONY: doc
......
......@@ -25,6 +25,7 @@ in pkgs // rec {
with pkgs.haskell.lib; rec {
hbandit = self.callPackage ./hbandit.nix {};
panpipe = doJailbreak super.panpipe;
refined = unmarkBroken super.refined;
lazysmallcheck2012 = null;
panhandle = doJailbreak (dontCheck (self.callCabal2nix "panhandle"
(builtins.fetchTarball
......
......@@ -167,7 +167,6 @@ in prelude.defaults.Package
prelude.v "2.0"
, category =
"algorithms"
, description =
"hbandit"
, library =
......@@ -187,8 +186,7 @@ in prelude.defaults.Package
"test"
( prelude.defaults.TestSuite
⫽ { build-depends =
common.libdep
# [ nobound "hbandit" ]
common.libdep # [ nobound "hbandit" ]
, hs-source-dirs =
[ "test" ]
, type =
......@@ -198,7 +196,7 @@ in prelude.defaults.Package
)
]
, extra-doc-files =
["literate/*.png"] : List Text
[ "literate/*.png" ] : List Text
, extra-source-files =
[] : List Text
, license =
......
......@@ -5,9 +5,9 @@
-- License : MIT
-- Maintainer : fre@freux.fr
module HBandit
( module HBandit.Class
, module HBandit.Types
, module HBandit.Util
( module HBandit.Class,
module HBandit.Types,
module HBandit.Util,
)
where
......
......@@ -5,169 +5,7 @@ module Main
)
where
import Control.Lens
import Control.Monad.Primitive
import Data.Generics.Labels ()
import Data.Sequence
import H.Prelude as R
import HBandit.Class
import HBandit.Exp4R
import HBandit.Types as HBT
import Protolude
import Refined hiding (NonEmpty)
import Refined.Unsafe
import System.Random
type ZO = ZeroOne Double
rrequire :: (MonadR m, Literal lib a) => lib -> m ()
rrequire p = void [r| suppressMessages(require(p_hs,character.only=TRUE)) |]
rpackages :: [Text]
rpackages = ["ggplot2", "plyr", "plotly"]
data OutputType = Plotly | Pdf | Png
deriving (Read, Show, Enum, Bounded)
data GameState
= GameState
{ historyActions :: Seq Int,
historyCosts :: Seq Double,
historyConstraints :: Seq Double,
bandit :: Exp4R () Int (ObliviousRep Int)
}
deriving (Generic)
onePass ::
( MonadState GameState m,
MonadIO m,
(Functor (Zoomed m' (Int, StdGen))),
(Zoom m' m (Exp4R () Int (ObliviousRep Int)) GameState)
) =>
[(ZO, ZO, ZO, ZO, ZO, ZO)] ->
m ()
onePass dataset =
for_ dataset $ \(c1, c2, c3, r1, r2, r3) ->
use (#bandit . #lastAction) >>= \case
Just (LastAction a _ _) -> do
let (c, r) = case a of
1 -> (c1, r1)
2 -> (c2, r2)
3 -> (c3, r3)
g <- liftIO getStdGen
(a', g') <- zoom (#bandit) $ stepCtx g (Just $ Feedback c r) ()
liftIO $ setStdGen g'
#historyActions %= (Data.Sequence.|> a')
#historyCosts %= (Data.Sequence.|> unrefine c)
#historyConstraints %= (Data.Sequence.|> unrefine r)
Nothing -> do
g <- liftIO getStdGen
(a', g') <- zoom (#bandit) $ stepCtx g Nothing ()
liftIO $ setStdGen g'
plot1pass ::
(MonadR m, MonadIO m) =>
[Double] ->
[Double] ->
[Double] ->
[Double] ->
[Double] ->
[Double] ->
m (SomeSEXP (PrimState m))
plot1pass one_cost two_cost three_cost one_risk two_risk three_risk = do
let b = initCtx $ Exp4RCfg
{ expertsCfg = expertsC,
constraintCfg = unsafeRefine 0.5,
horizonCfg = unsafeRefine 5000,
as = [1, 2, 3]
}
( GameState
((fmap fromIntegral) . toList -> hActions :: [Double])
(toList -> hCosts :: [Double])
(toList -> hConstraints :: [Double])
_
) <- execStateT (onePass dataset) $
GameState
{ historyActions = Data.Sequence.empty,
historyCosts = Data.Sequence.empty,
historyConstraints = Data.Sequence.empty,
bandit = b
}
[r|
print(length(hActions_hs))
print(length(hCosts_hs))
print(length(one_cost_hs))
d <- data.frame(seq(1, length(hActions_hs), 1),
hActions_hs,
hCosts_hs,
hConstraints_hs,
one_cost_hs[-1],
two_cost_hs[-1],
three_cost_hs[-1],
one_risk_hs[-1],
two_risk_hs[-1],
three_risk_hs[-1])
names(d)=c("t","action","cost","risk","c1", "c2","c3", "r1","r2","r3")
print(summary(d))
best = min(mean(d$c1),mean(d$c2),mean(d$c3))
costPlot <- ggplot(d,aes(x = t, y = cumsum(cost)/t)) +
ylab("average cost") +
geom_line() +
geom_hline(yintercept = 0.3, linetype="dashed", color = "black") +
geom_hline(yintercept = 0.7, linetype="dashed", color = "black") +
geom_hline(yintercept = 0.5, linetype="dashed", color = "green")
riskPlot <- ggplot(d,aes(x = t, y = cumsum(risk)/t)) +
ylab("average risk") +
geom_line() +
geom_hline(yintercept = 0.5, linetype="dashed", color = "red") +
geom_hline(yintercept = 0.9, linetype="dashed", color = "black") +
geom_hline(yintercept = 0.3, linetype="dashed", color = "black") +
geom_hline(yintercept = 0.4, linetype="dashed", color = "green")
ggsave("data/cost.png", costPlot)
ggsave("data/risk.png", riskPlot)
|]
where
expertsC :: NonEmpty (ObliviousRep Int)
expertsC =
[ ObliviousRep [(HBT.one, 1), (HBT.zero, 2), (HBT.zero, 3)],
ObliviousRep [(HBT.zero, 1), (HBT.one, 2), (HBT.zero, 3)],
ObliviousRep [(HBT.zero, 1), (HBT.zero, 2), (HBT.one, 3)]
]
p = ZipList . fmap unsafeRefine
(ZipList dataset) =
(\a b c d e f -> (a, b, c, d, e, f) :: (ZO, ZO, ZO, ZO, ZO, ZO))
<$> p one_cost
<*> p two_cost
<*> p three_cost
<*> p one_risk
<*> p two_risk
<*> p three_risk
experiment ::
(MonadR m) =>
Double ->
m (SomeSEXP (PrimState m))
experiment n = do
one_cost <- gen01TS 0.3 <&> fromSomeSEXP
two_cost <- gen01TS 0.7 <&> fromSomeSEXP
three_cost <- gen01TS 0.5 <&> fromSomeSEXP
one_risk <- gen01TS 0.9 <&> fromSomeSEXP
two_risk <- gen01TS 0.3 <&> fromSomeSEXP
three_risk <- gen01TS 0.4 <&> fromSomeSEXP
plot1pass one_cost two_cost three_cost one_risk two_risk three_risk
where
gen01TS :: (MonadR m) => Double -> m (SomeSEXP (PrimState m))
gen01TS mu =
[r| pmax(0,pmin(1,rnorm(n_hs, mean=mu_hs, sd=0.1))) |]
main :: IO ()
main =
R.withEmbeddedR defaultConfig
$ R.runRegion
$ do
for_ rpackages rrequire
[r| theme_set(theme_bw()) |]
void $ experiment 5000
main = putText "true"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment