2022/09,江端さんの技術メモ

さくらのレンタルサーバでの「ベーシック認証」は、自力でやらず、サーバーコントロールパネルを使うこと

特定のディレクトリのみベーシック認証を実施したかったのですが、

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at [no address given] to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

から逃げられず、どうして良いのか分からなかったのですが、どうも、これ、プロバイダ側の原因(仕様変更)らしいと分かってきました。

で、今、動いたようなので、忘れない内に、私の為に、メモのみ残しておきます。

これで,".htpassword"が生成されます。

これを操作すると(内容は忘れた)、".htaccess"が生成されます。

いずれにしても、「手動で作る」ことは諦めた方がいいです。

以上

 

 

2022/09,江端さんの技術メモ

/*

golangで時刻(time)情報を生成して、その情報をpostgresqlに格納して、さらに読み出す実験

テスト用DB環境の作り方

C:\Users\ebata>psql -U postgres -p 15432
Password for user postgres:
psql (13.4, server 12.5 (Debian 12.5-1.pgdg100+1))
Type "help" for help.

postgres=# create database agent_db;
CREATE DATABASE

postgres-# \c agent_db
psql (13.4, server 12.5 (Debian 12.5-1.pgdg100+1))
You are now connected to database "agent_db" as user "postgres".

agent_db-# create table LocMessage(
agent_db(# ID     int,
agent_db(# dt     timestamp,
agent_db(# Lat    double precision,
agent_db(# Lng    double precision,
agent_db(# TYPE   varchar(10),
agent_db(# POPUP  int
agent_db(# );

agent_db=# \dt
           List of relations
 Schema |    Name    | Type  |  Owner
--------+------------+-------+----------
 public | locmessage | table | postgres
(1 row)


agent_db=# select * from locmessage;
 id | dt | lat | lng | type | popup
----+----+-----+-----+------+-------
(0 rows)

と、まあ、こんな感じ

*/

package main

import (
	"database/sql"
	"fmt"
	"log"
	"time"

	_ "github.com/lib/pq" // ←これを追記
)

func main() {

	db, err := sql.Open("postgres",
		"user=postgres password=password host=localhost port=15432 dbname=agent_db sslmode=disable")
	if err != nil {
		log.Fatal("OpenError: ", err)
	}
	defer db.Close()

	ins, err := db.Prepare("insert into locmessage(id,dt,lat,lng,type,popup) VALUES($1,$2,$3,$4,$5,$6)")
	if err != nil {
		log.Fatal("db.Exec Error: ", err)
	}

	a := 1
	b := time.Now()
	c := 139.02
	d := 38.02
	e := 1
	f := 1

	ins.Exec(a, b, c, d, e, f)

	///// 書き込みここまで
	// 書き込まれたデータはこんな感じになっている (2回プログラムを実施)
	/*
		agent_db=# select * from locmessage;
		 id |             dt             |  lat   |  lng  | type | popup
		----+----------------------------+--------+-------+------+-------
		  1 | 2022-09-15 23:54:36.877274 | 139.02 | 38.02 | 1    |     1
		  1 | 2022-09-15 23:55:16.574776 | 139.02 | 38.02 | 1    |     1
		(2 rows)
	*/
	// さて、このDBからどうやって、時間情報を取り出すか?

	rows, err := db.Query("select dt from locmessage")
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()

	var dt time.Time

	for rows.Next() {
		if err := rows.Scan(&dt); err != nil {
			fmt.Println(err)
		}

		fmt.Println(dt)

		// 出力結果が、こんな感じになって、ちょっと変 "+0000 +0000"は、いらん
		/*
			2022-09-15 23:54:36.877274 +0000 +0000
			2022-09-15 23:55:16.574776 +0000 +0000
			2022-09-16 00:06:18.870051 +0000 +0000
			2022-09-16 00:12:51.251972 +0000 +0000
			.....
			2022-09-16 13:57:46.704896 +0000 +0000
		*/

		// こんな感じにすれば、整数などでデータ取り出せる
		fmt.Println(dt.Date())   // e.g 2022 September 16
		fmt.Println(dt.Clock())  // 13 57 46
		fmt.Println(dt.Minute()) // 57
		fmt.Println(dt.Hour())   // 13
		fmt.Println(dt.Day())    // 16

	}

}

