Merge pull request #30 from arieeefajar/fix/assessment-form

fix(assessment-form): function certainty factor calculation
This commit is contained in:
Arie Fajar Bachtiar 2025-03-31 20:00:50 +07:00 committed by GitHub
commit 96fbafd505
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 117 additions and 39 deletions

View File

@ -7,6 +7,7 @@
use App\Models\Indicator;
use App\Models\Land;
use App\Models\Rule;
use Dotenv\Parser\Value;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
@ -18,12 +19,21 @@ public function index()
$lands = Land::select('id', 'owner')->whereNotIn('id', function ($query) {
$query->select('land_id')->from('evaluation');
})->orderBy('created_at', 'desc')->get();
$indicators = Indicator::select('id', 'name')->whereIn('id', function ($query) {
$query->select('indicator_id')->from('rule');
})->get();
$indicators = Indicator::select('id', 'name')->whereHas('rules')->with('rules')->get();
return view('assessment.form', compact('lands', 'indicators'));
}
public function calculateCFc($cf1, $cf2)
{
if ($cf1 >= 0 && $cf2 >= 0) {
return $cf1 + $cf2 * (1 - $cf1);
} elseif ($cf1 < 0 && $cf2 < 0) {
return $cf1 + $cf2 * (1 + $cf1);
} else {
return ($cf1 + $cf2) / (1 - min(abs($cf1), abs($cf2)));
}
}
public function store(Request $request)
{
$customMessages = [
@ -40,54 +50,120 @@ public function store(Request $request)
return redirect()->back()->withInput();
}
$indicators = $request->except('_token', 'land');
if (count($indicators) == 0) {
$cf_e = $request->except('_token', 'land');
if (count($cf_e) == 0) {
toast('Harap pilih jawaban untuk nilai indikator', 'error')->position('top-right')->autoclose(3000);
return redirect()->back();
}
$rules = Rule::whereIn('indicator_id', array_keys($indicators))->get()->keyBy('indicator_id');
$cf_h = collect(Rule::whereIn('indicator_id', array_keys($cf_e))
->orderByDesc('cf')
->get()
->unique('indicator_id')
->pluck('cf', 'indicator_id')
->sortKeys());
$cfValue = [];
foreach ($indicators as $id => $value) {
if (isset($rules[$id])) {
$rule = $rules[$id];
$cfValue[$id] = $rule->mb * $value;
} else {
toast("Rule untuk indikator ID {$id} tidak ditemukan.", 'error')->position('top-right')->autoclose(3000);
return redirect()->back();
}
$cf_he = collect($cf_e)->map(function ($value, $key) use ($cf_h) {
return [
"cf(h,e)" . $key => (float) $value * (float) ($cf_h[$key] ?? 1)
];
})->collapse();
$cf_he_array = $cf_he->toArray();
$cf_combined = array_shift($cf_he_array);
foreach ($cf_he_array as $cf) {
$cf_combined = $this->calculateCFc($cf_combined, $cf);
}
$cfCombine = array_shift($cfValue);
$cf_pakar = Rule::pluck('cf')->toArray();
foreach ($cfValue as $cf) {
$cfCombine = $cfCombine + $cf * (1 - $cfCombine);
$mean_cf = collect($cf_pakar)->avg();
$std_dev_cf = sqrt(collect($cf_pakar)->map(function ($cf) use ($mean_cf) {
return pow($cf - $mean_cf, 2);
})->avg());
$threshold_cocok = $mean_cf + $std_dev_cf;
$threshold_cocok_bersyarat = $mean_cf - $std_dev_cf;
if ($cf_combined > $threshold_cocok) {
$hasil = 'cocok';
} elseif ($cf_combined > $threshold_cocok_bersyarat) {
$hasil = 'cocok bersyarat';
} else {
$hasil = 'tidak cocok';
}
$landName = Land::find($request->land)->owner;
// dd($cf_e, $cf_h, $cf_he, $cf_he_array, $cf_combined, $hasil);
DB::beginTransaction();
try {
$evaluation = new Evaluation;
$evaluation->land_id = $request->land;
$evaluation->cf_value = $cfCombine;
$evaluation->cf_value = $cf_combined;
$evaluation->user_id = auth()->user()->id;
$evaluation->save();
foreach ($indicators as $id => $value) {
$evaluationDetail = new EvaluationDetail();
foreach ($cf_e as $id => $value) {
$evaluationDetail = new EvaluationDetail;
$evaluationDetail->evaluation_id = $evaluation->id;
$evaluationDetail->indicator_id = $id;
$evaluationDetail->md_value = $value;
$evaluationDetail->cf_e = $value;
$evaluationDetail->save();
}
DB::commit();
$presentase = round($cfCombine * 100, 2);
toast('Data berhasil disimpan', 'success')->position('top-right')->autoclose(3000);
return redirect()->back()->with('presentase', $presentase);
$presentase = round($cf_combined * 100, 2);
toast("Data berhasil disimpan.", "success")->position('top-right')->autoclose(3000);
return redirect()->back()->with(['land' => $landName, 'presentase' => $presentase, 'hasil' => $hasil]);
} catch (\Throwable $th) {
DB::rollBack();
toast('Terjadi kesalahan', 'error')->position('top-right')->autoclose(3000);
toast($th->getMessage(), 'error')->position('top-right')->autoclose(3000);
return redirect()->back();
}
// $cfValue = [];
// foreach ($cf_e as $id => $value) {
// if (isset($rules[$id])) {
// $rule = $rules[$id];
// $cfValue[$id] = $rule->mb * $value;
// } else {
// toast("Rule untuk indikator ID {$id} tidak ditemukan.", 'error')->position('top-right')->autoclose(3000);
// return redirect()->back();
// }
// }
// $cfCombine = array_shift($cfValue);
// foreach ($cfValue as $cf) {
// $cfCombine = $cfCombine + $cf * (1 - $cfCombine);
// }
// DB::beginTransaction();
// try {
// $evaluation = new Evaluation;
// $evaluation->land_id = $request->land;
// $evaluation->cf_value = $cfCombine;
// $evaluation->user_id = auth()->user()->id;
// $evaluation->save();
// foreach ($indicators as $id => $value) {
// $evaluationDetail = new EvaluationDetail();
// $evaluationDetail->evaluation_id = $evaluation->id;
// $evaluationDetail->indicator_id = $id;
// $evaluationDetail->md_value = $value;
// $evaluationDetail->save();
// }
// DB::commit();
// $presentase = round($cfCombine * 100, 2);
// toast('Data berhasil disimpan', 'success')->position('top-right')->autoclose(3000);
// return redirect()->back()->with('presentase', $presentase);
// } catch (\Throwable $th) {
// DB::rollBack();
// toast('Terjadi kesalahan', 'error')->position('top-right')->autoclose(3000);
// return redirect()->back();
// }
}
}

View File

@ -15,7 +15,7 @@ public function up(): void
$table->id();
$table->unsignedBigInteger('evaluation_id');
$table->unsignedBigInteger('indicator_id');
$table->float('md_value');
$table->float('cf_e');
$table->timestamps();
});

View File

@ -52,15 +52,12 @@
<select name="{{ $indicator->id }}" class="form-control"
id="{{ $indicator->id }}-field" required>
<option value="" selected disabled>Pilih Jawaban</option>
<option value="0">Tidak</option>
<option value="0.2">Tidak Tahu</option>
<option value="0.4">Mungkin</option>
<option value="0.6">Kemungkinan Besar</option>
<option value="0.8">Hampir Pasti</option>
<option value="1">Pasti</option>
@foreach ($indicator->rules as $rule)
<option value="{{ $rule->cf }}">{{ $rule->parameter_type }}</option>
@endforeach
</select>
<div class="invalid-feedback">
Pilih Jawaban
Pilih Jawaban {{ $indicator->name }}
</div>
</div>
@endforeach
@ -79,19 +76,25 @@
<form action="">
<div class="card-body text-center">
@if (session('presentase'))
<h1 class="mb-0 mt-2"><span class="counter-value"
data-target="{{ session('presentase') }}">0</span>%
</h1>
<p class="text-muted mb-0 mt-2">Presentase kelayakan tanah berdasarkan perhitungan
Certainty
Factor.</p>
<h1 class="mb-0 mt-2"><span class="counter-value"
data-target="{{ session('presentase') }}">0</span>%
</h1>
@endif
@if (session('hasil') && session('land'))
<p class="text-muted mb-0 mt-2">Hasil perhitungan sistem menetapkan lahan
<b>"{{ session('land') }}"</b>
termasuk
dalam kondisi <b>"{{ session('hasil') }}"</b>
</p>
@endif
</div>
</form>
</div>
</div>
</div>
</div>
<!-- container-fluid -->
</div>
@ -104,7 +107,6 @@
<!-- listjs init -->
<script src="assets/js/pages/form-validation.init.js"></script>
<script src="assets/js/pages/customJs/assessment/form.js"></script>
@endpush
@endsection