こんだけのことがやりたかっただけなのに、"golang" "postgresql", "timestamp", "time.Time"で検索しても、ドンピシャの情報が出てこないんですよ、不思議なことに。

2022/09,江端さんの技術メモ

dis: 0.2839182387773051
dis: 0.09663299157559467
dis: 1.2640838214822172
dis: 0.2725675930110564
fatal error: concurrent map iteration and map write
goroutine 26 [running]:
runtime.throw({0x432b38, 0xc000292000})
        c:/go/src/runtime/panic.go:1198 +0x76 fp=0xc00033d858 sp=0xc00033d828 pc=0x196416
runtime.mapiternext(0x48a620)
        c:/go/src/runtime/map.go:858 +0x4eb fp=0xc00033d8c8 sp=0xc00033d858 pc=0x16f8cb
main.echo3({0x48f558, 0xc00013e2a0}, 0x0)
---
さて、やっつけますか。
「mapの競合状態のはなし」のページを参考にさせて頂き、新しく作ったmapが悪さをしているとアタリをつけました。
で、当初、sync.Mutexでロックしていたのですが、デッドロックが発生してしまいました。
そこで、sync.RWMutexに変更したところ、デッドロックが発生しなくなりました。
―― というのは気のせいのようで、RWMutex でも メソッドにLock Unlockを使えば、Mutexと同じらしいので、たまたま偶然だったようです。
RWMutexのウリは、
RLock : 読み取り用のロック。RLock同士はブロックせず、Lockのみがブロックされる。解除時は RUnlockを使う
Lock : Mutexと同じロック。RLock, Lock双方をブロックする。
のようです。今回は、読み出し中に、書き込みや削除処理が走ることがあったのですが、読み出し場所が一箇所だけだったので、普通に、Lock, Unlockを使用することにしました。
これでサーバがダウンすることはなくなりました。
以上

2022/09,江端さんの技術メモ

Goにはクラスの概念がありません。

C++で作ってきたプログラムをGoに移植しているのですが、クラスで記載した部分を上手いこと使い回したくても、それができなくて困っていました。

で、

GO言語でクラスっぽいことをする

という記事を見つけて、これを使って勉強させて頂いていますが、困ったことがあります。

プログラムの中の変数の意味が分からない ―― HPとか、MPとか、APは、アニメ(多分ゲームも?)の中では頻繁に登場するのですが、私は何の略号か分からなかったので、サンプルプログラムの意味が読み解けませんでした。

そこで、上記記事に記載されていたプログラムに、そのまま、私の為に、コメントを付けさせて頂きました。

package main

import (
	"fmt"
)

const line = "--------------------"

func main() {
	wiz := newWizard("魔法少女", 10, 10, 5)
	war := newWarrior("+‡†狂戦士†‡+", 10, 15, 30)

	fmt.Println(wiz.hello())
	fmt.Println(war.hello())

	fmt.Println(line)
	fmt.Println(wiz.attack())
	fmt.Println(war.attack())

	fmt.Println(line)
	fmt.Println(wiz.magic())
	fmt.Println(war.attack())
}

type human struct { // "人間"という基底クラス
	name string
	hp   int
	ap   int
}

func (h *human) init(name string, hp, ap int) { // "人間"という基底クラスのコンストラクタ(3つの初期値を入れていることに注意))
	h.name = name // 名前
	h.hp = hp     // ヒットポイント: ゲームにおいてキャラの体力や生命力を示すステータスの一つ
	h.ap = ap     // アタックポイント: ゲームにおいてキャラの行動力などを示すステータスの一つ
}

func (h *human) hello() string { // "人間"という基底クラスのメソッド1
	return fmt.Sprintf("こんにちは、私は%sです。", h.name)
}

func (h *human) attack() string { // "人間"という基底クラスのメソッド2
	return fmt.Sprintf("%sの攻撃!%dのダメージ!", h.name, h.ap)
}

type wizard struct { // "魔法使い"というクラス
	human     // "人間の属性"を手動挿入
	mp    int // マジックポイント: ゲームにおいてキャラの魔法や呪文を使う力を示すステータスの一つ
}

func newWizard(name string, hp, ap, mp int) *wizard { // "魔法使い"クラスのコンストラクタ(4つの初期値を入れていることに注意))
	w := new(wizard)     // "魔法使い"の実体生成
	w.init(name, hp, ap) // "基底クラス"の属性値の設定
	w.mp = mp            // 新しい属性値の設定
	return w
}

func (w *wizard) magic() string {
	if w.mp <= 0 {
		return fmt.Sprintf("%sは力がでない", w.name)
	}
	w.mp -= 1
	return fmt.Sprintf("%sは魔法を使った!30のダメージ!", w.name)
}

type warrior struct {
	human // "人間の属性"のみを手動挿入
}

func newWarrior(name string, hp, ap, mp int) *warrior {
	w := new(warrior)
	w.init(name, hp, ap)
	// mpは使わない? Warrior(戦士)は、マジックポイントを持たないものなの?
	return w
}

func (w *warrior) attack() string {
	return fmt.Sprintf("%sの攻撃!%dのダメージ!", w.name, w.ap*2)
}

なるほど、変数の意味が分かれば、プログラムの趣旨が読み取れました ―― 加えて、アニメの中の台詞も理解できるようになり、一石二鳥でした。
ゲームをやっている人であれば、「今更なこと」だったんだろうな、と思っています。


とほほのGo言語入門 構造体(struct)

を読んでいたら、構造体(struct)でクラスと同じことができることが分かりました。

また、インタフェース(interface)を使えばポリモーフィズムもできるみたいでした。

しかも、コンストラクタとかデストラクタとか、面倒なものもを入れていないという点も好印象です。

Golangの設計者、よく分かってるじゃん!』と、絶賛賞賛中です。

2022/09,江端さんの技術メモ

Golangで "OpenError: sql: unknown driver "postgres" (forgotten import?) "で悩まされていました。

よく分かりませんが、"go.mod", "go.sum"とかをいじることなく

(1)ターミナルから、

go get github.com/lib/pq

として、

(2)import に

import (
   //  色々
    _ "github.com/lib/pq" // ←これを追記
)

で動きました。

2022/09,江端さんの技術メモ

golangを使った、postgresqlのinsertの方法

/*

こんな感じで、DBとTableを作りました

create database agent_db;

create table LocMessage(
	ID     int,
	Lat    double precision,
	Lng    double precision,
	TYPE   varchar(10),
	POPUP  int,
	RealId int,
	PersonState      int,
	BikeState        int,
	StartStationLat  double precision,
	StartStationLng  double precision,
	GoalStationLat   double precision,
	GoalStationLng   double precision,
	StartStationName varchar(40),
	GoalStationName  varchar(40)
);

ちなみに、全部小文字になるみたいですねえ。
agent_db=# select * from locmessage;
 id |  lat   |  lng  |   type   | popup | realid | personstate | bikestate | startstationlat | startstationlng | goalstationlat | goalstationlng | startstationname | goalstationname
----+--------+-------+----------+-------+--------+-------------+-----------+-----------------+-----------------+----------------+----------------+------------------+-----------------

*/

package main

import (
	"database/sql"
	"log"

	_ "github.com/lib/pq"
)

func main() {

	db, err := sql.Open("postgres",
		"user=postgres password=password host=localhost port=15432 dbname=agent_db sslmode=disable")
	if err != nil {
		log.Fatal("OpenError: ", err)
	}
	defer db.Close()

	//_, err = db.Exec("insert into locmessage(id,lat,lng,type,popup,realid,startstationlng ) VALUES(1, 139.0, 38.0, 'PERSON',1,1,222.222);")
	// _, err = db.Exec("insert into locmessage(id,lat) VALUES(a, b);")
	//ins, err := db.Prepare("insert into locmessage(id,lat,lng,type,popup,realid,startstationlng ) VALUES(?,?,?,?,?,?,?)")
	ins, err := db.Prepare("insert into locmessage(id,lat,lng,type,popup,realid,startstationlng ) VALUES($1,$2,$3,$4,$5,$6,$7)")
	if err != nil {
		log.Fatal("db.Exec Error: ", err)
	}

	a := 1
	b := 139.02
	c := 38.02
	dd := "Tomoichi"
	e := 1
	f := 1
	g := 333.333

	//ins.Exec("1", "139.0", "38.0", "BIKE", "1", "1", "222.222")
	//ins.Exec(1, 139.01, 38.01, "BIKE", 1, 1, 222.222)
	//ins.Exec(1, 139.0, 38.0, 1, 1, 1, 222.222)
	ins.Exec(a, b, c, dd, e, f, g)

}

とりあえず、これでデータベースの書き込みはできるみたい。

2022/09,江端さんの技術メモ

(Step.1) vscodeでmarkdownを使って本文を書いて、PDF(A4サイズ)にする

vscodeに以下の拡張機能は絶対に入れる

Visual Studio CodeのMarkdown編集から、いきなりPDFを作ったり、目次を追加したりできる件

vscodeでMarkdownを使って文章を書いている時、図面のコピペを文書の中にサクっと入れることができるアドイン "Paste Image"

(Step.1) SAIやPowerPointを使って、表紙と裏表紙を描いて、PDF(A4サイズ)にする

(Step.3) ConcatPDF(GUI)を使って、上記をマージして、書籍の名前のファイル名に保存する

(Step.4) 「江端さんの本屋さん」へアップロードする

新しいファイルをドラッグして、完了

ちなみに、以前のバージョンを購入して頂いた方は、最新版を無料で御提供する旨を、メッセージに追加(現在は、『メールでご連絡下さい』としているが、自動的にやる方法があるのか? 現在調査中)。

 

2022/09,江端さんの技術メモ

最近、スペルチェックをvscodeにやらせていて、emacsでのリロードを何度もやっているのだけど、これが中々に面倒で、これを一発でできないものか、色々調べた結果です。

方針: 面倒なので、".emacs" だけの記載で終了したい
操作:<F5>押すだけ
判ったこと: ".emacs"の最後に追記すると動かない(私の環境だけかもしれないが) → ".emacs"の真ん中あたりに突っ込んだ。

;;;; バッファを一発でリロードする (.emacsの最後に記載したら動かなかったので、真ん中くらいに移動)

(defun revert-buffer-no-confirm (&optional force-reverting)
  (interactive "P")
  ;;(message "force-reverting value is %s" force-reverting)
  (if (or force-reverting (not (buffer-modified-p)))
      (revert-buffer :ignore-auto :noconfirm)
    (error "The buffer has been modified")))

;; reload buffer
(global-set-key (kbd "<f5>") 'revert-buffer-no-confirm)
;;(global-set-key "\C-c\C-r" 'revert-buffer-no-confirm)
;;(global-set-key "\M-r" 'revert-buffer-no-confirm)
;(global-set-key "\C-c\C-m" 'revert-buffer-no-confirm)

2022/09,江端さんの技術メモ

Go言語を使った自作のファジィ(Fuzzy)推論コードを探していたのですが、見つけることができずにちょっとショックを受けております。

というか、Go言語どころか、C言語でも見つけられなかった(いや、あるはずだ)ので、今、PCの中探して見つかったものをアップしておきます。

毎回スクラッチから作るのも迂遠なので。

↓のコードですが、こちらのコラムで使ったものだと思います。

/*
  このプログラムをfuzzy.cpp"という名前で保存して、
  gcc -g fuzzy.cpp -o fuzzy
  でコンパイルして下さい。
*/



#include <stdio.h>
#include <stdlib.h>


// 共通関数
double max(double a){
    return a;
}

double max(double a, double b){
    if (a > b) 
        return a;
    else 
        return b;
};

double max(double a, double b, double c ){
    return max(max(a,b), max(b,c)); 
};

double max(double a, double b, double c, double d ){
    return max(max(a,b,c), d); 
};

double min(double a){
    return a;
}

double min(double a, double b){
    if (b > a) 
        return a;
    else 
        return b;
};

double min(double a, double b, double c ){
    return min(min(a,b), min(b,c)); 
};

double min(double a, double b, double c, double d ){
    return min(min(a,b,c), d); 
};


// ファジィ表現
typedef enum scale {LESSLESS, LESS, ZERO, MORE, MOREMORE} SCALE;

// 前件部メンバーシップ関数(山3つ)クラス
class condition_MF3
{
private:
    double center;
    double width;
    SCALE express;
   
public:
    condition_MF3(double _center, double _witdth, SCALE _express){
        center = _center;
        width = _witdth;
        express = _express;

        // 使用できないファジィ表現を使った場合は止める        
        if ((express == LESSLESS) || (express == MOREMORE)){
            printf("wrong expression used \n");
            exit(0);
        }

    };
    double func(double _x);
};

double condition_MF3::func(double _x)
{
// x,yは、メンバーシップ関数上の座標を示す
    double x = _x;
    double y = 0.0; // yの値は、必ず0以上1以下になる

    if (express == LESS){
        if (x <= center - width){
            y = 1.0;
        }
        else if (x <= center){
            y = - 1.0 / width * (x - center);
        }
        else{
            y = 0.0;
        }
    }
    else if (express == ZERO){
        if (x <= center - width){
            y = 0.0;
        }
        else if (x <= center){
            y = 1.0 / width * (x - center) + 1.0;
        }
        else if (x <= center + width){
            y = -1.0 / width * (x - center) + 1.0;
        }
        else{
            y = 0.0;
        }
    }
    else if (express == MORE){
        if (x <= center){
            y = 0.0;
        }
        else if (x <= center + width){
            y = 1.0 / width * (x - center);
        }
        else{
            y = 1.0;
        }
    }
    else {
        printf("wrong expression\n");
        exit(1);
    }

    return y;
};

// 前件部メンバーシップ関数(山5つ)クラス
class condition_MF5
{
private:
    double center;
    double width;
    SCALE express;
   
public:
    condition_MF5(double _center, double _witdth, SCALE _express){
        center = _center;
        width = _witdth;
        express = _express;
    };
    double func(double _x);
};


double condition_MF5::func(double _x)
{
// x,yは、メンバーシップ関数上の座標を示す
    double x = _x;
    double y = 0.0; // yの値は、必ず0以上1以下になる

    if (express == LESSLESS){
        if (x <= center - 2.0 * width){
            y = 1.0;
        }
        else if (x <= center - width){
            y = - 1.0 / width * (x - (center - 2.0 * width)) + 1.0;
        }
        else{
            y = 0.0;
        }
    }
    else if (express == LESS){
        if (x <= center - 2.0 * width){
            y = 0.0;
        }
        else if (x <= center - width){
            y = 1.0 / width * (x - (center - width)) + 1.0;
        }
        else if (x <= center){
            y = -1.0 / width * (x - (center - width)) + 1.0; 
        }
        else{
            y = 0.0;
        }
    }
    else if (express == ZERO){
        if (x <= center - width){
            y = 0.0;
        }
        else if (x <= center){
            y = 1.0 / width * (x - center) + 1.0;
        }
        else if (x <= center + width){
            y = -1.0 / width * (x - center) + 1.0;
        }
        else{
            y = 0.0;
        }
    }
    else if (express == MORE){
        if (x <= center){
            y = 0.0;
        }
        else if (x <= center + width){
            y = 1.0 / width * (x - (center + width)) + 1.0;
        }
        else if (x <= center + 2.0 * width){
            y = -1.0 / width * (x - (center + width)) + 1.0; 
        }
        else{
            y = 0.0;
        }
    }
    else if (express == MOREMORE){
        if (x <= center + width){
            y = 0.0;
        }
        else if (x <= center + 2.0 * width){
            y = 1.0 / width * (x - (center + 2.0 * width)) + 1.0;
        }
        else{
            y = 1.0;
        }
    }

    return y;
};

// 後件部メンバーシップ関数(山3つ)クラス  
class action_MF3
{
private:
    double center;
    double width;
    SCALE express;

    double x;
    double y;

public:
    action_MF3(double _center, double _witdth, SCALE _express){

        y = 0.0; // yの値は、必ず0以上1以下になる
       
        center = _center;
        width = _witdth;
        express = _express;
        
        if (express == LESS){
            x = center - width;
        }
        else if (express == ZERO){
            x = center;
        }
        else if (express == MORE){
            x = center + width;
        }
        else{
            printf("wrong scale expression\n");
            exit(0);
        }
    };
    
    void func_Max(double b){
        y = max(b, y);
    };
    
// X座標を返す
    double func_X(void){
        return x;
    };
    
    // (最大値で更新された、最後の)Y座標を返す
    double func_Y(){
        return y;
    };

};

// 後件部メンバーシップ関数(山5つ)クラス  
class action_MF5
{
private:
    double center;
    double width;
    SCALE express;

    double x;
    double y;

public:
    action_MF5(double _center, double _witdth, SCALE _express){
        y = 0.0; // yの値は、必ず0以上1以下になる
       
        center = _center;
        width = _witdth;
        express = _express;
        
        if (express == LESSLESS){
            x = center - 2.0 * width;
        }
        else if (express == LESS){
            x = center - width;
        }
        else if (express == ZERO){
            x = center;
        }
        else if (express == MORE){
            x = center + width;
        }
        else if (express == MOREMORE){
            x = center + 2.0 * width;
        }
        else{
            printf("wrong scale expression\n");
        }
    };
    
    void func_Max(double b){
        y = max(b, y);
    };
    
// X座標を返す
    double func_X(void){
        return x;
    };
    
    // (最大値で更新された、最後の)Y座標を返す
    double func_Y(){
        return y;
    };

};

int main()
{
    // ダイエット経過期間
    condition_MF3 Period_Short(14, 14, LESS); // 0日
    condition_MF3 Period_Middle(14, 14, ZERO); // 14日
    condition_MF3 Period_Long(14, 14, MORE); // 28日

    // いわゆる「停滞期」(14日間あたりの平均減重変化量)
    condition_MF3 WeighChange_Small(1.0, 1.0, LESS); // 0kg
    condition_MF3 WeighChange_Middle(1.0, 1.0, ZERO); // 1kg
    condition_MF3 WeighChange_High(1.0, 1.0, MORE); // 2kg

    // 苦痛度
    condition_MF3 Suffer_Zerol(400.0, 400.0, LESS); // 0
    condition_MF3 Suffer_Middle(400.0, 400.0, ZERO); // 400
    condition_MF3 Suffer_Hight(400.0, 400.0, MORE); // 800

    // BMI
    condition_MF5 BMI_lowlow(22.0, 3.0, LESSLESS); // 痩せすぎ(15.0)
    condition_MF5 BMI_low(22.0, 3.0, LESS);// 痩せぎみ(19.0)
    condition_MF5 BMI_Normal(22.0, 3.0, ZERO);// 普通(22.0)
    condition_MF5 BMI_High(22.0, 3.0, MORE); // 太りぎみ(25.0)
    condition_MF5 BMI_HighHigh(22.0, 3.0, MOREMORE);// 太りすぎ(28.0)

    // 一日の摂取カロリー
    action_MF5 Eat_LittleLittle(2500, 1000, LESSLESS); // 500 Kcal
    action_MF5 Eat_Little(2500, 1000, LESS);// 1500 Kcal
    action_MF5 Eat_Normal(2500, 1000, ZERO);// 2500 Kcal
    action_MF5 Eat_Lot(2500, 1000, MORE);// 3500 Kcal
    action_MF5 Eat_LotLot(2500, 1000, MOREMORE);// 4500 Kcal



    
    
    // 超シンプルシミュレータ投入
    // 体重変動シミュレーションプログラム
    //基礎代謝量と求め方	?ハリス・ベネディクト方程式(日本人版) 計算式
    //http://www.kintore.info/kisotaisya_mass/index.cgi
    //【計算式】
    // 男性 66.5+(体重kg×13.8)+(身長cm×5.0)-(年齢×6.8)
    // 女性 665.1+(体重kg×9.6)+(身長cm×1.9)-(年齢×7.0)							
    // 江端智一 2014日2月4日現在のデータを使って計算
    double weight = 78.0;  // 開始時の体重 78.0kg
    double lengthw = 172.0;  // 身長 172cm
    double age = 49.0 + 94/365;  // 開始時の年齢 49歳と94日 (2014年2月4日現在)

    // 最初の基礎代謝カロリー 男性  66.5+(体重kg×13.8)+(身長cm×5.0)-(年齢×6.8)
    double consumption_calorie =  
        66.5 + weight * 13.8 + 172.0 * 5.0 - age * 6.8;
    // このカロリーに運動消費カロリーを加えて、
    // トータルの一日の消費カロリーを算出する

    // (現在の体重 66.5kgにおいて)一日運動消費カロリー 708kcalと推定し、
    // このカロリーは、体重に比例するものと仮定する。
    consumption_calorie += weight/66.5 * 708.0;
    printf("consumption_calorie = %f\n",  consumption_calorie);

    double last_weight[14]; // 過去14日分の体重
    
    // 最初は全部同じ体重とする 14日経過までは同じ体重とする
    for (int i = 0; i++; i < 14){
        last_weight[i] = weight;
    }

    // ここからループ開始
    for (int day=0; day < 1460; day++){ // 1460日は、ちょうど4年分の日数
        // 7kcal = 1g = 0.001kg とする    

        // 体重データを一日分ずらす
        for (int i = 0; i++; i < 14-1 ){
            last_weight[i+1] = last_weight[i];
        }
        last_weight[0] = weight;
        
        // 線形補完の傾きだけを求める(14日分)
        // http://d.hatena.ne.jp/rainlib/20090112/1231735459
        double k1=0,k2=0,k3=0,k4=0;
        for (int i = 0; i++; i < 14 ){
            k1 += (double)i * (double)i;
            k2 += (double)i * last_weight[i];
            k3 += (double)i;
            k4 += last_weight[i] *last_weight[i];
        }
        double dW = (14.0 * k2 - k3 * k4) / (14.0 * k4 - k3*k3); // 14

        // BMIの計算式 体重(Kg)÷身長(m)÷身長(m) 江端の身長172cm
        double bmi = weight / 1.72 / 1.72;
        
        // [ルール1] ダイエット開始直後は、「無条件にがんばれる」
        double r1 = min(Period_Short.func(day));
        Eat_Little.func_Max(r1);
        //printf("r1: %f\n",r1);
        
        //printf("Eat_Little.func_X() = %f\n",Eat_Little.func_X());
        //printf("Eat_Little.func_Y() = %f\n",Eat_Little.func_Y());
        
        // [ルール2] ダイエット一ヶ月を過ぎても、停滞し続けると「切れる」
        double r2 = min(Period_Long.func(day),WeighChange_Small.func(dW));
        Eat_LotLot.func_Max(r2);
        //printf("r2: %f\n",r2);
        
        // [ルール3] ダイエット2週間を過ぎて、停滞し続けると「緩んでくる」
        double r3 = min(Period_Middle.func(day),WeighChange_Small.func(dW));
        Eat_Normal.func_Max(r3);
        //printf("r3: %f\n",r3);
        
        // [ルール4] 停滞期がなく順調に体重が落ちている場合は「がんばれる」
        double r4 = min(WeighChange_High.func(dW));
        Eat_Little.func_Max(r4);
		//        printf("r4: %f\n",r4);
        
        // [ルール5] ダイエット一ヶ月を過ぎて、再び太り出してくると、またダイエットを再開してしまう
        double r5 = min(Period_Long.func(day),BMI_High.func(bmi));
        Eat_Little.func_Max(r5);
        //printf("r5: %f\n",r5);
        
        // [ルール6] ダイエット一ヶ月を過ぎて、太り過ぎると、滅茶苦茶なダイエットを始めてしまう
        double r6 = min(Period_Long.func(day),BMI_HighHigh.func(bmi));
        Eat_LittleLittle.func_Max(r6);

        //printf("r6: %f\n",r6);
		/*        
        printf("Eat_LittleLittle.func_X() = %f\n",Eat_LittleLittle.func_X());
        printf("Eat_LittleLittle.func_Y() = %f\n",Eat_LittleLittle.func_Y());
        printf("Eat_Little.func_X() = %f\n",Eat_Little.func_X());
        printf("Eat_Little.func_Y() = %f\n",Eat_Little.func_Y());
        printf("Eat_Normal.func_X() = %f\n",Eat_Normal.func_X());
        printf("Eat_Normal.func_Y() = %f\n",Eat_Normal.func_Y());
        printf("Eat_Lot.func_X() = %f\n",Eat_Lot.func_X());
        printf("Eat_Lot.func_Y() = %f\n",Eat_Lot.func_Y());
        printf("Eat_LotLot.func_X() = %f\n",Eat_LotLot.func_X());
        printf("Eat_LotLot.func_Y() = %f\n",Eat_LotLot.func_Y());
        */
        
        double source_calorie =  
            (Eat_LittleLittle.func_X() * Eat_LittleLittle.func_Y() + 
             Eat_Little.func_X() * Eat_Little.func_Y() +
             Eat_Normal.func_X() * Eat_Normal.func_Y() +
             Eat_Lot.func_X() * Eat_Lot.func_Y() +
             Eat_LotLot.func_X() * Eat_LotLot.func_Y() )  
            /
            (Eat_LittleLittle.func_Y() + 
             Eat_Little.func_Y() +
             Eat_Normal.func_Y() +
             Eat_Lot.func_Y() +
             Eat_LotLot.func_Y() ) ;
        
        printf("\n source_calorie = %f\n",source_calorie);

        double diff_weight = (source_calorie - consumption_calorie)/7.0 / 1000.0;
        weight += diff_weight;
        age += 1.0/365.0;
        consumption_calorie =  66.5 + weight * 13.8 + 172.0 * 5.0 - age * 6.8;
        consumption_calorie += weight/66.5 * 708.0;
        //       printf("day:%d\tweight = %f\tconsumption_calorie =%f\n", day,weight, consumption_calorie);
    }



}

ファジィ推論

 

2022/09,江端さんの技術メモ

長女が125CCのバイクに乗り替えてから、放置し続けてきたバイクが動かなくなった、というので、廃棄処分にするか相談を受けていたのですが ―― 私には、どうにも故障に見えない。

 

バイク屋で見て貰った時間は、3秒くらい。

簡単に言えば、「スピードメータに、オイル交換やらバッテリーやらの電飾表示が出ないので、充電してもダメ」ということらしい。ただし「スターターが壊れている可能性もあるが、まずはバッテリーを交換してみないと分からん」と言われました。

で、早速Amazonで購入。もっとも安いもので2000円、リチウムは16000円もするのでスコープ外として、今回はこれにしました。

購入の判断は、評価者数の多さと(1040個の評価)と、

評価の分布でした。私は、こういう正規分布に近い評価を見ると、安心できます。

さて、これが届くまでの間、バイクの空気圧の補充をしに、ガススタンドにいっていました。150~200PKは必要なのに、90くらいしか入っていませんでした。


本日の昼前にバッテリーが届いたので、さきほど5分くらいで、バッテリー交換しました(一度やっているので、コツは掴んでいる)。

ちゃんと充電されて配送されてきた。感心、感心。

バッテリーボックスの回りにガムテープを貼っているのは・・・以前、きちんとバッテリー収納ボックスを開けてみたはいいが、閉めれなかったんだろうな~、と推測。 → 保険証を貼りつけていたことを思い出した。

古いバッテリーを取り出す。

新しいバッテリーに交換。

無事、インジケータの表示とセル起動を確認して、バイク復活を確認しました。

バッテリー不良ごときで、バイクが廃棄されたかもしれないかも、と考えると、さすがの私も「もったいない」と思います。

敢えていうのであれば、「面倒くさい」あたりがしっくりきます。

3000円程度であれば、今後もバッテリー交換を繰替えせば良いと思うけど、もしかしたら、バッテリー液交換(で遊ぶ)かもしれないので、自分の為に、マニュアルを添付しておきます。

しかし、我が家には、当然のように、テスターや、電圧可変ができる安定化電源(ジャンク屋で拾ってきた)があるのだけど、一般家庭にはないかもしれません。

こういうDIYをサクっとできるのは、そういうことが好きな人間だけの特権かもしれません。

以上